Flatten PDF interactive form fields in C# and VB.NET

The flattening of the PDF interactive form is the process of retrieving the appearance of form fields and incorporating that appearance into the static content of PDF pages containing those fields and, at the end, removing form fields, thus making the PDF document non-interactive.

The following example shows how to flatten the PDF interactive form with GemBox.Pdf.

PDF interactive form fields flattened with GemBox.Pdf
Screenshot of PDF interactive form fields flattened with GemBox.Pdf
Upload your file (Drag file here)
using GemBox.Pdf;
using GemBox.Pdf.Content;
using GemBox.Pdf.Forms;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("%InputFileName%"))
        {
            // A flag specifying whether to construct appearance for all form fields in the document.
            bool needAppearances = document.Form.NeedAppearances;

            foreach (var field in document.Form.Fields)
            {
                // Do not flatten button fields.
                if (field.FieldType == PdfFieldType.Button)
                    continue;

                // Construct appearance, if needed.
                if (needAppearances)
                    field.Appearance.Refresh();

                // Get the field's appearance form.
                var fieldAppearance = field.Appearance.Get();

                // If the field doesn't have an appearance, skip it.
                if (fieldAppearance == null)
                    continue;

                // Add a new content group to the field's page and
                // add new form content with the field's appearance form to the content group.
                // The content group is added so that transformation from the next statement is localized to the content group.
                var flattenedContent = field.Page.Content.Elements.AddGroup().Elements.AddForm(fieldAppearance);

                // Translate the form content to the same position on the page that the field is in.
                var fieldBounds = field.Bounds;
                flattenedContent.Transform = PdfMatrix.CreateTranslation(fieldBounds.Left, fieldBounds.Bottom);
            }

            // Remove all fields, thus making the document non-interactive,
            // since their appearance is now contained directly in the content of their pages.
            document.Form.Fields.Clear();

            document.Save("FormFlattened.%OutputFileType%");
        }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content
Imports GemBox.Pdf.Forms

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = PdfDocument.Load("%InputFileName%")

            ' A flag specifying whether to construct appearance for all form fields in the document.
            Dim needAppearances = document.Form.NeedAppearances

            For Each field In document.Form.Fields

                ' Do not flatten button fields.
                If field.FieldType = PdfFieldType.Button Then Continue For

                ' Construct appearance, if needed.
                if needAppearances Then field.Appearance.Refresh()

                ' Get the field's appearance form.
                Dim fieldAppearance = field.Appearance.Get()

                ' If the field doesn't have an appearance, skip it.
                If fieldAppearance Is Nothing Then Continue For

                ' Add a new content group to the field's page and
                ' add new form content with the field's appearance form to the content group.
                ' The content group is added so that transformation from the next statement is localized to the content group.
                Dim flattenedContent = field.Page.Content.Elements.AddGroup().Elements.AddForm(fieldAppearance)

                ' Translate the form content to the same position on the page that the field is in.
                Dim fieldBounds = field.Bounds
                flattenedContent.Transform = PdfMatrix.CreateTranslation(fieldBounds.Left, fieldBounds.Bottom)
            Next

            ' Remove all fields, thus making the document non-interactive,
            ' since their appearance is now contained directly in the content of their pages.
            document.Form.Fields.Clear()

            document.Save("FormFlattened.%OutputFileType%")
        End Using
    End Sub
End Module

Using the same approach, you can also flatten PdfAnnotations contained in the Annotations property of the PdfPage class.

Another approach of making the PDF form non-interactive is to make all form fields read-only using the ReadOnly property of the PdfField class.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.