I have 5 pages pdf need to highlight specific element based on coordinates
Have X top left,Y top left,X top right ,Y top right , X bottom right , Y bottom right ,X bottom left, Y bottom left
.
I tried below code using iTextsharp please suggest how can we do this including page no
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);
}
}
Output Highlight element in rectangular. Need to highlight 3rd page of PDF.
"boundingBox": [3.2924,7.7146,5.7564,7.7038,5.7671,7.9836,3.3032,7.9943]
this "text": "66 66 6666 6666" should get higlighted
First of all, you add the annotation to the wrong page.
You say
Need to highlight 3rd page of PDF.
but you put it on page 1:
stamper.AddAnnotation(highlight,1);
To fix this, change the page number there:
stamper.AddAnnotation(highlight,3);
Neither the coordinates in your code
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f);
nor those you gave in that JSON'ish way
"boundingBox": [3.2924,7.7146,5.7564,7.7038,5.7671,7.9836,3.3032,7.9943]
are anywhere near the location you want to highlight, at least not in the regular PDF coordinate system given by the page media box. By measuring in Adobe Acrobat I got the following approximate coordinates:
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(240f, 264f, 413f, 289f);
If any of the coordinates you showed are meant to be actual coordinates of the image part to highlight, ask the provider of those coordinates about the coordinate system used and accordingly transform to coordinates in the given page's media box.
You create quad
using this order:
float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top };
This results in concave caps. You probably want to use
float[] quad = { rect.Left, rect.Top, rect.Right, rect.Top, rect.Left, rect.Bottom, rect.Right, rect.Bottom };
instead which Adobe Reader shows as convex caps. For a background read this answer.
You say:
"66 66 6666 6666" should get higlighted
With the three changes above applied to your code I get this: