How to use PDFBox to create a link i can click to go to another page in the same document(如何使用PDFBox创建链接,我可以单击该链接转到同一文档中的另一个页面)
问题描述
我正在尝试使用PDFBox创建一个链接,我可以单击该链接转到同一文档中的另一个页面。
从这个问题(How to use PDFBox to create a link that goes to *previous view*?)我知道这应该很容易做到,但是当我尝试这样做时,我得到了这个错误:在线程"main"java.lang.IlLegalArgumentException:GoTo操作的目标必须是页面字典对象
我正在使用以下代码:
//Loading an existing document consisting of 3 empty pages.
File file = new File("C:\Users\Student\Documents\MyPDF\Test_doc.pdf");
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(1);
PDAnnotationLink link = new PDAnnotationLink();
PDPageDestination destination = new PDPageFitWidthDestination();
PDActionGoTo action = new PDActionGoTo();
destination.setPageNumber(2);
action.setDestination(destination);
link.setAction(action);
link.setPage(page);
我正在使用PDFBox 2.0.13,有人能给我一些指导吗?我哪里做错了?
感谢所有答案。
推荐答案
首先,对于本地链接("我可以单击以转到同一文档中的另一页"),destination.setPageNumber
是错误的使用方法,cf。其Java文档:
/**
* Set the page number for a remote destination. For an internal destination, call
* {@link #setPage(PDPage) setPage(PDPage page)}.
*
* @param pageNumber The page for a remote destination.
*/
public void setPageNumber( int pageNumber )
因此,替换
destination.setPageNumber(2);
由
destination.setPage(document.getPage(2));
此外,您忘记为链接设置矩形区域,并且忘记将链接添加到页面批注。
全部:
PDPage page = document.getPage(1);
PDAnnotationLink link = new PDAnnotationLink();
PDPageDestination destination = new PDPageFitWidthDestination();
PDActionGoTo action = new PDActionGoTo();
destination.setPage(document.getPage(2));
action.setDestination(destination);
link.setAction(action);
link.setPage(page);
link.setRectangle(page.getMediaBox());
page.getAnnotations().add(link);
(AddLink测试testAddLinkToMwb_I_201711
)
这篇关于如何使用PDFBox创建链接,我可以单击该链接转到同一文档中的另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用PDFBox创建链接,我可以单击该链接转到同一文档中的另一个页面


基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01