使用iTextSharp读取.Pdf时,索引超出了数组的界限

本文关键字:数组 界限 索引 读取 iTextSharp Pdf 使用 | 更新日期: 2023-09-27 18:21:11

我使用开源工具iTextSharp读取我的Asp.Net MVC3应用程序中的.Pdf文件,该文件用c#.Net.编码

下面是我的代码。

   filePath = Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    Path.GetFileName(Infile.FileName));
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                    Infile.SaveAs(filePath);
                    var pdfdoc = new iTextSharp.text.Document();
                    PdfReader reader2 = new PdfReader((string)filePath);
                    string strText = string.Empty;
                    for (int page = 1; page <= reader2.NumberOfPages; page++)
                    {
                        iTextSharp.text.pdf.parser.ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                        PdfReader reader = new PdfReader((string)filePath);
                        String s = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, page,its);
                        s = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(s)));
                        strText = strText + s;
                        reader.Close();
                    }

我收到线上的错误

  String s = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, page,its);

错误为索引超出了数组的界限
当做

使用iTextSharp读取.Pdf时,索引超出了数组的界限

我通过将iTextSharp版本从5.1更新到5.2解决了这个问题。