如何使用pdf框中的书签选择pdf页面? [英] How to select pdf page using bookmark in pdf box?

查看:146
本文介绍了如何使用pdf框中的书签选择pdf页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我是PDF框的新手,正在寻找一种解决方案,该方案如何使用书签名称获取特定的pdf页面?像下面的代码片段一样,我试图循环所有页面,但是卡住了将书签与我需要的页面链接.谁能帮忙吗?

Sorry i am new to PDF box and was looking for a solution on how to get a specific pdf page using the bookmark name? Like the below code snippet am trying to loop all the pages but stuck to link the book mark with the page i need. Can any one please help?

import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSObject;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;

public class PDFLoader {

    public static void main(String[] args) throws InvalidPasswordException, IOException, PrinterException {


          File file = new File("d:\\pdf\\sample.pdf"); 
          PDDocument document = PDDocument.load(file); 
          PDPage page01 = document.getDocumentCatalog().getPages().get(0);
          PDPage page02 = document.getDocumentCatalog().getPages().get(1);

          PDDocumentOutline outline =  document.getDocumentCatalog().getDocumentOutline();
          printBookmark(outline, "");
          PDDocument doc = new PDDocument();
          doc.addPage(page01);
          doc.addPage(page02);
          doc.save("d:\\pdf\\newSample.pdf");
          doc.close();

    }



public static PDPage getBookmark(PDOutlineNode bookmark, String indentation) throws IOException
    {
        PDOutlineItem current = bookmark.getFirstChild();
        while (current != null)

        {
            System.out.println(indentation + current.getTitle());



            if (current.getAction() instanceof PDActionGoTo)
            {
                PDActionGoTo gta = (PDActionGoTo) current.getAction();
                if (gta.getDestination() instanceof PDPageDestination)
                {

                    if(current.getTitle().equals("MyBookMark")){


                        PDPageDestination pd = (PDPageDestination) current.getDestination();
                        System.out.println("Destination page: " + pd.retrievePageNumber());
                        return pd.getPage();

                        }
                }
            }

            getBookmark(current, indentation + "    ");
            current = current.getNextSibling();

            }


        return null;
    }







//Stack Trace
    Exception in thread "main" java.lang.NullPointerException
    at com.mypackage.PDFLoader.getBookmark(PDFLoader.java:67)
    at com.mypackage.PDFLoader.main(PDFLoader.java:40)

推荐答案

事实证明,在PDF中,页面目标不在书签的目标项中,而是在书签的操作项中(是的,PDF使得具有两种做同一件事的方式).将此添加到您的代码中:

It turns out that in your PDF the page destination is not in the bookmark's destination entry, but in the bookmark's action entry (yes, PDF makes it possible to have two ways to do the same thing). Add this to your code:

if (current.getDestination() instanceof PDPageDestination)
{
    PDPageDestination pd = (PDPageDestination) current.getDestination();
    System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
    return pd.getPage();
}
if (current.getAction() instanceof PDActionGoTo)
{
    PDActionGoTo gta = (PDActionGoTo) current.getAction();
    if (gta.getDestination() instanceof PDPageDestination)
    {
        PDPageDestination pd = (PDPageDestination) gta.getDestination();
        System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
        return pd.getPage();
    }
}

这篇关于如何使用pdf框中的书签选择pdf页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆