首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Itextsharp pdf阅读器只阅读某些页的pdf

Itextsharp pdf阅读器只阅读某些页的pdf
EN

Stack Overflow用户
提问于 2015-09-04 14:58:16
回答 1查看 857关注 0票数 1

我正在使用iTextSharp做一些工作,它必须读取100到20万页pdfs,有时创建pdfReader需要10分钟以上的时间!我一直在寻找一种方法,一次只读某些页面,这样它就不会一次存储全部pdf,但却找不到任何东西。有人知道这在iTextSharp中是否可行吗?

EN

回答 1

Stack Overflow用户

发布于 2015-09-08 08:43:25

PDF格式允许您只阅读您感兴趣的部分,您不需要阅读所有的文件来查找特定的信息。

如果iText(Sharp) PdfReader是在部分模式,cf中初始化的,那么它可以选择地支持它。主构造函数所有其他构造函数都依赖于:

代码语言:javascript
运行
复制
/**
 * Constructs a new PdfReader.  This is the master constructor.
 * @param byteSource source of bytes for the reader
 * @param partialRead if true, the reader is opened in partial mode (PDF is parsed on demand), if false, the entire PDF is parsed into memory as the reader opens
 * @param ownerPassword the password or null if no password is required
 * @param certificate the certificate or null if no certificate is required
 * @param certificateKey the key or null if no certificate key is required
 * @param certificateKeyProvider the name of the key provider, or null if no key is required
 * @param closeSourceOnConstructorError if true, the byteSource will be closed if there is an error during construction of this reader
 */
private PdfReader(IRandomAccessSource byteSource, bool partialRead, byte[] ownerPassword, X509Certificate certificate, ICipherParameters certificateKey, bool closeSourceOnConstructorError)

不幸的是,这个主构造函数是私有的。因此,我们必须寻找构造函数,允许我们使用true作为bool partialRead的值。允许这样做的公共建设者是:

代码语言:javascript
运行
复制
public PdfReader(String filename, byte[] ownerPassword, bool partial)

代码语言:javascript
运行
复制
[Obsolete("Use the constructor that takes a RandomAccessFileOrArray")]
public PdfReader(RandomAccessFileOrArray raf, byte[] ownerPassword)

(后者总是使用部分模式)。

因此,如果您从文件系统中打开一个PDF,使用前一个构造函数和partial = true,然后创建一个适当的RandomAccessFileOrArray实例并使用后者。如果不需要密码,请设置ownerPassword = null

或者,一些内省/反思魔法可以让您直接使用主构造函数。

顺便说一句,后一个构造函数是@ChrisHaas在他的评论中指出的。不幸的是,它已被宣布为废弃(又称过时)。

Ceterum的重要功能(如部分模式)应易于使用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32401111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档