我正在使用iTextSharp做一些工作,它必须读取100到20万页pdfs,有时创建pdfReader需要10分钟以上的时间!我一直在寻找一种方法,一次只读某些页面,这样它就不会一次存储全部pdf,但却找不到任何东西。有人知道这在iTextSharp中是否可行吗?
发布于 2015-09-08 08:43:25
PDF格式允许您只阅读您感兴趣的部分,您不需要阅读所有的文件来查找特定的信息。
如果iText(Sharp) PdfReader是在部分模式,cf中初始化的,那么它可以选择地支持它。主构造函数所有其他构造函数都依赖于:
/**
* 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的值。允许这样做的公共建设者是:
public PdfReader(String filename, byte[] ownerPassword, bool partial)和
[Obsolete("Use the constructor that takes a RandomAccessFileOrArray")]
public PdfReader(RandomAccessFileOrArray raf, byte[] ownerPassword)(后者总是使用部分模式)。
因此,如果您从文件系统中打开一个PDF,使用前一个构造函数和partial = true,然后创建一个适当的RandomAccessFileOrArray实例并使用后者。如果不需要密码,请设置ownerPassword = null。
或者,一些内省/反思魔法可以让您直接使用主构造函数。
顺便说一句,后一个构造函数是@ChrisHaas在他的评论中指出的。不幸的是,它已被宣布为废弃(又称过时)。
Ceterum的重要功能(如部分模式)应易于使用。
https://stackoverflow.com/questions/32401111
复制相似问题