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.
[Visual Basic]
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" )
[C#]
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" );
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.
[Visual Basic]
' 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 )
' 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" )
[C#]
// Input Document
PdfDocument pdfDocument = new PdfDocument( @"C:\Temp\DynamicPDF\Import\fw9_03_AcroForm.pdf" );
// Merged Document
MergeDocument document = new MergeDocument( pdfDocument );
// 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" );
If the page that the original form field was located on is not known ahead of time it can be read using the form fields 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.
[Visual Basic]
' 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() = objDocument.Append( MyPdfDocument )
' 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" )
[C#]
// 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 );
// 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" );
| See Also |
PdfFormField Class | PdfDocument Class | CreateLabel Method | GetOriginalPageNumber Method | Security Class | Working with Acro Forms | Programming with Merger for .NET

