How do I read a particular page (given a page number) from a PDF document using PDFBox?
问问题
58670 次
6 回答
31
这应该有效:
PDPage firstPage = (PDPage)doc.getAllPages().get( 0 );
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 );
于 2011-07-27T05:33:33.447 回答
20
//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){}
于 2012-07-13T20:19:49.647 回答
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");
}
}
于 2014-01-28T09:13:49.897 回答
1
Add this to the command-line call:
ExtractText -startPage 1 -endPage 1 filename.pdf
Change 1 to the page number that you need.
于 2011-07-27T05:29:45.133 回答
1
你可以在 PDDocument 实例上使用 getPage 方法吗
PDDocument pdDocument=null;
pdDocument = PDDocument.load(inputStream);
PDPage pdPage = pdDocument.getPage(0);
于 2015-11-14T06:31:51.053 回答
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();
于 2017-07-01T09:02:16.157 回答