Create and fill PDF forms in .NET 6

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.

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.

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.

6 thoughts on “Create and fill PDF forms in .NET

  1. Reply Bala Nov 16,2016 5:00 am

    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.

  2. Reply Bala Dec 22,2016 7:06 pm

    Does Xfinium support Forms Data Format (FDF) to populate the form pdf

    • Reply xfinium.pdf Dec 23,2016 8:54 am

      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);
      doc.Save(filledForm);

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

  3. Reply ToniW Jul 4,2017 7:17 am

    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;

    document.Form.FlattenFields();

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

    • Reply xfinium.pdf Jul 4,2017 2:30 pm

      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.

Leave a Reply