如何使用 PDFBox 创建 link 我可以单击以转到同一文档中的另一页
How to use PDFBox to create a link i can click to go to another page in the same document
我正在尝试使用 PDFBox 创建一个 link 我可以单击以转到同一文档中的另一页。
从这个问题 () 我看到这应该很容易做到,但是当我尝试这样做时我得到这个错误:线程异常 "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,任何人都可以就我做错了什么给我一些指导吗?
感谢所有回答。
首先,对于本地link("a link i can click to go to another page in the same document")来说,destination.setPageNumber
是错误的使用方法,cf.它的 JavaDocs:
/**
* 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));
此外,您忘记为 link 设置矩形区域并且忘记将 link 添加到页面注释中。
总计:
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 创建一个 link 我可以单击以转到同一文档中的另一页。
从这个问题 (
我正在使用此代码:
//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,任何人都可以就我做错了什么给我一些指导吗?
感谢所有回答。
首先,对于本地link("a link i can click to go to another page in the same document")来说,destination.setPageNumber
是错误的使用方法,cf.它的 JavaDocs:
/**
* 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));
此外,您忘记为 link 设置矩形区域并且忘记将 link 添加到页面注释中。
总计:
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
)