我的问题如下:
有一些巨大的 PDF 文件(>500MB),我想使用 JAVA 找到它们的页数。如果我使用 itext 或 pdfbox,我必须等到它读取整个文件,并且大多数时候都会失败,因为文件很大或者需要很长时间。
所以,我想知道是否有任何快速有效的方法来查找 PDF 文件的页数。
最佳答案
可能重复吗? Page count of Pdf with Java
从那篇文章中,Mark Storer的回答:
The itext API underwent a little overhaul. Now (in version 5.4.x) the correct way to use it is to pass through java.io.RandomAccessFile:
int efficientPDFPageCount(File file) {
RandomAccessFile raf = new RandomAccessFile(file, "r");
RandomAccessFileOrArray pdfFile = new RandomAccessFileOrArray(
new RandomAccessSourceFactory().createSource(raf));
PdfReader reader = new PdfReader(pdfFile, new byte[0]);
int pages = reader.getNumberOfPages();
reader.close();
return pages;
}
关于java - 无需阅读整个文件即可查找 PDF 页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38115808/