2

We are currently porting our code base from iText 2.1.7 to iText 5.5.0 (yeah I know.. we had a little longer ;-). Well.. "now" that copyAcroForm has gone the way of the Dodo, I'm struggling to find an alternative to this code:

  File outputFile = new File...
  Document document = new Document();
  FileOutputStream fos = new FileOutputStream(outputFile);
  PdfCopy subjobWriter = new PdfCopy(document, fos);
  document.open();
  PdfReader reader = new PdfReader(generationReader);
  for (int i=1; i<=reader.getNumberOfPages(); i++) {
    PdfImportedPage page = subjobWriter.getImportedPage(reader, i);
    subjobWriter.addPage(page);
  }
  PRAcroForm form = reader.getAcroForm();
  if (form != null)
    subjobWriter.copyAcroForm(reader);
  subjobWriter.freeReader(reader);
  reader.close();
  subjobWriter.close();
  document.close();
  fos.close();

but haven't really found anything. I read in the changelog of 4.34 or so that I apparently should use PdfCopy.addDocument(). I tried that and commented out the other code, such as this:

  ...
  PdfReader reader = new PdfReader(generationReader);
  reader.consolidateNamedDestinations();
  subjobWriter.addDocument(reader);
  subjobWriter.freeReader(reader);
  subjobWriter.setOutlines(SimpleBookmark.getBookmark(reader));
  ...

but that didn't help either.

The problem is, that everything from the original PDF is copied EXCEPT the form (and its fields and content), or rather, it looks like the whole form has been flattened instead.

Since all the samples I could find either used copyAcroForm() which doesn't exist anymore or the PdfCopyFields class which is deprecated and all the samples at itextpdf.com and the "iText in Action, 2nd edition" use copyAcroForm() as well, I'm at loss as to how to solve this. Any idea anyone?

Rog

Fuggly
  • 905
  • 1
  • 8
  • 18

1 Answers1

6

Please take a look at the MergeForms example:

Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
copy.setMergeFields();
document.open();
for (PdfReader reader : readers) {
    copy.addDocument(reader);
}
document.close();
for (PdfReader reader : readers) {
    reader.close();
}

One line in particular is very important:

copy.setMergeFields();

Did you add that line?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Ah thanks Bruno.. yes I added that at one time, but apparently at the wrong position. Your sample works and is appreciated, thanks a lot! Is there maybe a 3rd edition planned of iText in Action (I'd be a happy purchaser :-)? – Fuggly Mar 12 '14 at 10:36
  • Yes and no. I'm writing a series of books, but not for a publisher. They will be available for free on LeanPub: https://leanpub.com/u/itextsoftware I've started with a book about low-level PDF, but as you can see, I'm already collecting examples for books about high-level PDF creation and manipulation. This video tutorial already gives you an impression about what you can expect: http://itextpdf.com/codenvy_webapp – Bruno Lowagie Mar 12 '14 at 10:53
  • Neat! I will have a look at them... thanks for helping me. One more, last, quick question: I see that XmpWriter.addRdfDescription() is deprecated. What is the alternative to that? Is there a page to look at what alternatives to deprecated methods would be? Or can I make a suggestion and add a Javadoc description (if possible) that links to alterntives or, if its a small change, shows the changes in the JavaDoc already? – Fuggly Mar 12 '14 at 12:55
  • The old XMP functionality is replaced by a complete package (com.textpdf.xmp) and apart from the tests in the test suite (follow the Maven structure and you'll find the tests), there aren't any examples yet. – Bruno Lowagie Mar 12 '14 at 14:36
  • I want to use this answer to close another duplicate question, but it seems that, although my answer is correct, it has not been accepted nor up-voted. Hence I can't use it to close another question as duplicate. Can you please accept the answer? – Bruno Lowagie Sep 13 '14 at 10:43
  • sorry, of course.. I forgot accept your solution, it is now done – Fuggly Sep 16 '14 at 08:59