Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a particular page from a PDF document using PDFBox

Tags:

java

pdf

pdfbox

How do I read a particular page (given a page number) from a PDF document using PDFBox?

like image 259
missingfaktor Avatar asked Jul 27 '11 05:07

missingfaktor


People also ask

How do you add a page to a PDFBox?

You can add a page to the PDF document using the addPage() method of the PDDocument class. To this method you need to pass the PDPage object as a parameter. Therefore, add the blank page created in the previous step to the PDDocument object as shown in the following code block. document.

What is PDFBox used for?

Apache PDFBox is an open source Java library that can be used to create, render, print, split, merge, alter, verify and extract text and meta-data of PDF files.


2 Answers

This should work:

PDPage firstPage = (PDPage)doc.getAllPages().get( 0 );

as seen in the BookMark section of the tutorial

Update 2015, Version 2.0.0 SNAPSHOT

Seems this was removed and put back (?). getPage is in the 2.0.0 javadoc. To use it:

PDDocument document = PDDocument.load(new File(filename));
PDPage doc = document.getPage(0);

The getAllPages method has been renamed getPages

PDPage page = (PDPage)doc.getPages().get( 0 );
like image 145
Nicolas Modrzyk Avatar answered Oct 24 '22 07:10

Nicolas Modrzyk


//Using PDFBox library available from http://pdfbox.apache.org/  
//Writes pdf document of specific pages as a new pdf file

//Reads in pdf document  
PDDocument pdDoc = PDDocument.load(file);

//Creates a new pdf document  
PDDocument document = null;

//Adds specific page "i" where "i" is the page number and then saves the new pdf document   
try {   
    document = new PDDocument();   
    document.addPage((PDPage) pdDoc.getDocumentCatalog().getAllPages().get(i));   
    document.save("file path"+"new document title"+".pdf");  
    document.close();  
}catch(Exception e){}
like image 44
Raymond C Borges Hink Avatar answered Oct 24 '22 07:10

Raymond C Borges Hink