Skip to main content

Form Flattening

Version: 12.0

Form Flattening

Flattening the form fields of a PDF is the process that takes the values of the form fields, adds them to the PDF stream, and then removes the form field structures from the document. The advantages of flattening form fields are that you achieve a PDF that is simpler and smaller, as well as a PDF where the field values can be more easily edited.

Document Level Flattening​

The FormOutput.Flatten option flattens a PDF's form fields. A MergeDocument has a Form property that represents a document's form. The Form class has an Output property that is a FormOutput enumeration.

FormOutput Fields​

EnumerationValueDescription
FormOutput.Flatten0Form fields will be flattened.
FormOutput.Remove1Form fields will be removed.
FormOutput.Retain2Form fields will be retained.

The following example demonstrates how to flatten a PDF's form fields using the FormOutput.Flatten enumeration choice.

MergeDocument document = new MergeDocument(pdfFilePath);
document.Form.Output = FormOutput.Flatten;
document.Draw(pdfFilePath);
Dim document As MergeDocument = New MergeDocument(pdfFilePath)
document.Form.Output = FormOutput.Flatten
document.Draw(pdfFilePath)

[FormFlatteningExample.cs](https://github.com/DynamicPDF/examples-dynamicpdf-core-suite-dotnet-core/blob/master/dynamicpdf-csharp-examples/Examples/FormFlatteningExample.cs){#gitlink-id target=_blank}


[FormFlatteningExample.vb](https://github.com/DynamicPDF/examples-dynamicpdf-core-suite-dotnet-core/blob/master/dynamicpdf-vbnet-examples/Examples/FormFlatteningExample.vb){#gitlink-id target=_blank}

Field Level Flattening​

Individual fields can also be flattened rather than an entire form. A MergeDocument has a Form property, which has a Fields property. The Fields property is a FormFieldsList that contains the individual form fields. The FormField class has an Output property that accepts a FormFieldOutput enumeration.

FormFieldOutput Fields​

EnumerationValueDescription
FormFieldOutput.Flatten0The field will be flattened.
FormFieldOutput.Inherit3Field's output will depend on the document's form level output settings.
FormFieldOutput.Remove1The field will be removed.
FormFieldOutput.Retain2The field will be retained.

The following example demonstrates how to flatten individual form field within a PDF.

MergeDocument document = new MergeDocument(pdfFilePath);
document.Form.Fields[0].Output = FormFieldOutput.Flatten;
document.Draw(pdfFilePath);
Dim document As MergeDocument = New MergeDocument(pdfFilePath)
document.Form.Fields(0).Output = FormFieldOutput.Flatten
document.Draw(pdfFilePath);

[FormFlatteningExample.cs](https://github.com/DynamicPDF/examples-dynamicpdf-core-suite-dotnet-core/blob/master/dynamicpdf-csharp-examples/Examples/FormFlatteningExample.cs){#gitlink-id target=_blank}


[FormFlatteningExample.vb](https://github.com/DynamicPDF/examples-dynamicpdf-core-suite-dotnet-core/blob/master/dynamicpdf-vbnet-examples/Examples/FormFlatteningExample.vb){#gitlink-id target=_blank}

Preserving Digital Signatures During Flattening​

Digital Signatures are default flattened along with all the other form fields in the PDF (as shown above). If you wish to retain the Digital Signatures during the form field flattening, then you can use the Form class's SignatureFieldsOutput property, which takes a FormFieldOutput enumeration. Adding the following line, if the form had digital signatures, to the above example would retain the Digital Signatures.

document.Form.SignatureFieldsOutput = FormFieldOutput.Retain;
document.Form.SignatureFieldsOutput = FormFieldOutput.Retain