使用PDFBox从PDF文档中读取特定页面

如何使用PDFBox从PDF文档中读取特定页面(给定页码)?

这应该工作:

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

如本教程的BookMark部分所示

更新2015,版本2.0.0 SNAPSHOT

似乎这被删除并放回(?)。 getPage在2.0.0 javadoc中 。 要使用它:

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

getAllPages方法已重命名为getPages

 PDPage page = (PDPage)doc.getPages().get( 0 ); 
 //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){} 

以为我会在这里添加我的答案,因为我发现上述答案有用,但不完全是我需要的。

在我的场景中,我想单独扫描每个页面,查找关键字,如果出现该关键字,则对该页面执行某些操作(即复制或忽略它)。

在我的回答中,我试图简单地替换常见变量等:

 public void extractImages() throws Exception { try { String destinationDir = "OUTPUT DIR GOES HERE"; // Load the pdf String inputPdf = "INPUT PDF DIR GOES HERE"; document = PDDocument.load( inputPdf); List list = document.getDocumentCatalog().getAllPages(); // Declare output fileName String fileName = "output.pdf"; // Create output file PDDocument newDocument = new PDDocument(); // Create PDFTextStripper - used for searching the page string PDFTextStripper textStripper=new PDFTextStripper(); // Declare "pages" and "found" variable String pages= null; boolean found = false; // Loop through each page and search for "SEARCH STRING". If this doesn't exist // ie is the image page, then copy into the new output.pdf. for(int i = 0; i < list.size(); i++) { // Set textStripper to search one page at a time textStripper.setStartPage(i); textStripper.setEndPage(i); PDPage returnPage = null; // Fetch page text and insert into "pages" string pages = textStripper.getText(document); found = pages.contains("SEARCH STRING"); if (i != 0) { // if nothing is found, then copy the page across to new output pdf file if (found == false) { returnPage = list.get(i - 1); System.out.println("page returned is: " + returnPage); System.out.println("Copy page"); newDocument.importPage(returnPage); } } } newDocument.save(destinationDir + fileName); System.out.println(fileName + " saved"); } catch (Exception e) { e.printStackTrace(); System.out.println("catch extract image"); } } 

将其添加到命令行调用:

 ExtractText -startPage 1 -endPage 1 filename.pdf 

将1更改为您需要的页码。

你可以通过PDDocument实例获取getPage方法

 PDDocument pdDocument=null; pdDocument = PDDocument.load(inputStream); PDPage pdPage = pdDocument.getPage(0); 

这是解决方案。 希望它能解决你的问题。

 string fileName="C:\mypdf.pdf"; PDDocument doc = PDDocument.load(fileName); PDFTextStripper stripper = new PDFTextStripper(); stripper.setStartPage(1); stripper.setEndPage(2); //above page number 1 to 2 will be parsed for only one page set both value same (ex:setStartPage(1); setEndPage(1);) string reslut = stripper.getText(doc); doc.close();