iText 7.0.5:如何合并 PDF 并在每个文档的新书签下缩进现有书签?

iText 7.0.5: How to combine PDF and have existing bookmarks indented under new bookmarks for each document?

问题: com.itextpdf.kernel.PdfException: Pdf间接对象属于其他PDF文档。将对象复制到当前 pdf 文档。

我想将 PDF 文档与一组经过编辑的书签组合在一起,使书签与每个原始文档保持清晰的配对。我还想要一个新的顶级书签来描述整个集合,以便在以后用户选择时改进与其他文档的结合。合并的文档数量和每个文档中的书签数量未知,有些文档可能没有任何书签。

为简单起见,假设我有两个文档,每个文档都有两页,每个文档的第二页都有一个书签。我希望合并的文档具有这样的书签结构,其中 "NEW" 是我根据每个源文档的元数据创建的,"EXISTING" 是我从各个文档复制的内容:

-- NEW Combined Document meta(page 1)
---- NEW Document one meta   (page 1)
------ EXISTING Doc one link (page 2)
---- NEW Document two meta   (page 3)
------ EXISTING Doc two link (page 4)

代码:

private static String combinePdf(List<String> allFile, LinkedHashMap<String, String> bookmarkMetaMap, Connection conn) throws IOException {
System.out.println("=== combinePdf()  ENTER");  // TODO REMOVE
File outFile = File.createTempFile("combinePdf", "pdf", new File(DocumentObj.TEMP_DIR_ON_SERVER));
if (!outFile.exists() || !outFile.canWrite()) {
  throw new IOException("Unable to create writeable file in " + DocumentObj.TEMP_DIR_ON_SERVER);
}
if (bookmarkMetaMap == null || bookmarkMetaMap.isEmpty()) {
  bookmarkMetaMap = new LinkedHashMap<>(); // prevent NullPointer below
  bookmarkMetaMap.put("Documents", "Documents");
}
try ( PdfDocument allPdfDoc = new PdfDocument(new PdfWriter(outFile)) ) {
  allPdfDoc.initializeOutlines();
  allPdfDoc.getCatalog().setPageMode(PdfName.UseOutlines);
  PdfMerger allPdfMerger = new PdfMerger(allPdfDoc, true, false); // build own outline
  Iterator<Map.Entry<String, String>> itr = bookmarkMetaMap.entrySet().iterator();
  PdfOutline rootOutline = allPdfDoc.getOutlines(false);
  PdfOutline mainOutline;
  mainOutline = rootOutline.addOutline(itr.next().getValue());
  mainOutline.addDestination(PdfExplicitDestination.createFit(allPdfDoc.getNumberOfPages() + 1));
  int fileNum = 0;
  for (String oneFile : allFile) {
    PdfDocument onePdfDoc = new PdfDocument(new PdfReader(oneFile));
    PdfAcroForm oneForm = PdfAcroForm.getAcroForm(onePdfDoc, false);
    if (oneForm != null) {
      oneForm.flattenFields();
    }
    allPdfMerger.merge(onePdfDoc, 1, onePdfDoc.getNumberOfPages());
    fileNum++;
    String bookmarkLabel = itr.hasNext() ? itr.next().getKey() : "Document " + fileNum;
    PdfOutline linkToDoc = mainOutline.addOutline(bookmarkLabel);
    linkToDoc.addDestination(PdfExplicitDestination.createFit(allPdfDoc.getNumberOfPages() + 1));
    PdfOutline srcDocOutline = onePdfDoc.getOutlines(false);
    if (srcDocOutline != null) {
      List<PdfOutline> outlineList = srcDocOutline.getAllChildren();
      if (!outlineList.isEmpty()) {
        for (PdfOutline p : outlineList) {
          linkToDoc.addOutline(p);    // if I comment this out, no error, but links wrong order
        }
      }
    }
    onePdfDoc.close();
  }
  System.out.println("=== combinePdf()  DONE ADDING PAGES ===");    //TODO REMOVE
}
return outFile.getAbsolutePath();
}

问题: com.itextpdf.kernel.PdfException: Pdf间接对象属于其他PDF文档。将对象复制到当前 pdf 文档。

在调试行“=== combinePdf() DONE ADDING PAGES ===”之后发生错误,因此 for 循环按预期完成。 这意味着当 allPdfDoc 自动关闭时会发生错误。 如果我删除行 linkToDoc.addOutline(p); 我得到我所有的链接并且它们转到正确的页面但它们不是我想要的 nested/ordered:

-- NEW Combined Document meta(page 1)
---- NEW Document one meta   (page 1)
---- NEW Document two meta   (page 3)
-- EXISTING Doc one link   (page 2)
-- EXISTING Doc two link   (page 4)

注释掉上述行后,我什至不确定如何包含现有链接。我在 PdfMerger 构造函数中将 mergeOutlines 标志设置为 false,因为我认为我必须构建自己的大纲。无论我将 getOutlines() 设置为 true 还是 false,以及是否取出任意顶级新书签,我都会得到类似的结果。

我知道如何按所需顺序创建新书签和现有书签的扁平化列表。所以我的问题是关于如何根据需要获得缩进和排序。

感谢观看!

我没有在合并的 PDF 中移动书签,而是在合并之前在组件 PDF 中进行了移动。 欢迎反馈,尤其是当 PDF 大小增加时效率极低时:

private static void shiftPdfBookmarksUnderNewBookmark(PdfDocument pdfDocument, String bookmarkLabel) {
if (pdfDocument == null || pdfDocument.getWriter() == null) {
  log.warn("shiftPdfBookmarksUnderNewBookmark(): no writer linked to PDFDocument, cannot modify bookmarks");
  return;
}
pdfDocument.initializeOutlines();
try {
  PdfOutline rootOutline = pdfDocument.getOutlines(false);
  PdfOutline subOutline = rootOutline.addOutline(bookmarkLabel);
  subOutline.addDestination(PdfExplicitDestination.createFit(pdfDocument.getFirstPage())); // Not sure why this is needed, but problems if omitted.
  List<PdfOutline> pdfOutlineChildren = rootOutline.getAllChildren();
  if (pdfOutlineChildren.size() == 1) {
    return;
  }
  int i = 0;
  for (PdfOutline p : rootOutline.getAllChildren()) {
    if (p != subOutline) {
      if (p.getDestination() == null) {
        continue;
      }
      subOutline.addOutline(p);
    }
  }
  rootOutline.getAllChildren().clear();
  rootOutline.addOutline(subOutline);
  subOutline.addDestination(PdfExplicitDestination.createFit(pdfDocument.getFirstPage())); // not sure why duplicate line above seems to be needed
}
catch (Exception logAndIgnore) {
  log.warn("shiftPdfBookmarksUnderNewBookmark ignoring error and not shifting bookmarks: " +logAndIgnore, logAndIgnore);
}
}