Harish Moyal
Harish Moyal

Reputation: 123

How to fill a table dynamically using PDFBox in a pdf form template

I have searched a lot but couldn't find appropriate answer to my problem. I am working on a solution with PDFBox library where I have a predefined pdf form template. This template is designed in Adobe InDesign and given to me as an editable pdf form.

My goal is to fill this form programatically where the data will come from some other apis. I am able to fill the fixed part (non repetitive info) of the template by setting the appropriate field of the PDAcroForm object. This works fine and I am able to view the populated pdf properly.

There is a table in this template form and the number of rows can grow dynamically depending upon the data. I am not able to fill this table or you can say I don't know how to create dynamic rows in the template. Can you please advise how to fill the table in the form template? Thanks in advance.

Following is the code I have written:


PDDocument pdfDocument = PDDocument.load(new ClassPathResource("sample_template.pdf").getFile());

PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
acroForm.getField("Field1).setValue("Hello");
acroForm.getField("Field2).setValue("World");
.
.
for (Order eachOrder : Orders) {
// Here I want to fill a table with the data.
// Number of columns are fixed but rows are dynamic.

 acroForm.getField("OrderDate").setValue(eachOrder.getDate());
 acroForm.getField("OrderDesc").setValue(eachOrder.getDescription());
}
// The problem with above loop is that it does not creates new rows but it
// keeps on overwriting the first row (which is obvious because in my template there is only 1 row defined with these 2 columns).
// I don't know how to define a variable row in template and fill it with the data.
.
.
acroForm.flatten();
pdfDocument.save("test.pdf");

Please suggest how to proceed from here.

Upvotes: 1

Views: 1027

Answers (1)

Miyagi
Miyagi

Reputation: 151

The file is a XFA PDF and you need to handle it accordingly. You basically have a field with XML that contains your field values. There is a getXFA() method on your PDAcroForm object.

A short description of XFA can be found at: https://blog.idrsolutions.com/2014/03/extracting-data-xfa-files/

Upvotes: 0

Related Questions