0

I use OpenOffice to create odt file,create form fields in this file, and save odf file as PDF file. Then use itext to fill data into pdf files.Every form field showing in each pdf. But when I merger pdf files, all form fields missing. Please help.

Code of set fields:

            FileOutputStream fos = new FileOutputStream(outpath);
            PdfReader reader= new PdfReader(templateinputsream);
            PdfStamper stamp = new PdfStamper(reader, fos);
            AcroFields form = stamp.getAcroFields();
            setField(form, fieldsMap);

private void setField(AcroFields form, Map<String, String> map) {
    Iterator<Entry<String, String>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, String> entry = it.next();
        String key = entry.getKey();
        String value = entry.getValue();
        try {
            form.setField(key, value);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

Code of merge pdf:

private void mergePdf(List<String> files, String savepath) {
    try {
        List<InputStream> pdfs = new ArrayList<InputStream>();
        for (int i = 0; i < files.size(); i++) {
            pdfs.add(new FileInputStream(files.get(i)));
        }
        OutputStream output = new FileOutputStream(savepath);
        Document document = new Document(new PdfReader(files.get(0)).getPageSize(1));
        concatPDFs(document, pdfs, output);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void concatPDFs(Document document, List<InputStream> streamOfPDFFiles, OutputStream outputStream) {

    try {
        List<InputStream> pdfs = streamOfPDFFiles;
        List<PdfReader> readers = new ArrayList<PdfReader>();
        int totalPages = 0;
        Iterator<InputStream> iteratorPDFs = pdfs.iterator();
        while (iteratorPDFs.hasNext()) {
            InputStream pdf = iteratorPDFs.next();
            PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            totalPages += pdfReader.getNumberOfPages();
        }
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
        PdfImportedPage page;
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();
        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (document.isOpen())
            document.close();
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
Tom
  • 2,857
  • 9
  • 46
  • 59
  • You chose to use code to merge which explicitly throws away all interactive content. You'll find an explanation and alternatives [here](http://stackoverflow.com/a/15945467/1729265). – mkl Apr 26 '14 at 20:11

1 Answers1

2

I see three possible problems:

[1.]

There is a known problem with forms generated using OpenOffice: appearances are missing! (Actually, somebody should tell the OpenOffice developers to fix this). You can solve this by adding the following line:

fields.setGenerateAppearances(true);

See FillDataSheet for the full example.

[2.]

You are making the same mistake as described in "Duplicate questions" versus "RTFM"

As documented, using PdfWriter throws away all interactive features. As you don't flatten your document, the fields remain interactive, so by chosing PdfWriter instead of PdfCopy, you deliberately throw away the fields...

[3.]

When you use PdfCopy, make sure you use the latest version of iText, because concatenating of forms has been implemented only recently. See https://stackoverflow.com/a/20659958/1622493 to find an almost exact copy of your question.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • When merging pages, It seems all values of one fields are same. Looks like, one field name in different page of the merged file can only have one value. For example, I have a field name PageNumber, in different it should be different.But now all value remain same. – Tom Apr 28 '14 at 03:22
  • If you read the PDF reference (ISO-32000-1), you discover that one field with one unambiguous name can only have one unambiguous value. Are you complaining that the reference is being respected? Please read the documentation? If you want the fields to remain interactive, you *rename* the fields (as explained in my book); if they don't need to be interactive, flatten the fields before merging the document. In any case: please read the documentation... – Bruno Lowagie Apr 28 '14 at 06:10
  • Afte flattening,every fields missing. Already added this line: fields.setGenerateAppearances(true); – Tom Apr 28 '14 at 11:44
  • 1
    We can't reproduce that problem. See http://itextpdf.com/examples/iia.php?id=122 where we take a form created with OpenOffice http://examples.itextpdf.com/resources/pdfs/datasheet.pdf and fill it out with this as result: http://examples.itextpdf.com/results/part2/chapter06/imdb0413300.pdf – Bruno Lowagie Apr 28 '14 at 12:53