本文介紹了如何使用PDFBox創(chuàng)建鏈接,我可以單擊該鏈接轉(zhuǎn)到同一文檔中的另一個(gè)頁(yè)面的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
我正在嘗試使用PDFBox創(chuàng)建一個(gè)鏈接,我可以單擊該鏈接轉(zhuǎn)到同一文檔中的另一個(gè)頁(yè)面。
從這個(gè)問題(How to use PDFBox to create a link that goes to *previous view*?)我知道這應(yīng)該很容易做到,但是當(dāng)我嘗試這樣做時(shí),我得到了這個(gè)錯(cuò)誤:在線程”main”java.lang.IlLegalArgumentException:GoTo操作的目標(biāo)必須是頁(yè)面字典對(duì)象
我正在使用以下代碼:
//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,有人能給我一些指導(dǎo)嗎?我哪里做錯(cuò)了?
感謝所有答案。
推薦答案
首先,對(duì)于本地鏈接(“我可以單擊以轉(zhuǎn)到同一文檔中的另一頁(yè)”),destination.setPageNumber
是錯(cuò)誤的使用方法,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));
此外,您忘記為鏈接設(shè)置矩形區(qū)域,并且忘記將鏈接添加到頁(yè)面批注。
全部:
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測(cè)試testAddLinkToMwb_I_201711
)
這篇關(guān)于如何使用PDFBox創(chuàng)建鏈接,我可以單擊該鏈接轉(zhuǎn)到同一文檔中的另一個(gè)頁(yè)面的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,