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);
添加回答
舉報