编程语言
首页 > 编程语言> > 首页> C#> iTextSharp的IndexOutOfRange

首页> C#> iTextSharp的IndexOutOfRange

作者:互联网

不断获取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,该版本的bug完全符合您的问题:

> iTextSharp v5 GetTextFromPage() throws IndexOutOfRangeException
> Index was outside the bounds of the array while reading a .Pdf using iTextSharp

刚刚使用以下代码在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;
    }

标签:itextsharp,c,net
来源: https://codeday.me/bug/20191120/2047298.html