【问题标题】:Reading a particular page from a PDF document using PDFBox使用 PDFBox 从 PDF 文档中读取特定页面
【发布时间】:2011-10-13 23:08:42
【问题描述】:

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

【问题讨论】:

  • 您能否更具体地说明“阅读”的含义?
  • @Adrian:说,我想要PDPage对象中的第2页。

标签: java pdf pdfbox


【解决方案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();

【讨论】:

    【解决方案2】:

    你可以在 PDDocument 实例上使用 getPage 方法

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

    【讨论】:

      【解决方案3】:

      这应该可行:

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

      BookMark section of the tutorial中所见

      2015 年更新,2.0.0 版快照

      似乎这已被删除并放回(?)。 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 );
      

      【讨论】:

      • 这里doc的类型是什么? PDDocument 类似乎没有 getAllPages 方法。
      • @missingfaktor doc 是一个PDDocumentCatalog 对象
      • 对于那些稍后来到这里的人:pdfbox.apache.org/cookbook/textextraction.html 基本上——使用 PDFTextStripper,而不是 PDPage,因为 PDPage 似乎更多的是在屏幕上显示页面而不是获取文本stackoverflow.com/questions/13563482/…
      • 对于 pdfbox 2.0,我只使用了:pdDoc.getPage(pageNumber);其中 pdDoc 是一种 PDDocument。
      • 对于 PDFBox 1.8.10,PDDocument 类型似乎没有 getAllPages() 方法。不幸的是,该链接不再有效。
      【解决方案4】:
      //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){}
      

      【讨论】:

        【解决方案5】:

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

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

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

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

        【讨论】:

        • 个人喜好,但我发现“if (! found)”比“if (found == false)”语法更易读:)
        【解决方案6】:

        将此添加到命令行调用中:

        ExtractText -startPage 1 -endPage 1 filename.pdf
        

        把1改成你需要的页码。

        【讨论】:

        • 我必须通过一个程序来完成。
        猜你喜欢
        • 2012-11-13
        • 2013-03-28
        • 2014-04-25
        • 2021-01-29
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-26
        • 2013-07-15
        相关资源
        最近更新 更多