java - 无需阅读整个文件即可查找 PDF 页数

标签 java pdf

我的问题如下:

有一些巨大的 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/

相关文章:

java - 如何在 Windows 环境中写入安装在 'C:\Program Files' 中的 java 桌面应用程序的嵌入式 derby 数据库?

python - 使用 pdfminer.6 从每个 PDF 页面中提取文本

pdf - 替换 PDF 中的字体

java - 在 JComponent 上创建阴影的颜色不同于白色的问题

java - 准备语句 : create method using an ArrayList of parameters

java - weblogic.xml 文件中的乐观序列化 - ClassCastException

android - 哎呀!有一个 ...previewing this pdf document android

c# - 从 C# 客户端在 Solr 中索引 pdf 文档

python - 将多个 PDF 合并为一个 PDF

java - 无法找到规范上下文路径与用户代理提供的 URI 之间的匹配项