Making PDF forms using Aspose.PDF for .NET (part 1)

Andriy Andruhovski
asposepdf
Published in
4 min readDec 12, 2018
Photo by Cytonn Photography on Unsplash

One of the powerful features of PDF is an interactive form. The PDF form is a collection of fields for gathering information interactively from the user. Data fields that appear as placeholders for user-supplied data.

In Acrobat, we can use text string fields, numeric fields, date fields, calculation fields, signature fields, and a variety of custom fields created with JavaScripts.

The advantage of using forms is that doing so enables we to maintain design integrity for the appearance of a form while providing you powerful control over data management.

It is clear that in most cases, forms are created by Adobe Acrobat or other client software. But in documents processing sometimes we need to automate such creation.

So, in this post I’m going to talk about how to we can add form fields into PDF documents using Aspose.PDF for .NET.

Disclaimer: Aspose.PDF .NET is a paid product and have some limitations in a trial version. If you want to test your own code or complex examples from this post, please obtain a temporary license.

We have several ways to create PDF forms programmatically with C#:

  • place all elements manually;
  • generate a PDF form using some C# class (i.g. POCO from EntityFramework);
  • generate a PDF from using HTML layout (with form-element);

According to PDF Standard v.1.7 we can use 4 types of fields:

  • button fields (pushbuttons, checkboxes, radio buttons);
  • choice fields (list boxes, combo boxes);
  • text fields;
  • signature fields;

To add a form field we need to perform two steps:

  1. Create the form field you want to add.
  2. Call the Add method of the Form collection.

Obviously, that each type has a number of properties. Let’s consider simple cases — adding most used fields with minimal settings.

Adding a text field to the PDF document

// Open document
Document pdfDocument = new Document("demo.pdf");
// Create a field
TextBoxField textBoxField = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(10, 600, 160, 32));
textBoxField.PartialName = "textbox1";
textBoxField.Value = "Text Box Value";
// Create a border
Border border = new Border(textBoxField);
border.Width = 1;
border.Dash = new Dash(1, 1);
textBoxField.Border = border;
textBoxField.Color = Aspose.Pdf.Color.Green;
// Add field to the document
pdfDocument.Form.Add(textBoxField, 1);
// Save modified PDF
pdfDocument.Save("demoForm.pdf");

Adding a combobox field to the PDF document

// Open document
Document pdfDocument = new Document("demo.pdf");
// Create a field
ComboBoxField combo = new ComboBoxField(pdfDocument.Pages[1],
new Aspose.Pdf.Rectangle(200, 600, 350, 616));
// Add option to ComboBox
combo.AddOption("North Region");
combo.AddOption("South Region");
combo.AddOption("West Region");
combo.AddOption("East Region");
// Add field to the document
doc.Form.Add(combo);
// Save modified PDF
pdfDocument.Save("demoForm.pdf")

As we can see, to add the field we need to create an instance of the appropriate class and pass to the constructor the page reference and the rectangle’s coordinates.

In practice, we can get these coordinates using the features of the document. For example, assume we have a document with some personal info fields (as shown on the figure below). Usually, the underscore chars are used to reserve a space for user inputs.

Demo document with personal info fields

Therefore, we can find the location of these char on the page and use it for the fields placement.

Making a PDF form from the existing document

The following example shows how we can place fields into the existing document where user inputs marked with underscore chars. To get the underscore strings location we will use a TextFragmentAbsorber class, which allows finding text fragments by regular expression.

Having found the necessary text fragments, we place the text fields, but slightly shift them up.

var pageNum = 1;
var document = new Document("PersonalInfo.pdf");
var textFragmentAbsorber =
new TextFragmentAbsorber("(_+)", new TextSearchOptions(true));
textFragmentAbsorber.Visit(document.Pages[pageNum]);
var count = 1;
foreach (var textFragment in textFragmentAbsorber.TextFragments)
{
//Move text field up
textFragment.Rectangle.LLY++;
textFragment.Rectangle.URY++;
var textBoxField = new TextBoxField(document.Pages[1],
textFragment.Rectangle)
{
PartialName = $"textBoxField{count++}",
DefaultAppearance = { FontName = "Calibri", FontSize = 11 },
Required = true
};

var border = new Border(textBoxField)
{
Width = 1,
Dash = new Dash(1, 1)
};
textBoxField.Border = border;
document.Form.Add(textBoxField, pageNum);
}
document.Save(@"formDemo.pdf");

After the running this code we get the following layout:

PDF document with the text fields

Let’s sum up with this part. Now we learned how to add simple fields and place them into the existing document.

In the next part, we learn how to add checkboxes and radio buttons.

--

--