imported form information

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for Java (v4)  /  imported form information

DynamicPDF Generator for Java (v4) Forum

 Mar 01 2010 1:40 PM
Hello,

Suppose I have a document that holds only a PDF form created with Adobe Acrobat. I need to load the PDF document, populate the placeholders with values and import the area into another document. This works fine, however I need in some cases the form fields to still be editable by the user and this is not possible if imported using ImportedPageArea? The code snippet is included below.

How should the code below be changed for the PDF form fields to be populated but still editable?

Thanks in advance,
Best regards,
Giovanni

// load existing PDF document containing form
PdfDocument pdfDocument = new PdfDocument(fileName);
MergeDocument mergeDocument = new MergeDocument(pdfDocument);

// populate placeholders
Map<String, String> replacements = new HashMap<String, String>();
for (Map.Entry<String, String> entry : replacements.entrySet()) {
 PdfFormField field = pdfDocument.getForm().getFields().getPdfFormField(entry.getKey());
 field.createLabel(mergeDocument.getPages().getPage(field.getOriginalPageNumber() - 1), entry.getValue(), Font.getTimesRoman(), 12);
}
                
// output the populated template to a pdfDocument
byte[] data = mergeDocument.draw();
pdfDocument = new PdfDocument(data);

// create an imported area from the in-memory pdfDocument
int pageOne = 1;
int xOffset = 50;
int yOffset = 550;
ImportedPageArea importedArea = new ImportedPageArea(pdfDocument.getPage(pageOne), xOffset, yOffset);
importedArea.getContents().setClipLeft(0);
importedArea.getContents().setClipTop(0);
importedArea.getContents().setClipRight(0);
importedArea.getContents().setClipBottom(0);
 Mar 01 2010 2:56 PM
Posted by a ceTe Software moderator
Hello Giovanni,

When using ImportedPageArea it is not possible to preserve the form fields that exist on the original PDF document and make them editable.

In order to keep the form fields editable, you will have to use the MergeDOcument object, fill the PDF and save it to the file system. Then the PDF can be rendered again for editing to the end user.

Thanks,
ceTe Software Support Team
 Mar 02 2010 3:15 AM
Hello,

Can you please provide a code example for doing this.

Or better yet show me what's being done wrong in the snippet below. The fields will be editable in the intermediate "temp.pdf" file but not in the final document "letter.pdf". I need them to be editable in the final document too.

Thanks in advance,
Best regards,
Giovanni

// load some content
MergeDocument footer = new MergeDocument("content.pdf", new MergeOptions(true));

// fill the placeholders with data
footer.getForm().getFields().getFormField("ph0001").setValue("Juan Perez");
footer.getForm().getFields().getFormField("ph0002").setValue("Head of Customer Acquisition");      
footer.getForm().getFields().getFormField("ph0003").setValue("Dayana Gonzales");      
footer.getForm().getFields().getFormField("ph0004").setValue("Head of Customer Satisfaction");               

// write filled form to a temp file so can be later imported
footer.draw("temp.pdf");

// import the temp pre-populated file into the final document
int pageNumber = 1;
int xOffset = 10;
int yOffset = 400;
ImportedPageArea contentArea = new ImportedPageArea("temp.pdf", pageNumber, xOffset, yOffset);
contentArea.getContents().setClipLeft(0);
contentArea.getContents().setClipTop(0);
contentArea.getContents().setClipRight(0);
contentArea.getContents().setClipBottom(0);

// create a new document contained the pre-populated form but
// still editable for end users
Document document = new Document();

// add one blank page to the document
document.getPages().add(new Page(PageSize.A4));

// create a template and add elements to it
Template template = new Template();

// build the page
template.getElements().add(getContent());
       
// add the template to the document
document.setTemplate(template);
       
document.draw("letter.pdf");
 Mar 02 2010 4:46 AM
Posted by a ceTe Software moderator
Hello,

When importing a page from one PDF to another, it is not possible to get the editable fields from the original PDF. The behavior is something like getting a screen shot of the original PDF on to the final PDF. If you want editable fields then you will have to add new form fields to the final document.

you can get the X, Y width and height etc properties of the existing fields and you can use these values for the new fields. If you are doing clipping or changing the sizes then you will have to use the appropriate position values to the new form fields.

Thanks,
ceTe Software Support Team.

 Mar 02 2010 7:50 AM
Hello,

Can you please provide an example of doing that or point me to a relevant example.

Thanks in advance,
Best regards,
Giovanni
 Mar 02 2010 8:31 AM
Posted by a ceTe Software moderator
Hello Giovanni,

We do not have any example for your scenario. However you can refer to the help documentation for sample codes. You can refer to the PdfFormField members on our help documentation. PdfFormField has GetX(), GetY(), GetOriginalPageNumber() etc methods and Width, Height etc properties to get the form field information.

   PdfDocument pdf = new PdfDocument( filepath );
   float height = pdf.Form.Fields["fieldName"].Height;

You may have to add the new form fields to the final document using this information or using your desired values.

It is not possible to preserve the form fields in the final PDF when importing pages.

Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 2:26 AM.