I am using iTextSharp for reading PDF documents but lately it seems that i'm getting a
{"Object reference not set to an instance of an object."}
or NullReferenceException upon getting the text from the page of PdfReader. Before it is working but after this day, it is not already working. I didn't change my code.
Below is my code:
for (int i = 1; i <= reader.NumberOfPages; i++)
{
ITextExtractionStrategy its = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(reader, i, its);
if (currentText.Contains("ADVANCES"))
{
return i;
}
}
return 0;
The above code throws a null reference exception, reader is not null and i is obviously not null being an int.
I am instantiating the PDFreader from the input stream
PdfReader reader = new PdfReader(_stream)
Below is the stack trace:
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.DisplayXObject(PdfName xobjectName)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.InvokeOperator(PdfLiteral oper, List`1 operands)
at iTextSharp.text.pdf.parser.PdfContentStreamProcessor.ProcessContent(Byte[] contentBytes, PdfDictionary resources)
at iTextSharp.text.pdf.parser.PdfReaderContentParser.ProcessContent[E](Int32 pageNumber, E renderListener)
at iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(PdfReader reader, Int32 pageNumber, ITextExtractionStrategy strategy)
To be simple, i tried to create a simple console application that will just read all the text from the PDF file and display it. Below is the code. Result is the same as above, it gives NullReferenceException.
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ExtractTextFromPdf(@"stockQuotes_03232015.pdf"));
}
public static string ExtractTextFromPdf(string path)
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
}
return text.ToString();
}
}
}
Does anyone know what might be going on here or how i might work around it?