Parse, Edit, and Save PDF Form Fields - .NET Core

This tutorial shows how to work with PDF form fields in a C# .NET Core Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to load, edit, and save PDF form fields in a C# .NET Core Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core Console Application
IDE Visual Studio 2019, 2022
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Parse, Edit, and Save PDF Form Fields - .NET Core tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet packages:

For a complete list of which files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the PDFFormField Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs.

C#
using System; 
using System.Collections.Generic; 
using Leadtools; 
using Leadtools.Pdf; 

Add a new method to the Program class named ParsePDFFields(string inFile) to return a list of parsed PDFFormField objects from a loaded PDF file. Call this method below the set license code in the Main() method:

C#
static List<PDFFormField> ParsePDFFields(string inFile) 
{ 
    PDFDocument pdfDocument = new PDFDocument(inFile); 
    pdfDocument.ParsePages(PDFParsePagesOptions.FormFields, 1, -1); 
 
    var fields = new List<PDFFormField>(); 
    foreach (var page in pdfDocument.Pages) 
        fields.AddRange(page.FormFields); 
 
    return fields; 
} 

Add another method in the Program class named EditFormFields(List<PDFFormField> editedFields) to take the parsed form fields, edit them, and return them to the Main() method to be saved. Add this method inside the Main() method below the call to ParsePDFFields().

C#
static List<PDFFormField> EditFormFields(List<PDFFormField> editedFields) 
{ 
    foreach (var field in editedFields) 
    { 
        if (field.ContentType == PDFFormField.ContentTypeUnrestrained) 
        { 
            field.Contents = new List<string> { "Hello world!" }; 
        } 
    } 
 
    return editedFields; 
} 

Add the code below to the Main() method, below the call to EditFormFields(), to save the modified form fields.

C#
static void Main(string[] args) 
{ 
    if (!SetLicense()) 
        Console.WriteLine("Error setting license"); 
    else 
        Console.WriteLine("License file set successfully"); 
 
    var inFile = @"C:\LEADTOOLS22\Resources\Images\InteractiveForm.pdf"; 
    var outFile = @"C:\LEADTOOLS22\Resources\Images\FormFieldsOutput.pdf"; 
 
    // Load and parse PDF form fields 
    List<PDFFormField> fields = ParsePDFFields(inFile); 
    // Edit the fields 
    List<PDFFormField> editedFields = EditFormFields(fields); 
    // Save the fields 
    PDFFile formDocument = new PDFFile(inFile); 
    formDocument.FillFormFields(editedFields, outFile); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the form fields inside the PDF are loaded, parsed, edited, and saved back out to PDF file.

Wrap-up

This tutorial showed how to work with the PDFFile, PDFDocument and PDFFormField classes.

See Also

Help Version 22.0.2022.9.27
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2022 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2022 LEAD Technologies, Inc. All Rights Reserved.