亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PDFBox 創建鏈接,我可以單擊以轉到同一文檔中的另一個頁面

如何使用 PDFBox 創建鏈接,我可以單擊以轉到同一文檔中的另一個頁面

MMMHUHU 2022-07-20 16:07:33
我正在嘗試使用 PDFBox 創建一個鏈接,我可以單擊以轉到同一文檔中的另一個頁面。從這個問題(How to use PDFBox to create a link that go 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,誰能給我一些關于我做錯了什么的指導?欣賞所有答案。
查看完整描述

1 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

首先,對于本地鏈接(“我可以單擊以轉到同一文檔中的另一個頁面的鏈接”),destination.setPageNumber使用方法是錯誤的,參見。它的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));

此外,您忘記為鏈接設置矩形區域,并且忘記將鏈接添加到頁面注釋中。


全部一起:


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


查看完整回答
反對 回復 2022-07-20
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號