pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From phi...@free.fr
Subject "Empty" AcroFields
Date Mon, 27 Apr 2015 10:32:35 GMT
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

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: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message