PDF 格式允许您将自己限制为只阅读您感兴趣的部分,您不必阅读所有文件来查找特定信息。
如果 iText(Sharp) PdfReader 在 部分模式 下初始化,则可选择支持此功能,参见。 主构造函数所有其他构造函数依赖:
/**
* 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 censeo 重要的功能(如部分模式)应该易于使用。