Skip to content

JaimeStill/pdf-processing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PDF Processing

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.

Scenario

Back to Top

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.

Managers

Back to Top

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.

Generators

Back to Top

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 of PdfRecordProp.Value to a field that matches the PdfRecordProp.Map name 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.

CLI

Back to Top

A System.CommandLine utility has been created in Pdf.Cli to test the above features.

Root Command Help

image

generate-iron Help

image

generate-itext Help

image

Running generate-iron

image

PDF Generated by generate-iron

image

Running generate-itext

image

PDF Generated by generate-itext

image

API

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:

  1. Receives a Person in the POST body.

  2. Uses the IGenerator service to generate a SSN form from the template PDF in the app domain base directory.

  3. Once generated, the bytes are captured in memory, the file is deleted, and the data is streamed back to the client.

App

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:

  1. POSTs the provided Person record to the API endpoint and the expected response type is set to blob.

  2. Once the response is received, the content-disposition header is retrieved to extract the PDF metadata.

  3. If the header and the body have values, the filename is extracted.

  4. An anchor element is generated with the contents of the body set as the href and the filename set as the download.

  5. 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.

VS Code Debugging

Back to Top

Debugging profiles for generate-iron and generate-itext are available via the Debug menu:

image

VS Code Tasks

Back to Top

Tasks for generate-iron and generate-itext are available.

They can be accessed by:

  • Command Palette (Ctrl + P) and typing task :

    image

  • Installing the Task Explorer extension:

    image

Back to Top

About

Evaluate the ability for IronPdf and iText to inject C# object data into PDF form fields.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published