Whats the alternative to copyAcroForm?

十年热恋 提交于 2019-12-29 09:28:33

问题


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


回答1:


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?



来源:https://stackoverflow.com/questions/22348436/whats-the-alternative-to-copyacroform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!