Fill xfa pdf form using itext in java

3.9k views Asked by At

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.

1

There are 1 answers

2
sethu On BEST ANSWER

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...