You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by ph...@free.fr on 2015/04/27 12:32:35 UTC
"Empty" AcroFields
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
Re: "Empty" AcroFields
Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,
> Am 30.04.2015 um 11:31 schrieb phiroc@free.fr:
>
>
> Hello,
>
> I have solved my problems by re-creating my template from scratch with OpenOffice and exporting it to PDF, instead of using a template created by one of my colleagues using an MS Word file which he imported into Acrobat Pro.
>
> Case closed.
>
thanks Philippe for letting us know - I was just about to take a look at your files.
Good luck with your project.
Maruan
> Philippe
>
>
> ----- Mail original -----
> De: phiroc@free.fr
> À: users@pdfbox.apache.org
> Envoyé: Lundi 27 Avril 2015 14:15:56
> Objet: Re: "Empty" AcroFields
>
> Hi Maruan,
>
> I've printed one of the generated PDFs and some of the fields contain vertical text.
>
> Best regards,
>
> Philippe
>
> ----- Mail original -----
> De: phiroc@free.fr
> À: users@pdfbox.apache.org
> Envoyé: Lundi 27 Avril 2015 13:46:45
> Objet: Re: "Empty" AcroFields
>
> Hi Maruan,
>
> I am using version 1.8.9.
>
> Yes, I can provide the empty sample and a filled-out form.
>
> May I send them to your e-mail address?
>
> Thanks.
>
> Philippe
>
>
>
> ----- Mail original -----
> De: "Maruan Sahyoun" <sa...@fileaffairs.de>
> À: users@pdfbox.apache.org
> Envoyé: Lundi 27 Avril 2015 12:40:04
> Objet: Re: "Empty" AcroFields
>
> Hi
>
>> Am 27.04.2015 um 12:32 schrieb phiroc@free.fr:
>>
>> 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: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
Re: "Empty" AcroFields
Posted by ph...@free.fr.
Hello,
I have solved my problems by re-creating my template from scratch with OpenOffice and exporting it to PDF, instead of using a template created by one of my colleagues using an MS Word file which he imported into Acrobat Pro.
Case closed.
Philippe
----- Mail original -----
De: phiroc@free.fr
À: users@pdfbox.apache.org
Envoyé: Lundi 27 Avril 2015 14:15:56
Objet: Re: "Empty" AcroFields
Hi Maruan,
I've printed one of the generated PDFs and some of the fields contain vertical text.
Best regards,
Philippe
----- Mail original -----
De: phiroc@free.fr
À: users@pdfbox.apache.org
Envoyé: Lundi 27 Avril 2015 13:46:45
Objet: Re: "Empty" AcroFields
Hi Maruan,
I am using version 1.8.9.
Yes, I can provide the empty sample and a filled-out form.
May I send them to your e-mail address?
Thanks.
Philippe
----- Mail original -----
De: "Maruan Sahyoun" <sa...@fileaffairs.de>
À: users@pdfbox.apache.org
Envoyé: Lundi 27 Avril 2015 12:40:04
Objet: Re: "Empty" AcroFields
Hi
> Am 27.04.2015 um 12:32 schrieb phiroc@free.fr:
>
> 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: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
Re: "Empty" AcroFields
Posted by ph...@free.fr.
Hi Maruan,
I've printed one of the generated PDFs and some of the fields contain vertical text.
Best regards,
Philippe
----- Mail original -----
De: phiroc@free.fr
À: users@pdfbox.apache.org
Envoyé: Lundi 27 Avril 2015 13:46:45
Objet: Re: "Empty" AcroFields
Hi Maruan,
I am using version 1.8.9.
Yes, I can provide the empty sample and a filled-out form.
May I send them to your e-mail address?
Thanks.
Philippe
----- Mail original -----
De: "Maruan Sahyoun" <sa...@fileaffairs.de>
À: users@pdfbox.apache.org
Envoyé: Lundi 27 Avril 2015 12:40:04
Objet: Re: "Empty" AcroFields
Hi
> Am 27.04.2015 um 12:32 schrieb phiroc@free.fr:
>
> 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: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
Re: "Empty" AcroFields
Posted by ph...@free.fr.
Hi Maruan,
I am using version 1.8.9.
Yes, I can provide the empty sample and a filled-out form.
May I send them to your e-mail address?
Thanks.
Philippe
----- Mail original -----
De: "Maruan Sahyoun" <sa...@fileaffairs.de>
À: users@pdfbox.apache.org
Envoyé: Lundi 27 Avril 2015 12:40:04
Objet: Re: "Empty" AcroFields
Hi
> Am 27.04.2015 um 12:32 schrieb phiroc@free.fr:
>
> 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: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
Re: "Empty" AcroFields
Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi
> Am 27.04.2015 um 12:32 schrieb phiroc@free.fr:
>
> 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: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org