This tutorial shows how to work with PDF form fields in a C# .NET 6 Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to load, edit, and save PDF form fields in a C# .NET 6 Console application. |
Completion Time | 15 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET 6 Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or Higher |
Development License | Download LEADTOOLS |
Try it in another language |
|
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 6 tutorial.
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:
Leadtools.Pdf
For a complete list of which files are required for your application, refer to Files to be Included With Your Application.
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:
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
.
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:
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()
.
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.
static void Main(string[] args)
{
InitLEAD();
var inFile = @"C:\LEADTOOLS23\Resources\Images\InteractiveForm.pdf";
var outFile = @"C:\LEADTOOLS23\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 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.
This tutorial showed how to work with the PDFFile
, PDFDocument
and PDFFormField
classes.