我使用pdfbox 2.0,我在文件pdf中找到内部的所有链接。但是我不知道如何找到要跳转的页面?我在第20页有一个内部链接“第1章”,这是我使用jquery cookbook.pdf
的文件public static void main(String[] args) throws IOException {
PDDocument doc = null;
doc = PDDocument.load(new File("D:\\demoConverter\\input\\jQuery_Cookbook.pdf"));
int pageNum = 0;
doc.getNumberOfPages();
for (PDPage page : doc.getPages()) {
pageNum++;
List<PDAnnotation> annotations = page.getAnnotations();
for (PDAnnotation annot : annotations) {
if (annot instanceof PDAnnotationLink) {
PDAnnotationLink link = (PDAnnotationLink) annot;
PDAction action = link.getAction();
if (action instanceof PDActionURI) {
PDActionURI uri = (PDActionURI) action;
}
// internal link
if (action instanceof PDActionGoTo) {
PDDestination destination = ((PDActionGoTo)action).getDestination();
// how to get page will to jump to
}
}
}
}
答案 0 :(得分:4)
此类目的地可以是页面目的地或命名目的地。页面目标很简单,命名目标需要在名称表中查找。
if (action instanceof PDActionGoTo)
{
PDPageDestination pageDestination = null;
PDPage page = null;
PDDestination destination = ((PDActionGoTo)action).getDestination();
if (destination instanceof PDPageDestination)
{
pageDestination = (PDPageDestination) destination;
page = pageDestination.getPage();
}
else if (destination instanceof PDNamedDestination)
{
pageDestination = doc.getDocumentCatalog().findNamedDestinationPage((PDNamedDestination) destination);
if (pageDestination == null)
{
return null;
}
page = pageDestination.getPage();
}
else
{
// error handling
}
}