gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:44:17 24 4
gpt4 key购买 nike

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

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

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

-- 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); 我会得到所有链接,它们会转到正确的页面,但它们没有按照我想要的方式嵌套/排序:

-- 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);
}
}

关于java - iText 7.0.5 : How to combine PDF and have existing bookmarks indented under new bookmarks for each document?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com