0
 private void highlightPDFAnnotation(string outputFile, int pageno, string[] splitText)
{
    try
    {
        PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(outputFile));
        using (FileStream fs = new FileStream(outputFile, FileMode.Open, FileAccess.Write, FileShare.None))
        {
            using (PdfStamper stamper = new PdfStamper(reader, fs))
            {
                myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
                string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy);
                for (int i = 0; i < splitText.Length; i++)
                {
                    List<iTextSharp.text.Rectangle> MatchesFound = strategy.GetTextLocations(splitText[i].Trim(), StringComparison.CurrentCultureIgnoreCase);
                    foreach (Rectangle rect in MatchesFound)
                    {
                        float[] quad = { rect.Left - 3.0f, rect.Bottom, rect.Right, rect.Bottom, rect.Left - 3.0f, rect.Top + 1.0f, rect.Right, rect.Top + 1.0f };
                        //Create our hightlight
                        PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad);
                        //Set the color
                        highlight.Color = BaseColor.YELLOW;

                        PdfAppearance appearance = PdfAppearance.CreateAppearance(stamper.Writer, rect.Width, rect.Height);
                        PdfGState state = new PdfGState();
                        state.BlendMode = new PdfName("Multiply");
                        appearance.SetGState(state);
                        appearance.Rectangle(0, 0, rect.Width, rect.Height);
                        appearance.SetColorFill(BaseColor.YELLOW);
                        appearance.Fill();

                        highlight.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance);

                        //Add the annotation
                        stamper.AddAnnotation(highlight, pageno);                            

                        highlight.Clear();

                    }
                }
                stamper.Close();
                stamper.Dispose();

            }
            fs.Close();
            fs.Dispose();


        }
        reader.Close();

        reader = null;
    }
    catch (Exception ex)
    {
        throw;
    }
}

After fourth attempt it throws "Object reference not set to an instance of an object." error.

Stacktrace :

at iTextSharp.text.pdf.PdfReader.PageRefs.ReadPages()
at iTextSharp.text.pdf.PdfReader.PageRefs..ctor(PdfReader reader)
at iTextSharp.text.pdf.PdfReader.ReadPages()
at iTextSharp.text.pdf.PdfReader.ReadPdf()
at iTextSharp.text.pdf.PdfReader..ctor(Byte[] pdfIn, Byte[] ownerPassword) at iTextSharp.text.pdf.PdfReader..ctor(Byte[] pdfIn) at bottom.highlightPDFAnnotation(String outputFile, Int32 pageno, String[] splitText)

Karthik
  • 91
  • 2
  • 13
  • At which line of code it throws this ? – Dhrumil Dec 01 '15 at 10:29
  • PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(outputFile)); – Karthik Dec 01 '15 at 10:32
  • 1
    Check the value for `outputFile` variable and make sure it is accessible at the specified location. – Dhrumil Dec 01 '15 at 10:34
  • this line does not throw any error. "System.IO.File.ReadAllBytes(outputFile)". new PdfReader( only throws error – Karthik Dec 01 '15 at 10:35
  • Three times i can highlight the pdf. It throws error after 4th attempting. – Karthik Dec 01 '15 at 10:37
  • Its tricky. Try putting a `for` loop with that one line of code and loop for 5-10 times and check if its working or not. Also, you might want to check on the official site for such an issue. It could possibly be a bug. – Dhrumil Dec 01 '15 at 10:41
  • for (int i = 1; i <= 20; i++){PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(outputFile)); reader.Close();} reader = null; It's working. – Karthik Dec 01 '15 at 10:44
  • So I guess the code seems fine if its functioning properly in the loop. You said that you did four attempts. Any chance that it might be due to the variation in attempts such as time gap between attempts or some kind of manipulation performed or some other logic executed between the attempts that could somehow affect the file. – Dhrumil Dec 01 '15 at 10:49
  • If you try again after such an exeption with the same input file, does the exception occur again? – mkl Dec 01 '15 at 10:57
  • Kindly check my updated code in the question. It does not throws any error when i comment this line " stamper.AddAnnotation(highlight, pageno);" After adding annotation only it throws error – Karthik Dec 01 '15 at 10:57
  • @mkl, yes, I'm over writing the same file. – Karthik Dec 01 '15 at 10:58
  • Please use different file names and try again. If the issue occurs then, too, please share the PDF that cannot be loaded anymore for analysis. – mkl Dec 01 '15 at 11:10
  • In other two pdf files, i highlighting the text 4 times and removed the higlighted text, it throws nullreferenceexception. Shall i provide other two pdf files? – Karthik Dec 01 '15 at 11:25
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – David Arno Dec 01 '15 at 11:29
  • I get the bytes from this line "byte[] bytes = File.ReadAllBytes(highLightFile);" When passing the bytes to this line "PdfReader reader = new PdfReader(bytes);". It throws null value exception. – Karthik Dec 01 '15 at 11:38
  • @mkl, i will create new file and swap them at the end. File.Copy(highLightFile, outputFile,true); File.Delete(highLightFile); is it correct? – Karthik Dec 01 '15 at 11:51
  • It's working fine. Thank you for all your support. – Karthik Dec 01 '15 at 11:57

0 Answers0