Java 确定 PDF 文件中的页数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4134949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine the number of pages in a PDF file
提问by Tony the Pony
How can I determine the number of pages in a given PDF file, using a free/open source Java API?
如何使用免费/开源 Java API 确定给定 PDF 文件中的页数?
采纳答案by dogbane
You can use Apache PDFBoxto load a PDF document and then call the getNumberOfPages
method to return the page count.
您可以使用Apache PDFBox加载 PDF 文档,然后调用该getNumberOfPages
方法返回页数。
PDDocument doc = PDDocument.load(new File("file.pdf"));
int count = doc.getNumberOfPages();
回答by HamoriZ
If you generates the PDF with FOP, then you can use http://xmlgraphics.apache.org/fop/
如果您使用 FOP 生成 PDF,则可以使用 http://xmlgraphics.apache.org/fop/
You can count the pages with the help of fop tags.
您可以在 fop 标签的帮助下计算页面数。
If it is just a simple pdf file from an external source, then you should check iText API.
如果它只是来自外部来源的简单 pdf 文件,那么您应该检查 iText API。
回答by Bozho
You should be able to do this with iText. See this threadfor how to solve the problem. Hereis chapter 2, which is incorrectly linked in the thread:
你应该可以用iText做到这一点。请参阅此线程以了解如何解决问题。这是第 2 章,它在线程中被错误链接:
PdfReader reader = new PdfReader("SimpleRegistrationForm.pdf");
int pages = reader.getNumberOfPages();
回答by Shailesh Vikram Singh
If you want to get more information about PDF, please use below code. If document does not contain any of the information, it returns null. This is pdfbox library of apache.
如果您想获得有关 PDF 的更多信息,请使用以下代码。如果文档不包含任何信息,则返回 null。这是apache的pdfbox库。
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
public class DocumentService {
public void showDocumentInfo(){
PDDocument document= PDDocument.load(new File("file.pdf"));
PDDocumentInformation info = document.getDocumentInformation();
System.out.println( "Page Count=" + document.getNumberOfPages() );
System.out.println( "Title=" + info.getTitle() );
System.out.println( "Author=" + info.getAuthor() );
System.out.println( "Subject=" + info.getSubject() );
System.out.println( "Keywords=" + info.getKeywords() );
System.out.println( "Creator=" + info.getCreator() );
System.out.println( "Producer=" + info.getProducer() );
System.out.println( "Creation Date=" + info.getCreationDate() );
System.out.println( "Modification Date=" + info.getModificationDate());
System.out.println( "Trapped=" + info.getTrapped() );
}
}