1

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.

  • 1
    *"There is a table in this template form and the number of rows can grow dynamically depending upon the data"* - that sounds like a xfa form, not an acroForm form. – mkl Jun 26 '21 at 17:13
  • Yes I am not able to get xfa fields from the document. Do i need to create the template differently? I do have the header and footer fixed and the centre table is only dynamic. – Harish Moyal Jun 27 '21 at 03:06
  • Most pdf libraries support only AcroForm fields. XFA in pdf has been deprecated since 2017, so more xfa support is unlikely. – mkl Jun 27 '21 at 04:05
  • Thank you for the answer. I am kind of stuck here and I am not able to find any solution on how to proceed. If you have previous experience or any other post which can help me here then it would be a great help. – Harish Moyal Jun 27 '21 at 05:02
  • As mentioned above, most pdf libraries support only AcroForm fields. So does pdfbox. The only option to edit xfa forms with it is to extract the xfa xml from the pdf (pdfbox offers a method to do so), edit the xml manually, and store it again in the pdf. Beware, though, the xfa format for pdf is not completely documented publicly. – mkl Jun 27 '21 at 10:03

1 Answers1

0

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/

Miyagi
  • 154
  • 2
  • 17