Create and fill PDF forms in .NET

XFINIUM.PDF library can both create new PDF forms and fill existing PDF forms.
Each type of form fields supported in the PDF specification is represented by the corresponding class:

  • PdfTextBoxField – textbox fields
  • PdfCheckBoxField – checkboxes
  • PdfRadioButtonField – sets of radiobuttons
  • PdfComboboxField – comboboxes
  • PdfListboxField – listboxes
  • PdfPushbuttonField – push buttons
  • PdfSignatureField – signature fields

The minimum information required to create a form field is the field name and its position on the page. After the field object has been created it has to be added to the page before setting other properties.

PdfTextBoxField firstNameTextBox = new PdfTextBoxField("firstname");
firstNameTextBox.Widgets[0].VisualRectangle = 
    new PdfVisualRectangle(150, 45, 200, 20);
Dim firstNameTextBox As New PdfTextBoxField("firstname")
firstNameTextBox.Widgets(0).VisualRectangle = New PdfVisualRectangle(150, 45, 200, 20)

A PDF field consists of 2 parts: the logical field object which represents an abstract container for a value and the field widget (a widget annotation) which is the field visual representation on a PDF page. A PDF field can have one or more widgets, on the same page or on different pages, and all these widgets display the same field value.
Existing PDF forms can be filled if they are loaded in a PdfFixedDocument object. After the form has been loaded the document’s Fields collection is populated automatically with the fields defined in the form. A field is located in the collection by index or by its name and it is filled by setting the generic Value property defined in the PdfField base class or specific value properties defined on each field type.

PdfFixedDocument document = new PdfFixedDocument("form.pdf");
(document.Form.Fields["firstname"] as PdfTextBoxField).Text = "John";
(document.Form.Fields["lastname"] as PdfTextBoxField).Value = "Doe";
(document.Form.Fields["sex"].Widgets[0] as PdfRadioButtonWidget).Checked = true;
(document.Form.Fields["firstcar"] as PdfComboBoxField).SelectedIndex = 0;
(document.Form.Fields["secondcar"] as PdfListBoxField).SelectedIndex = 1;
(document.Form.Fields["agree"] as PdfCheckBoxField).Checked = true;
Dim document As New PdfFixedDocument("form.pdf")
TryCast(document.Form.Fields("firstname"), PdfTextBoxField).Text = "John"
TryCast(document.Form.Fields("lastname"), PdfTextBoxField).Value = "Doe"
TryCast(document.Form.Fields("sex").Widgets(0), PdfRadioButtonWidget).Checked = True
TryCast(document.Form.Fields("firstcar"), PdfComboBoxField).SelectedIndex = 0
TryCast(document.Form.Fields("secondcar"), PdfListBoxField).SelectedIndex = 1
TryCast(document.Form.Fields("agree"), PdfCheckBoxField).Checked = True

In PDF the form fields have document scope. This means that 2 fields with the same name on the same page or on different pages are in fact a single PDF field with 2 field widgets. When 2 PDF forms are merged, the fields with the same name are merged into a single field. If the fields have different values then only one value is kept and all the other are discarded. If the 2 forms contain fields with the same name but with different types then the result of the field merge operation is undefined and the resulting form might not work correctly.
A possible to solution to the merge problem above is form fields flattening. The flattening operation merges the field’s visual appearance with the page content and removes the field entirely from the document. The appearance of the field remains on the page but the field content or its attributes are no longer editable.

8 thoughts on “Create and fill PDF forms in .NET”

  1. Hi,

    I am trying to read a Adobe Lifecycle designer PDF form fields using the Xfinium library with the below code.

    Stream formStream = await GetStorageFileStream("C:\test\book13.pdf");
    PdfFixedDocument document = new PdfFixedDocument(formStream);
    document.Form.Fields["test"] as PdfTextBoxField).Text = objVal.Value;

    I am unable to read the form fields.
    Please let me know how do we read the form fields.

    Thanks in Advance.

    1. Yes, XFINIUM.PDF library supports FDF form filling. The PdfForm class has the ImportData method which lets you import data in a form in FDF or XFDF format.
      Basic code looks like this:
      PdfFixedDocument doc = new PdfFixedDocument(sourceForm);
      doc.Form.ImportData(fdfData, PdfFormDataFormat.FDF);

      FDF form filling is available on all supported .NET and Xamarin platforms.

  2. Hi I’m using the standard edition of xfinium for trying (for now). This Try should decide if we buy this Framework.

    My Problem is, that i’ve created a pdf form in adobe acrobat pro 8 lifecycle designer and xfinium does not find the fields. I know this problem was posted by another user here, but it is now July 2017. What is the actual state about that problem?

    using Xamarin.Forms -> in Android and iOS Project -> should work ->
    PDF is on external storage. Permissions granted.

    string localPath = Android.OS.Environment.ExternalStorageDirectory.Path + “/test4.pdf”;
    //Stream formStream = asm.GetManifestResourceStream(“test4.pdf”);

    Stream formStream = System.IO.File.OpenRead(localPath);
    long l = formStream.Length;

    output = FormFill.Run(formStream);


    public static SampleOutputInfo[] Run(Stream stream)
    PdfFixedDocument document = new PdfFixedDocument(stream);
    //here no fields found
    (document.Form.Fields[“tf1”] as PdfTextBoxField).Text = “John”;
    (document.Form.Fields[“tf2”] as PdfTextBoxField).Value = “Doe”;

    PdfSignatureField signatureF = document.Form.Fields[“signature”] as PdfSignatureField;


    SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, “formfill.pdf”) };
    return output;

    1. Adobe LiveCycle Designer forms are not supported yet in XFINIUM.PDF library. They will be deprecated in PDF 2.0 so we’re still debating whether to add support for them or not.

  3. Hallo, i would like to save pdf with filled forms as pdfa. what i have to do? is it possible? thank for your advice.

    1. XFINIUM.PDF can save a PDF form as PDF/A if the PDF/A conditions are fulfilled. If the form contains elements that are not allowed in PDF/A files, the form will not be saved as the library will not remove the content. The PDF/A sample shows how to save a PDF file in PDF/A format.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: