3

Keep getting IndexOutOfRangeException was 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));
}
  • What does this have to do with a console window? – John Saunders Jan 08 '15 at 01:27
  • You should start by looking at the API of that `PdfTextExtractor.GetTextFromPage`. But you might try starting your index `currentPageIndex` at zero, and going until `currentPageIndex < numberOfPages` – GEEF Jan 08 '15 at 01:28
  • @JohnSaunders Thank you for editing my title, Wasn't aware of that thanks. The Console Window is where i'd like to display the text. Honestly i can;t past that error so i have gone about Writing it to the PDF to the Console. – Nathan Russell Jan 08 '15 at 01:36
  • @VP. Tried it without success. Got a different exception this time however. NullReferenceException – Nathan Russell Jan 08 '15 at 01:37
  • So you are _not_ trying to write to a console window. Your question is about the IndexOutOfRangeException. Once you have fixed that problem, you'll change your code to try to write to a console window. – John Saunders Jan 08 '15 at 01:37
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jan 08 '15 at 01:38
  • @JohnSaunders Yes exactly. – Nathan Russell Jan 08 '15 at 01:38
  • @VP. Api offered no help. – Nathan Russell Jan 08 '15 at 01:39
  • Please provide a stack trace of the exception. – mkl Jan 08 '15 at 05:10
  • Yes, another rule on SO should be: *always* mention the version number of the tool you're using. If it's not the latest version, try the latest version. If it works for the latest version, don't post your question. – Bruno Lowagie Jan 08 '15 at 06:50

2 Answers2

2

Make sure you're running an iTextSharp version greater than 5.1, which had a bug that exactly matches your problem:

Just tested with 5.5.4.0 (latest version), using this code, which 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;
    }
Community
  • 1
  • 1
kuujinbo
  • 8,814
  • 3
  • 38
  • 56
  • Yes, another rule on SO should be: *always* mention the version number of the tool you're using. If it's not the latest version, try the latest version. If it works for the latest version, don't post your question. – Bruno Lowagie Jan 08 '15 at 06:50
  • @kuujinbo Thank you very much. Solved the problem. Well sort off. I can now display a pdf, Just not the on i want. That however i will figure out on my own. Thank you very much. – Nathan Russell Jan 08 '15 at 13:04
1

Your problem lies within 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 reasons for starting at index 1, rather than index 0, since collections and arrays in C# start at index 0... perhaps you're trying to skip the first page. If you do start at index 1, understand that you're starting the count on the second page. This brings me to the second problem...

Problem #2

currentPageIndex <= numberOfPages

As an example, if currentPageIndex is 3 and numberOfPages is 3, this expression would evaluate to true, allowing the code within the block to execute. However, numberOfPages indicates the length/count of the array/collection. Thus, the last valid index for the length of 3, would be index 2.

You must change it to:

currentPageIndex < numberOfPages

... since currentPageIndex must remain less than the total number pages. Otherwise, it will be out of bounds.

I would also recommend learning how to debug, so that you may either step through your code or check the values at the time the exception gets thrown.

B.K.
  • 9,418
  • 9
  • 64
  • 98
  • I now get a different exception NullReferenceException. I am not sure if this is better or worse... – Nathan Russell Jan 08 '15 at 02:04
  • @T0rchic That tells us nothing. It could be that the file you're loading does not exist or you've provided a wrong path. Step through your code using the debugger. Besides, I'm not even sure if that's all of your code that you're showing us... since `sb` doesn't do anything. – B.K. Jan 08 '15 at 02:06
  • i use to do this, Not sure why i didnt incorporate this in the beginning. sb.Append(PdfTextExtractor.GetTextFromPage(reader, currentPageIndex) – Nathan Russell Jan 08 '15 at 02:13