合并pdf并在java中添加iText书签 [英] Merge pdfs and add bookmark with iText in java

查看:655
本文介绍了合并pdf并在java中添加iText书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用iText将书签添加到现有PDF?

我将多个PDF合并为一个PDF并且我需要为最终PDF构建书签。例如,我有三个PDF:doc1.pdf,doc2.pdf和doc3.pdf,doc1和doc2属于Group1,doc3属于Group2。我需要合并它们,并且必须为结果PDF构建嵌套书签,如下所示:

I am merging multiple PDFs together into one PDF and I need to build bookmarks for the final PDF. For example, I have three PDFs: doc1.pdf, doc2.pdf and doc3.pdf, doc1 and doc2 belong to Group1, doc3 belongs to Group2. I need to merge them and have to build nested bookmarks for the resulting PDFs like so:

Group1 
   doc1  
   doc2  
Group2 
   doc3 

等。

推荐答案

我做了一个 MergeWithOutlines 使用 PdfCopy 连接三个现有PDF的示例(我假设您已经知道该部分)。

I've made a MergeWithOutlines example that concatenates three existing PDFs using PdfCopy (I assume that you already know that part).

在这样做时,我创建一个轮廓对象,如下所示:

While doing so, I create an outlines object like this:

ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String, Object>>();

我将元素添加到此轮廓对象:

and I add elements to this outlines object:

HashMap<String, Object> helloworld = new HashMap<String, Object>();
helloworld.put("Title", "Hello World");
helloworld.put("Action", "GoTo");
helloworld.put("Page", String.format("%d Fit", page));
outlines.add(helloworld);

当我想要一些层次结构时,我会介绍 kids

When I want some hierachy, I introduce kids:

ArrayList<HashMap<String, Object>> kids = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> link1 = new HashMap<String, Object>();
link1.put("Title", "link1");
link1.put("Action", "GoTo");
link1.put("Page", String.format("%d Fit", page));
kids.add(link1);
helloworld.put("Kids", kids);

如果您想要一个没有链接的条目,请删除放置的行操作页面

If you want an entry without a link, remove the lines that put an Action and a Page.

完成后,添加轮廓到复制对象:

Once you're finished, add the outlines to the copy object:

copy.setOutlines(outlines);

查看生成的PDF ,您将在书签面板中看到大纲。

Look at the resulting PDF and you'll see the outlines in the bookmarks panel.

这篇关于合并pdf并在java中添加iText书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆