title: Highlight element in pdf based on coordinates c#
body: <p>I have 5 pages pdf need to highlight specific element based on coordinates </p>
  
  <p>Have <code>X top left,Y top left,X top right ,Y top  right ,  X bottom right , Y bottom right ,X bottom left, Y bottom left</code>  . </p>
  
  <p>I tried below code using iTextsharp please suggest how can we do this including page no </p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Data;
  using System.Text; 
  using System.Windows.Forms; 
  using System.IO;
  using iTextSharp.text;
  using iTextSharp.text.pdf;        
  
  
  //Create a simple test file
  string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
  
  //Create a new file from our test file with highlighting
  string highLightFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Highlighted.pdf");
  
  //Bind a reader and stamper to our test PDF
  PdfReader reader = new PdfReader(outputFile);
  
  using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None))
  {
      using (PdfStamper stamper = new PdfStamper(reader, fs))
      {
          //Create a rectangle for the highlight. NOTE: Technically this isn't used but it helps with the quadpoint calculation
          iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f);
          //Create an array of quad points based on that rectangle. NOTE: The order below doesn't appear to match the actual spec but is what Acrobat produces
          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;
  
          //Add the annotation
          stamper.AddAnnotation(highlight,1);
      }
  }
  </code></pre>
  
  <p><strong>Output</strong>
  Highlight element in rectangular.
  Need to highlight 3rd page of PDF.</p>
  
  <p><code>"boundingBox": [3.2924,7.7146,5.7564,7.7038,5.7671,7.9836,3.3032,7.9943]</code> </p>
  
  <p>this <strong>"text": "66 66 6666 6666"</strong> should get higlighted  </p>
  
  <p><a href="https://filebin.net/qwyjhkdvw72yzpgn/inputPdf.pdf?t=r2sh2utq" rel="nofollow noreferrer">Input File</a>
  <a href="https://filebin.net/qwyjhkdvw72yzpgn/output.pdf?t=r2sh2utq" rel="nofollow noreferrer">Output File</a></p>