Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
948 views
in Technique[技术] by (71.8m points)

itext - Whats the alternative to copyAcroForm?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...