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