Hello,

I found a simple solution caused by XFA logic during the pdf creation via template.

The code is based on an example founded in the web site. (http://itextdocs.lowagie.com/examples/com/lowagie/examples/general/copystamp/Register.java).

If I use that code I create a PDF without errors, but when I open it I have the message "expected a dict object".

I searched a solution in the web and I found a lot of messages about this problem but no one share a snippet code to solve it.

So I decided to post here my solution.
If any of you have a better solution please post it.

thanks

mirco


----------------------------------------------------------------------------------------------------------
    private void fill(String template, String pdfName, HashMap map) {

        FileOutputStream newPdf = null;
        PdfReader reader = null;
        try {
   
            newPdf = new FileOutputStream(pdfName);
            reader = new PdfReader(template);

            // ---- REMOVING XFA logic
            removeXFA(reader);

            PdfStamper stamp = new PdfStamper(reader, newPdf);
            AcroFields af = stamp.getAcroFields();

            for (int i = 0; i < ks.length; i++) {
                Object v = map.get(ks[i]);
                boolean exitCode = af.setField((String) ks[i], (String) v);
                if (exitCode == false) {
                    LOG.warn("setField(" + ks[i] + "," + v + ") error");
                }
            }

            stamp.setFormFlattening(true);
            stamp.close();

        } catch (FileNotFoundException e) {
            // FIXME
        } catch (IOException e) {
            // FIXME
        } catch (DocumentException e) {
            // FIXME
        }
    }

    private void removeXFA(PdfReader reader) {
        if (reader == null) {
            return;
        }

        PRAcroForm form = reader.getAcroForm();
        if (form == null) {
            return;
        }
        form.remove(new PdfName("XFA"));
    }

Reply via email to