c# - iTextSharp 索引超出范围

标签 c# .net itext

不断收到 IndexOutOfRangeException 是未处理的异常。

var sb = new StringBuilder();
var bdn = String.Format("{0}\\bdn.pdf", Application.StartupPath);
var reader = new PdfReader("bdn.pdf");
var numberOfPages = reader.NumberOfPages;
for (var currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
{
        sb.Append(PdfTextExtractor.GetTextFromPage(reader, currentPageIndex));
}

最佳答案

确保您运行的 iTextSharp 版本大于 5.1,该版本有一个与您的问题完全匹配的错误:

刚刚使用 5.5.4.0(最新版本)测试,使用此代码,有效:

    StringBuilder sb = new StringBuilder();
// substitute 'pdfPath' with path to YOUR PDF
    PdfReader reader = new PdfReader(pdfPath);
    int pageNumber = 1;
    while (pageNumber <= reader.NumberOfPages) {
      sb.Append(PdfTextExtractor.GetTextFromPage(reader, pageNumber));
      ++pageNumber;
    }

关于c# - iTextSharp 索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27831518/

相关文章:

c# - MDI 子项在最大化时显示图标

c# - 将实时数据存储到 1000 个文件中

c# - 如何在不在 C# 中生成垃圾的情况下将 int 转换为 char[]

c# - 为什么服务器可能收不到附加到使用 HttpClient 的请求的证书?

java - iText:重新对齐 PDF 页面上的文本位置

c# - 显示错误页面 MVC

c# - 具有多个存储库和服务的 .NET MVC Controller ?

c# - 在 C# 中将自定义对象数组转换为 System.Array

image - 如何使用 iText 在文本下插入图像作为 pdf 背景?

c# - 图像覆盖 iTextSharp 中的表格单元格边框