ITextSharp IndexOutOfRange
Keep getting IndexOutOfRangeException is an unhandled exception.
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));
}
source to share
Make sure you are using a version of iTextSharp greater than 5.1 that had a bug that exactly matches your problem:
- iTextSharp v5 GetTextFromPage () throws IndexOutOfRangeException
- Index was out of bounds array while reading .pdf with iTextSharp
Just tested with 5.5.4.0 (latest version) using this code that works:
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;
}
source to share
Your problem is your for loop
:
for (var currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
{
PdfTextExtractor.GetTextFromPage(reader, currentPageIndex);
}
There are a couple of problems with the above code:
Problem # 1
I don't know your reason to start with index 1
, not index 0
, since collections and arrays in C#
start with index 0
... you may be trying to skip the first page. If you start with index 1
, realize that you start counting on the second page. This brings me to my second problem ...
Problem # 2
currentPageIndex <= numberOfPages
As an example, if currentPageIndex
- 3
, a numberOfPages
- 3
, this expression will evaluate to true
, allowing code to be executed within the block. However, it numberOfPages
does indicate the length / amount of the array / collection. So the last valid subscript for length 3
would be index 2
.
You should change it to:
currentPageIndex < numberOfPages
... as there currentPageIndex
should be less than the total number of pages. Otherwise, it will be outside.
I would also recommend learning how to debug so that you can either step through your code or check the values ββat the time of the exception.
source to share