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.
[Java]
MergeDocument document = new MergeDocument("[PhysicalPath]/MyDocument.pdf");
// Set the field values
document.getForm().getFields().getFormField("TextBox1").setValue("My Text"); // TextBox field
// Make the form read only
document.getForm().setReadOnly(true);
// Encrypt and Secure the document
document.setSecurity(new HighSecurity());
// Save the PDF
document.draw("[PhysicalPath]/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.
[Java]
// Input Document
PdfDocument pdfDocument = new PdfDocument("[PhysicalPath]/Temp/DynamicPDF/Import/fw9_03_AcroForm.pdf");
// Merged Document
MergeDocument document = new MergeDocument(pdfDocument);
// Add the labels to the first page
Page page = document.getPages().getPage(0);
pdfDocument.getForm().getFields().getPdfFormField("f1-1").createLabel(page, "Acme Corp.", com.cete.dynamicpdf.Font.getCourier(), 12);
pdfDocument.getForm().getFields().getPdfFormField("c1-2").createLabel(page, 2.5f, -2.5f, "3", com.cete.dynamicpdf.Font.getZapfDingbats(), 12);
pdfDocument.getForm().getFields().getPdfFormField("f1-4").createLabel(page, "123 Main Street", com.cete.dynamicpdf.Font.getCourier(), 12);
pdfDocument.getForm().getFields().getPdfFormField("f1-5").createLabel(page, "Anytown, DC 12345", com.cete.dynamicpdf.Font.getCourier(), 12);
// Save the PDF
document.draw("[PhysicalPath]/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.
[Java]
// Input Document
PdfDocument pdfDocument = new PdfDocument("[PhysicalPath]/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.getForm().getFields().getPdfFormField("f1-1");
PdfFormField c1_2 = pdfDocument.getForm().getFields().getPdfFormField("c1-2");
PdfFormField f1_4 = pdfDocument.getForm().getFields().getPdfFormField("f1-4");
PdfFormField f1_5 = pdfDocument.getForm().getFields().getPdfFormField("f1-5");
// Add the labels
f1_1.createLabel(pages[f1_1.getOriginalPageNumber() - 1], "Acme Corp.", com.cete.dynamicpdf.Font.getCourier(), 12);
c1_2.createLabel(pages[c1_2.getOriginalPageNumber() - 1], 2.5f, -2.5f, "3", com.cete.dynamicpdf.Font.getZapfDingbats(), 12);
f1_4.createLabel(pages[f1_4.getOriginalPageNumber() - 1], "123 Main Street", com.cete.dynamicpdf.Font.getCourier(), 12);
f1_5.createLabel(pages[f1_5.getOriginalPageNumber() - 1], "Anytown, DC 12345", com.cete.dynamicpdf.Font.getCourier(), 12);
// Save the PDF
document.draw("[PhysicalPath]/MyDoc.pdf");
| See Also |
PdfFormField Class | PdfDocument Class | Security Class | Working With Acro Forms | Programming with Merger for Java

