Serialize a PDF Form - C# .NET 6

This tutorial shows how to serialize a PDF Form and fill form fields with JSON and XML files in a C# .NET Console application using the LEADTOOLS SDK.

Summary This tutorial covers how to serialize PDF forms in a C# .NET Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET 6+ (Console)

  • Python: Python

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 Serialize a PDF Form - .NET 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 Serialize PDF Form 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.

using Leadtools; 
using Leadtools.Pdf; 
using Newtonsoft.Json; 
using System.Runtime.Serialization; 
using System.Xml; 

Add four new methods to the Program class named SerializeToJson(string inputPdf, string jsonFile), FillFromJson(string inputPdf, string jsonFile), SerializeToXml(string inputPdf, string xmlFile), and FillFromXml(string inputPdf, string xmlFile). Call these new methods inside the Main() method, as shown below.

Also, add three string values to the Main() method, var inputPdf = @"C:\LEADTOOLS22\Resources\Images\InteractiveForm.pdf", var jsonFile = "formfields.json", and var xmlFile = "formfields.xml".

static void Main(string[] args) 
   // Set the input pdf file 
   var inputPdf = @"C:\LEADTOOLS22\Resources\Images\InteractiveForm.pdf"; 
   var jsonFile = "formfields.json"; 
   var xmlFile = "formfields.xml"; 
   SerializeToJson(inputPdf, jsonFile); 
   FillFromJson(inputPdf, jsonFile); 
   SerializeToXml(inputPdf, xmlFile); 
   FillFromXml(inputPdf, xmlFile); 

Add the code below to the SerializeToJson(string inputPdf, string jsonFile) method to write serialized fields to a JSON file.

static void SerializeToJson(string inputPdf, string jsonFile) 
   // Create a variable to hold the parsed field data 
   var formFields = ParsePdfFields(inputPdf); 
   // Convert the field data to JSON 
   var json = JsonConvert.SerializeObject(formFields); 
   // Write the JSON file 
   File.WriteAllText(jsonFile, json); 

Add the code below to the FillFromJson(string inputPdf, string jsonFile) method to fill serialized fields from a JSON file.

static void FillFromJson(string inputPdf, string jsonFile) 
   // Read the JSON contents 
   var json = File.ReadAllText(jsonFile); 
   // Create a variable to hold the converted JSON data 
   var formFields = JsonConvert.DeserializeObject<List<PDFFormField>>(json); 
   // Create a new PDFFile object 
   var pdfFile = new PDFFile(inputPdf); 
   // Fill the form fields 
   pdfFile.FillFormFields(formFields, inputPdf); 

Add the code below to the SerializeToXml(string inputPdf, string xmlFile) method to write serialized fields to an XML file.

static void SerializeToXml(string inputPdf, string xmlFile) 
   // Parse the PDF fields 
   var formFields = ParsePdfFields(inputPdf); 
   // Get the type of form 
   var type = formFields.GetType(); 
   // Create a DataContractSerializer object that will write the XML 
   var serializer = new DataContractSerializer(type); 
   using var stringWriter = new StringWriter(); 
   using var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true }); 
   // USe the serializer object to write to XML objects 
   serializer.WriteObject(xmlWriter, formFields); 
   // Write the text of the serialized fields 
   File.WriteAllText(xmlFile, stringWriter.ToString()); 

Add the code below to the FillFromXml(string inputPdf, string xmlFile) method to write serialized fields to an XML file.

static void FillFromXml(string inputPdf, string xmlFile) 
   // Read the XML file 
   var xmlString = File.ReadAllText(xmlFile); 
   // Create the object that holds the serialized data 
   var serializer = new DataContractSerializer(typeof(List<PDFFormField>)); 
   // String reader 
   using var stringReader = new StringReader(xmlString); 
   // XML reader 
   using var xmlReader = XmlReader.Create(stringReader); 
   // Reads the form fields from the XML file 
   var formFields = serializer.ReadObject(xmlReader) as List<PDFFormField>; 
   // Creates a pdf file with the data 
   var pdfFile = new PDFFile(inputPdf); 
   // Fills the data in the pdf file 
   pdfFile.FillFormFields(formFields, inputPdf); 

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 serializes the input PDF file to JSON and XML files, and fills the PDF form field data from JSON and XML files.


This tutorial showed how to work with the PDFFile, PDFDocument and PDFFormField to serialize and fill PDF Forms.

See Also

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

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