1

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.

Ravi Sharma
  • 873
  • 7
  • 24

1 Answers1

1

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

sethu
  • 8,181
  • 7
  • 39
  • 65
  • 2
    Oh.. For that you will need to refer to the XSD within the XFA data. The easier way is to first fill up the data on the pdf. Then click on export data on the pdf. Study the XML and create it the way you would like in java.. – sethu Oct 20 '11 at 14:38
  • Using this i get "Cannot invoke "String.indexOf(int)" because "s" is null" ... i'm using iText 4.2.0 – AurysVrV Jun 12 '23 at 07:14