Fill xfa pdf form using itext in java
Question
I have to fill a pdf form (used to submit data online) which is having xfa fields and using iText for this. I am able to generate the reader-enabled pdf document but fields are not filled.
Please suggest how can i get it working.
Solution
All you need is this :
private void fillXmlInPdf(File xmlFile, File inputPdf, File outputPdf) throws IOException, DocumentException, FileNotFoundException, CsmartException {
PdfStamper stamper=null;
try {
PdfReader reader = new PdfReader(inputPdf.getAbsolutePath());
stamper = new PdfStamper(reader, new FileOutputStream(outputPdf), '\0', true);
AcroFields afields = stamper.getAcroFields();
XfaForm xfa = afields.getXfa();
xfa.fillXfaForm(new FileInputStream(xmlFile));
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
stamper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
This code works nicely for me...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow