title: A number is out of range error while Highlighting PDF File using itextsharp
body: <p>I highlighted the word in pdf using the code in the answer to the following question: <a href="https://stackoverflow.com/questions/33952183/highlight-words-in-a-pdf-using-itextsharp-not-displaying-highlighted-word-in-br">[Highlight words in a pdf using itextsharp, not displaying highlighted word in browser]</a><a href="http://nsktex.com/101111j174754572003tb00036x.pdf" rel="nofollow noreferrer">2</a></p>
  
  <p>But i got "A number is out of range error" when open this <a href="http://nsktex.com/101111j174754572003tb00036x.pdf" rel="nofollow noreferrer">pdf</a>.</p>
  
  <pre><code>   private void hightlightPDFText()
  {
      try
      {
          string[] highlightText = new string[] { "EXTENSIONAL", "geology" };
          string inputFile = Server.MapPath("~/pdf/") + "101111j174754572003tb00036x.pdf";
          string outputFile = Server.MapPath("~/pdf/") + "101111j174754572003tb00036x_Highlighted.pdf";
          // get input document            
          highlightPDFAnnotation(inputFile, outputFile, highlightText);
      }
      catch (Exception ex)
      {
          Response.Write(ex.Message);
      }
  }
  private void highlightPDFAnnotation(string inputFile, string outputFile, string[] highlightText)
  {
      try
      {
          PdfReader reader = new PdfReader(inputFile);
  
          using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
          {
              using (PdfStamper stamper = new PdfStamper(reader, fs))
              {
                  myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
                  int pageCount = reader.NumberOfPages;
                  for (int pageno = 1; pageno &lt;= pageCount; pageno++)
                  {
  
  
                      string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy);
                      for (int i = 0; i &lt; highlightText.Length; i++)
                      {
                          List&lt;iTextSharp.text.Rectangle&gt; MatchesFound = strategy.GetTextLocations(highlightText[i].Trim(), StringComparison.CurrentCultureIgnoreCase);
                          foreach (Rectangle rect in MatchesFound)
                          {
                              float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top };
                              //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);
                              stamper.AddAnnotation(highlight, pageno);
                          }
                      }
                  }
              }
          }
          reader.Close();
  
          File.Copy(outputFile, inputFile, true);
          File.Delete(outputFile);
      }
      catch (Exception ex)
      {
          throw;
      }
  
  }
  </code></pre>