This repository evaluates the ability for IronPdf and iText to inject C# object data into PDF form fields following a standardized convention. The PDF is generated from an embedded PDF with fields configured to a common naming convention: {Type}.{Property}.
To account for fields that set a property based on a range of values (SSN is a single value, but exposed as 9 numeric form fields), the index of each field is added to the end of the field name: {Type}.{Property}.{Index}. The value is set by using prop.Value.ElementAt(Index).
To account for fields that provide several options for a single value (Gender is only Male or Female, but is exposed as two checkbox inputs), the value is added to the end of the field name: {Type}.{Property}.{Value}. The match is determined by the value of the property being equal to the value position in the name of the checkbox.
Available personnel data from a Person object should be able to be auto-filled into a Social Security Administration Application.
A generic PdfRecord class has been created that uses reflection to enumerate through the properties of a class and generate a collection of PdfRecordProp objects that facilitate mapping C# object data to PDF form fields.
Before getting into generating PDFs, I wanted to be able to test some standard PDF capabilities using the libraries. This included:
- Initializing a PDF document from a provided path.
- Reading details about the form fields contained in the PDF.
- Updating the names of form fields.
The required functionality has been defined by the IManager interface. Common features between implementations have been encapsulated in the abstract ManagerBase class, which implements IManager, IDisposable.
Concrete implementations for each library are defined in the classes IronManager and ITextManager.
The intent of a generator is to:
-
Initialize an instance of a PDF document from a provided source path at the provided destination path.
-
Given a
PdfRecord<T>object, map every instance ofPdfRecordProp.Valueto a field that matches thePdfRecordProp.Mapname using the conventions established at the beginning of this document.
The required functionality has been defined by the IGenerator interface.
Implementations for each library are defined in the classes IronGenerator and ITextGenerator.
A System.CommandLine utility has been created in Pdf.Cli to test the above features.
Root Command Help
generate-iron Help
generate-itext Help
Running generate-iron
PDF Generated by generate-iron
Running generate-itext
PDF Generated by generate-itext
The API can be started by running dotnet run from the src/server/Pdf.Api directory. It will startup at http://localhost:5000/swagger.
To allow the API to properly use an IGenerator as a service, it must first be registered in Program.cs. In this case, the instance provided is of type ITextGenerator.
To allow the PDF metadata to be exposed to the client, the CORS configuration must expose the content-disposition header.
The GeneratorController.GeneratePdf endpoint:
-
Receives a
Personin the POST body. -
Uses the
IGeneratorservice to generate a SSN form from the template PDF in the app domain base directory. -
Once generated, the bytes are captured in memory, the file is deleted, and the data is streamed back to the client.
The app can be started by running npm run start from the src/app directory.
The Person model is created to capture the shape of the Person data.
The GeneratorService class maps the API methods from the GeneratorController to the Angular app.
Particularly, the generatePdf function:
-
POSTs the provided
Personrecord to the API endpoint and the expected response type is set toblob. -
Once the response is received, the
content-dispositionheader is retrieved to extract the PDF metadata. -
If the header and the body have values, the filename is extracted.
-
An anchor element is generated with the contents of the body set as the
hrefand the filename set as thedownload. -
The anchor is clicked, initiating the PDF to be downloaded by the client.
All of this functionality is wired up to a button through the HomeRoute.
Debugging profiles for generate-iron and generate-itext are available via the Debug menu:
Tasks for generate-iron and generate-itext are available.
They can be accessed by:
-
Command Palette (Ctrl + P) and typing
task: -
Installing the Task Explorer extension:









