Hi

> Am 27.04.2015 um 12:32 schrieb [email protected]:
> 
> Hello,
> 
> 
> the following code creates a PDF whose pages contain AcroForms filled in with 
> data extracted from a CSV file.
> The forms' template was created with Acrobat Pro. It is landscaped (+90 
> degree rotation).
> 
> I have the following issues:
> 
> - when I open each generated form in Acrobat, the fields look empty or 
> contain text which is lightly offset to the left and top (for instance, you 
> can only see the text's bottom).
> If I click an "empty" field though, its contents appear.
> - if I set the form's fields to read-only in my code, the text appears, but 
> it is vertical

this behavior means that the appearance might not have been generated 
correctly. Which version of PDFBox are you using? Would you have an empty 
sample form, and the same filled with Acrobat and PDFBox?

Sidenote - although it's defined in the PDF specification how an appearance is 
generated the 'styling' (such a padding around the text, linespacing...) is not 
part of the PDF specification so one can only replicate the behavior of e.g. 
Acrobat. Your form might still look different (or OK) using a different reader.

BR
Maruan

> 
> How can I fix these issues?
> 
> Many thanks.
> 
> Philippe
> 
> 
>       void generatePdf(final List<TreeMap<String, String>> csvDataArrayList, 
> final File pdfTemplateFilePath,
>                       final String pathToGeneratedPdfDirectory)
>                                       throws COSVisitorException, IOException 
> {
> 
>               final File finalDocFilePath = new 
> File(pathToGeneratedPdfDirectory, "newPdf.pdf");
> 
>               final PDDocument finalDoc = new PDDocument();
>               final List<PDField> finalDocFields = new ArrayList<PDField>();
> 
>               int csvLineCounter = 0;
> 
>               // Loop through CSV lines and retrieve each line's data
>               for (Map<String, String> pdfLineTreeMap : csvDataArrayList) {
> 
>                       // Retrieve template's catalog
>                       final PDDocumentCatalog templateCatalog = 
> PDDocument.load(pdfTemplateFilePath).getDocumentCatalog();
>                       // Retrieve its acroForm
>                       final PDAcroForm templateAcroForm = 
> templateCatalog.getAcroForm();
> 
>                       // Get all template PDF's pages
>                       final List<PDPage> templateCatalogPages = 
> templateCatalog.getAllPages();
>                       // Get template document's first page
>                       final PDPage templateFirstPage = 
> templateCatalogPages.get(0);
> 
>                       // Add first page to final doc with filled out fields
>                       finalDoc.addPage(templateFirstPage);
> 
>                       // Loop through PDF field names in pdfLineTreeMap (this 
> map was previously
>                       // created to store the CSV data; its keys are equal to 
> the
>                       // PDF field names) and set their respective values in 
> PDF
>                       for (String fieldName : pdfLineTreeMap.keySet()) {
> 
>                               final String fieldValue = 
> pdfLineTreeMap.get(fieldName);
>                               // Try to retrieve the field in the template's 
> acroForm with the same name
>                               // as the column in csvDataArrayList
>                               final PDField pDField = 
> templateAcroForm.getField(fieldName);
> 
>                               // field with same name in CSV as in PDF was 
> found...
>                               if (pDField != null) {
> 
>                                       // Only circle non-empty insertion codes
>                                       if (fieldName.indexOf("INSERT") >= 0 && 
> fieldValue != null && fieldValue.length() > 0) {
>                                               circleField(pDField, 
> templateFirstPage);
>                                       }
>                                       // add increment to it's partial name
>                                       pDField.setPartialName(fieldName + 
> Integer.toString(csvLineCounter));
>                                       pDField.setValue(fieldValue);
>                                       //pDField.setReadonly(true);
> 
>                                       finalDocFields.add(pDField);
>                               }
> 
>                       } // end for fieldName
> 
>                       // Page number is in templateAcroForm (but not in 
> pdfLineTreeMap)
>                       final PDField pDPageField = 
> templateAcroForm.getField("pagenumber");
>                       if (pDPageField != null) {
>                               pDPageField.setPartialName("pagenumber" + 
> Integer.toString(csvLineCounter));
>                               
> pDPageField.setValue(Integer.toString(csvLineCounter + 1));
>                               //pDPageField.setReadonly(true);
>                               finalDocFields.add(pDPageField);
>                       }
> 
>                       // Stop at second CSV line for debugging !!!!!
>                       if (csvLineCounter == 10) {
>                               break;
>                       }
> 
>                       ++csvLineCounter;
> 
>               } // end for CSV Lines
> 
>               // Create new form in final document
>               final PDAcroForm finalDocAcroForm = new PDAcroForm(finalDoc);
>               // Set final document's form
>               finalDoc.getDocumentCatalog().setAcroForm(finalDocAcroForm);
>               // Set form's fields
>               finalDocAcroForm.setFields(finalDocFields);
>               // Save final doc
>               finalDoc.save(finalDocFilePath);
>               finalDoc.close();
> 
>       } // end generatePdf method
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to