SelectPdf for .NET 10 - XFA Form Filling - C# / .NET 10 Sample

This sample shows how to use SelectPdf Library for .NET to fill a dynamic XFA form (a form that lays itself out, as Adobe LiveCycle Designer makes them) through the same field types as an AcroForm one. Setting a value runs the form's own scripts, as in Acrobat: here the form calculates its total and checks the quantity. The form is saved as the same dynamic form, as the pages it lays out, or signed in its own signature field.

Here is our test XFA form (open it in Adobe Acrobat or Reader):
Test XFA form

Name:

Date of birth (yyyy-MM-dd):

Quantity:

Delivery:

I accept the terms

Save as:


XFA mode:

Sample code · C#
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class XfaFormFillingModel : PageModel
    {
        [BindProperty]
        public string TxtName { get; set; } = "John Smith";

        [BindProperty]
        public string TxtBirthDate { get; set; } = "1980-05-17";

        [BindProperty]
        public string TxtQuantity { get; set; } = "3";

        [BindProperty]
        public string DdlDelivery { get; set; } = "EXP";

        [BindProperty]
        public bool ChkAgree { get; set; } = true;

        // XfaForm, Flattened or Signed
        [BindProperty]
        public string DdlOutput { get; set; } = "XfaForm";

        // how the form manager treats a form defined in XFA: Auto, Xfa or AcroForm
        [BindProperty]
        public string DdlXfaMode { get; set; } = "Auto";

        // whether the loaded form is defined in XFA, shown on the page
        public bool IsXfaForm { get; private set; }

        // what the form's scripts reported, and the fields, shown on the page
        public List<string> Messages { get; private set; }

        public List<string[]> Fields { get; private set; }

        public void OnGet()
        {

        }

        // fills the form with the values entered on the page
        private PdfFormManager FillForm()
        {
            string file = Path.Combine(Startup.ServerPath, "wwwroot", "files", "xfa-form.pdf");

            // load the form: a dynamic XFA form is filled through the same field types
            // as an AcroForm one, and its own scripts run as in Acrobat
            PdfFormManager form = new PdfFormManager();

            // how a form defined in XFA is treated, set before the form is loaded: Auto (the
            // default) fills a form that lays itself out as XFA, Xfa also keeps the XFA of a hybrid
            // form in step with its fields, and AcroForm ignores the XFA - a form that lays itself
            // out then has no fields
            form.XfaMode = SampleForm.ParseEnum(DdlXfaMode, PdfFormXfaMode.Auto);
            form.Load(file);

            // whether the form is defined in XFA, whatever the mode says
            IsXfaForm = form.IsXfaForm;

            // a field is found by its path in the form, or by its own name
            // (null when the form has no such field)
            PdfFormFieldTextBox name =
                form.Fields["form1.Page1.Name"] as PdfFormFieldTextBox;
            if (name != null)
                name.Text = TxtName;

            // a date is set as the form keeps it (yyyy-MM-dd)
            PdfFormFieldTextBox birthDate =
                form.Fields["BirthDate"] as PdfFormFieldTextBox;
            if (birthDate != null)
                birthDate.Text = TxtBirthDate;

            // the form calculates its total and validates the quantity
            PdfFormFieldTextBox quantity =
                form.Fields["Quantity"] as PdfFormFieldTextBox;
            if (quantity != null)
                quantity.Text = TxtQuantity;

            PdfFormFieldComboBox delivery =
                form.Fields["Delivery"] as PdfFormFieldComboBox;
            if (delivery != null)
                delivery.SelectedValue = DdlDelivery;

            PdfFormFieldCheckBox agree =
                form.Fields["Agree"] as PdfFormFieldCheckBox;
            if (agree != null)
                agree.Checked = ChkAgree;

            return form;
        }

        public IActionResult OnPost()
        {
            PdfFormManager form = FillForm();

            // save the form: as the same dynamic form, or as the pages it lays out
            form.Flatten = DdlOutput == "Flattened";
            byte[] pdf = form.Save();
            form.Close();

            if (DdlOutput == "Signed")
            {
                // sign the form's own signature field, as Acrobat signs it
                string certFile = Path.Combine(Startup.ServerPath, "wwwroot", "files", "selectpdf.pfx");
                PdfSecurityManager security = new PdfSecurityManager();
                security.Load(pdf);
                security.Sign(certFile, "selectpdf", "Signature",
                    "Order approved", "Bucharest", "");
                pdf = security.Save();
                security.Close();
            }

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }

        public IActionResult OnPostCheck()
        {
            PdfFormManager form = FillForm();

            // press the form's own Check button (a button of the XFA form, so not when the XFA
            // is ignored)
            PdfFormField check = form.Fields["Check"];
            if (check != null && form.XfaMode != PdfFormXfaMode.AcroForm)
                form.Click(check);

            // what the form's scripts reported: its validation and its Check button
            Messages = new List<string>(form.ScriptMessages);
            Messages.AddRange(form.ScriptErrors);

            // the fields as the form shows them
            Fields = new List<string[]>();
            foreach (PdfFormField field in form.Fields)
            {
                Fields.Add(new string[] { field.Caption, field.GetType().Name,
                    field.FormattedValue });
            }

            form.Close();

            // show the results on the page
            return Page();
        }
    }
}