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

时间:2011-07-27 05:25:06

标签: java pdf pdfbox

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

6 个答案:

答案 0 :(得分:25)

这应该有效:

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

BookMark section of the tutorial

更新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 );

答案 1 :(得分:18)

//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){}

答案 2 :(得分:4)

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

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

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

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<PDPage> 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");
         }
    }

答案 3 :(得分:1)

将此添加到命令行调用:

ExtractText -startPage 1 -endPage 1 filename.pdf

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

答案 4 :(得分:1)

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

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

答案 5 :(得分:1)

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

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 parsing only one page set both value same (ex:setStartPage(1);  setEndPage(1);)
string reslut = stripper.getText(doc);

doc.close();