1

I've tried to extract all the fields out of a dynamic form. But I've observed that the code worked for some forms while not for others. Worst, the code worked differently for the same form but two different downloaded files. But after digging a lot, I found that those forms which correctly, were freshly processed. Not even a single details were filled from a PDF Software(Adobe Reader). Also, if the form was filled and saved the thumbnail of the form in the explorer would change from. The code snippet is as follows:

PdfDocument pdfDoc;
pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfDictionary perms = pdfDoc.getCatalog().getPdfObject().getAsDictionary(PdfName.Perms);
    if (perms != null) {
        perms.remove(new PdfName("UR"));
        perms.remove(PdfName.UR3);
        if (perms.size() == 0) {
            pdfDoc.getCatalog().remove(PdfName.Perms);
        }
    }
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);

    List<String> result = new ArrayList<String>(form.getFormFields().keySet());

    Map<String, PdfFormField> fields = form.getFormFields();

Below is the image for the same form, but downloaded twice. The one with the colorful thumbnail is not filled. Other is filled using Adobe Reader and saved, and on saving the thumbnail vanished.

I suspect a flag might get set on saving the form. Any help is appreciated.Another peculiar observation, there was a mismatch in the number of parameters in the PdfCatalog object for the above two forms. An entry for the property 'NeedsRendering' was present in the faulty PDF and otherwise for the working PDf. I've attached screenshots for the working PDF during a debugging session.

hrsa_working_form:

hrsa_working_form

Update 1 @Browno, apologies for the confusing question from a newbie's mind. I've posted the screenshots from the itext RUPS for the key '/AcroForm'. On exploring the answers for XFAForm, I've learned how to fill them. But flattening them causes an exception. I've used the pdfxfa jar under the license of AGPL. I'm lacking the knowledge of XFAFlattener and it's properties used in the XFAFlattenerProperties class. Below is the code snapshot:

public void fillData2(String src, String xml, String dest, String newDest){
        throws IOException, ParserConfigurationException, SAXException, InterruptedException {
    PdfReader reader = new PdfReader(src);
    reader.setUnethicalReading(true);
    PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest), new StampingProperties().useAppendMode());
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);

    List<String> result = new ArrayList<String>(form.getFormFields().keySet());
    System.out.println(result.size());
    XfaForm xfa = form.getXfaForm();
    xfa.fillXfaForm(new FileInputStream(xml));
    xfa.write(pdfDoc);
    //form.flattenFields(); throws exception
    pdfDoc.close();
    FileInputStream fis = new FileInputStream(dest);
    FileOutputStream fos = new FileOutputStream(newDest);
    XFAFlattener xfaFlattener = new XFAFlattener();
    xfaFlattener.setFontSettings(new XFAFontSettings().setEmbedExternalFonts(true));
    xfaFlattener.flatten(fis, fos);
    fis.close();
    fos.close();
}

The encountered exception is:

Exception in thread "main" java.lang.NoSuchFieldError: FONTFAMILY
at com.itextpdf.tool.xml.xtra.xfa.font.XFAFontProvider.addFonts(XFAFontProvider.java:117)
at com.itextpdf.tool.xml.xtra.xfa.font.XFAFontProvider.<init>(XFAFontProvider.java:56)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.initFlattener(XFAFlattener.java:643)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.flatten(XFAFlattener.java:201)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.flatten(XFAFlattener.java:396)
at com.mycompany.kitext.kitext.fillData2(kitext.java:153)
at com.mycompany.kitext.kitext.main(kitext.java:81)

Also, as per @mkl's comment, I've attached the PDF forms:

https://drive.google.com/file/d/0B6w278NcMSCrZDZoZklmVTNuOWc/view?usp=sharing
//iText RUPS /AcroForm Snapshot
https://drive.google.com/file/d/0B6w278NcMSCrZ1Q1VHc5YzY4UG8/view?usp=sharing
//Form filled with fillXfaForm()
//running low on reputation

Form filled with XFA

I've also read the pdfXFA Release notes for developers. But couldn't find a similar example. Thanks for your help and the great work on iText.

mark42inbound
  • 364
  • 1
  • 4
  • 19
  • I think we're in some kind of catch 22. I think you are confused about the different types of forms that exist in PDF, and because of that confusion, your question is very confusing. A good answer to your question could take away some of the confusion, but it's very hard to provide a good answer because your question is so confusing. Why don't you take one step back. Download a tool called iText RUPS, and open the PDFs in that tool. Navigate to the Catalog, and search for the `/AcroForm` key. Post screenshots of what you see. – Bruno Lowagie Sep 25 '17 at 18:48
  • It's important to understand that there's a big difference between interactive forms based on AcroForm technology, forms using the XML Forms Architecture (XFA), and hybrid forms (in which both AcroForm and XFA exists). You should probably read [Detect dynamic XFA with iText](https://stackoverflow.com/questions/13144500/) and [Get name's field from interactive form pdf](https://stackoverflow.com/questions/28601068/) first, and then rewrite your question so that it is less confusion for people who can't read your mind. – Bruno Lowagie Sep 25 '17 at 18:59
  • Mark if you want concrete help you had better share the PDFs in question. – mkl Sep 25 '17 at 20:08

0 Answers0