I would like to fill a PDF form with the PDFBox java library. The PDF form is created with Adobe Live Designer, so it uses the XFA format.
I try to find resources about filling XFA PDF forms with PDFBox, but i haven't any luck so far. I saw that a PDAcroForm.setXFA method is available in the API, but i don't see how to use it.
Do you know if it is possible to fill a PDF Form with PDFBox ? If yes, is there anywhere a code sample or a tutorial to achieve this ? If no, what are the best alternatives to achieve this ?
This it the best I was able to manage in the time I was allocated on the problem. I get the pdf saved (in Life Cycle) as optimized (I'm not the one doing the pdf). This is the PDF openning part, XML duplication and then saving:
then the methods who set values for fields. I set both the XFA and the AcroForm:
This work, but not for all "kind" of PDF life Cycle produce, some got a warning message about "extended fonction" not enabled anymore but still work. The optimize version is the only one I found who don't prompt message when openned after being filled.
I fill the XFA and the Acroform otherwise it don't work in all viewer.
I'm not familiar with pdfbox but you can do this with iText (http://itextpdf.com/) once you get access to the XFA (XML) DOM.
The question specifically identifies the PDFBox library in the subject; you do not need iText, the XFA manipulation can be done using the PDXFA object available in PDFBox 1.8.
Many thanks to Maruan Sahyoun for his great work on PDFBox + XFA.
This code only works when you remove all security on the PDDocument.
It also assumes the COS object in PDXFA is a COSStream. The simplistic example below reads the xml stream and writes it back into the PDF.
AcroForm is for PDF with static fields. If PDF have xfa forms you can use itext (Java) or itextsharp ( .net) to Populate your Data . Only problem with XFA Forms are they cannot be Flatten with itext only way to Flatten I found is using bullzip or similar pdf creator to open that xfa pdf created with itext and pass it through bullzip which will spit out flatten pdf version. Hope this will give u some ideas.
Below code just a rough idea how xfa is filled.
you can now use the xfa pdf you created and print through bullzip
const string Printer_Name = "Bullzip PDF Printer";
output file will be flatten pdf..