【问题标题】:Itextsharp pdf reader of only certain pages in pdfItextsharp pdf阅读器仅在pdf中的某些页面
【发布时间】:2015-09-04 14:58:16
【问题描述】:

我正在使用 iTextSharp 做一些工作,它必须阅读 100 到 200,000 页 pdf,而且有时可能需要 10 分钟以上才能创建 pdfReader!我一直在寻找一种方法,一次只能阅读某些页面,这样它就不会一次存储整个 pdf,但找不到任何东西。有谁知道这在 iTextSharp 中是否可行?

【问题讨论】:

  • 你能发布你的代码吗?也许您没有正确处置您的实例。
  • 这只是一个实例。初始 pdfReader 需要永远加载。加载后一切正常,但我等不及要创建 pdf 阅读器了。这就是为什么我试图找到一种方法来创建一个每次只有一个页面的 pdf 阅读器,而不是一次存储它
  • 你看到这个thread了吗?

标签: c# .net itextsharp


【解决方案1】:

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 重要的功能(如部分模式)应该易于使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    相关资源
    最近更新 更多