Skip to main content

Form Flattening

Version: 6.0

Form Flattening

The easiest way to flatten an Acro Form is to mark it as read only and use Security to prevent the modification of the form field values.

This example shows how to mark form fields as read only and encrypt the document to prevent modification.

MergeDocument document = new MergeDocument( @"C:\MyDocument.pdf" );
// Set the field values
document.Form.Fields["TextBox1"].Value = "My Text"; // TextBox field
// Make the form read only
document.Form.IsReadOnly = true;
// Encrypt and Secure the document
document.Security = new HighSecurity();
document.Security.AllowEdit = false;
// Save the PDF
document.Draw( @"C:\MyDocument.pdf" );
Dim MyDocument As MergeDocument = New MergeDocument( "C:\MyDocument.pdf" )
' Set the field values
MyDocument.Form.Fields( "TextBox1" ).Value = "My Text" ' TextBox field
' Make the form read only
MyDocument.Form.IsReadOnly = True
' Encrypt and Secure the document
MyDocument.Security = New HighSecurity()
MyDocument.Security.AllowEdit = False
' Save the PDF
MyDocument.Draw( "C:\MyDocument.pdf" )

Another way to flatten an Acro Form is by using the Security class.

This example shows how to use the Security to prevent the form filling and modification of the form field values.

MergeDocument document = new MergeDocument( @"C:\MyDocument.pdf" );
// Set the field values
document.Form.Fields["TextBox1"].Value = "My Text"; // TextBox field
// Encrypt and Secure the document
document.Security = new HighSecurity();
document.Security.AllowEdit = false;
document.Security.AllowUpdateAnnotsAndFields = false;
document.Security.AllowFormFilling = false;
// Save the PDF
document.Draw( @"C:\MyDocument.pdf" );
Dim MyDocument As MergeDocument = New MergeDocument( "C:\MyDocument.pdf" )
' Set the field values
MyDocument.Form.Fields( "TextBox1" ).Value = "My Text" ' TextBox field
' Encrypt and Secure the document
MyDocument.Security = New HighSecurity()
MyDocument.Security.AllowEdit = False
MyDocument.Security.AllowUpdateAnnotsAndFields = False
MyDocument.Security.AllowFormFilling = False
' Save the PDF
MyDocument.Draw( "C:\MyDocument.pdf" )

Replace Acro Form Fields with Text​

If you would prefer to remove the form fields entirely and place text at their coordinates, this can be done using the CreateLabel method of the PdfFormField class. The coordinates of the form field can also be read if other PageElements are required.

// Input Document
PdfDocument pdfDocument = new PdfDocument( @"C:\Temp\DynamicPDF\Import\fw9_03_AcroForm.pdf" );
// Merged Document
MergeDocument document = new MergeDocument( pdfDocument, MergeOptions.None );
// Add the labels to the first page
Page page = document.Pages[0];
pdfDocument.Form.Fields["f1-1"].CreateLabel( page, "Acme Corp.", ceTe.DynamicPDF.Font.Courier, 12 );
pdfDocument.Form.Fields["c1-2"].CreateLabel( page, 2.5f, -2.5f, "3", ceTe.DynamicPDF.Font.ZapfDingbats, 12 );
pdfDocument.Form.Fields["f1-4"].CreateLabel( page, "123 Main Street", ceTe.DynamicPDF.Font.Courier, 12 );
pdfDocument.Form.Fields["f1-5"].CreateLabel( page, "Anytown, DC 12345", ceTe.DynamicPDF.Font.Courier, 12 );
// Save the PDF
document.Draw( @"C:\MyDoc.pdf" );
' Input Document
Dim MyPdfDocument as PdfDocument = New PdfDocument( "C:\Temp\DynamicPDF\Import\fw9_03_AcroForm.pdf" )
' Merged Document
Dim MyDocument As MergeDocument = New MergeDocument( MyPdfDocument, MergeOptions.None )
' Add the labels to the first page
Dim MyPage as Page = MyDocument.Pages( 0 )
MyPdfDocument.Form.Fields( "f1-1" ).CreateLabel( MyPage, "Acme Corp.", ceTe.DynamicPDF.Font.Courier, 12 )
MyPdfDocument.Form.Fields( "c1-2" ).CreateLabel( MyPage, 2.5f, -2.5f, "3", ceTe.DynamicPDF.Font.ZapfDingbats, 12 )
MyPdfDocument.Form.Fields( "f1-4" ).CreateLabel( MyPage, "123 Main Street", ceTe.DynamicPDF.Font.Courier, 12 )
MyPdfDocument.Form.Fields( "f1-5" ).CreateLabel( MyPage, "Anytown, DC 12345", ceTe.DynamicPDF.Font.Courier, 12 )
' Save the PDF
MyDocument.Draw( "C:\MyDocument.pdf" )

If the page that the original form field was located on is not known ahead of time it can be read using the form field's GetOriginalPageNumber method. Note that this information is not required by the PDF spec and the method will return a value of -1 if this information is not provided in the import document.

// Input Document
PdfDocument pdfDocument = new PdfDocument( @"C:\Temp\DynamicPDF\Import\fw9_03_AcroForm.pdf" );
// Merged Document
MergeDocument document = new MergeDocument();
// Append the input document and return a Pages array
Page[] pages = document.Append( pdfDocument, MergeOptions.None );
// Retrieve the form fields
PdfFormField f1_1 = pdfDocument.Form.Fields[ "f1-1" ];
PdfFormField c1_2 = pdfDocument.Form.Fields[ "c1-2" ];
PdfFormField f1_4 = pdfDocument.Form.Fields[ "f1-4" ];
PdfFormField f1_5 = pdfDocument.Form.Fields[ "f1-5" ];
// Add the labels
f1_1.CreateLabel( pages[f1_1.GetOriginalPageNumber() - 1], "Acme Corp.", ceTe.DynamicPDF.Font.Courier, 12 );
c1_2.CreateLabel( pages[c1_2.GetOriginalPageNumber() - 1], 2.5f, -2.5f, "3", ceTe.DynamicPDF.Font.ZapfDingbats, 12 );
f1_4.CreateLabel( pages[f1_4.GetOriginalPageNumber() - 1], "123 Main Street", ceTe.DynamicPDF.Font.Courier, 12 );
f1_5.CreateLabel( pages[f1_5.GetOriginalPageNumber() - 1], "Anytown, DC 12345", ceTe.DynamicPDF.Font.Courier, 12 );
// Save the PDF
document.Draw( @"C:\MyDoc.pdf" );
' Input Document
Dim MyPdfDocument as PdfDocument = New PdfDocument( "C:\Temp\DynamicPDF\Import\fw9_03_AcroForm.pdf" )
' Merged Document
Dim MyDocument As MergeDocument = New MergeDocument()
' Append the input document and return a Pages array
Dim MyPages as Page() = MyDocument.Append( MyPdfDocument, MergeOptions.None )
' Retrieve the form fields
Dim MyF1_1 as PdfFormField = MyPdfDocument.Form.Fields( "f1-1" )
Dim MyC1_2 as PdfFormField = MyPdfDocument.Form.Fields( "c1-2" )
Dim MyF1_4 as PdfFormField = MyPdfDocument.Form.Fields( "f1-4" )
Dim MyF1_5 as PdfFormField = MyPdfDocument.Form.Fields( "f1-5" )
' Add the labels
MyF1_1.CreateLabel( MyPages( MyF1_1.GetOriginalPageNumber() - 1 ), "Acme Corp.", ceTe.DynamicPDF.Font.Courier, 12 )
MyC1_2.CreateLabel( MyPages( MyC1_2.GetOriginalPageNumber() - 1 ), 2.5f, -2.5f, "3", ceTe.DynamicPDF.Font.ZapfDingbats, 12 )
MyF1_4.CreateLabel( MyPages( MyF1_4.GetOriginalPageNumber() - 1 ), "123 Main Street", ceTe.DynamicPDF.Font.Courier, 12 )
MyF1_5.CreateLabel( MyPages( MyF1_5.GetOriginalPageNumber() - 1 ), "Anytown, DC 12345", ceTe.DynamicPDF.Font.Courier, 12 )
' Save the PDF
MyDocument.Draw( "C:\MyDoc.pdf" )

Replace Acro Form Fields with Text and Preserving Font Formatting​

The above examples show you how to replace the form fields on a PDF with a Label. While doing this you can also determine the actual font and font size of the text in the original form fields.  This is demonstrated in the code below:

PdfDocument pdf = new PdfDocument(@"C:\Temp\DynamicPDF\Import\fw9_03_AcroForm.pdf");
MergeDocument document = new MergeDocument(pdf, new MergeOptions(false));
PdfFormField field = pdf.Form.Fields["f1-1"];
Page page = document.Pages[field.GetOriginalPageNumber() - 1];
field.CreateLabel(page, "New Label", field.Font, field.FontSize);
document.Draw(@"C:\MyDoc.pdf");
Dim pdf As New PdfDocument("C:\Temp\DynamicPDF\Import\fw9_03_AcroForm.pdf")
Dim document As New MergeDocument(pdf, New MergeOptions(False))
Dim field As PdfFormField = pdf.Form.Fields("f1-1")
Dim page As Page = document.Pages(field.GetOriginalPageNumber() - 1)
field.CreateLabel(page, "New Label", field.Font, field.FontSize)
document.Draw("C:\MyDoc.pdf")