Draw circle around text in PDF using C# .NET | Aspose.PDF | Circle is drawn at wrong position

Hi asad ali,

When i am using bellow formulae for circular annotation the circle placed wrong position. The formulae taken from bellow link. Could please help how to to do circular annotation on PDF.

                var canvas = new Aspose.Pdf.Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
                page.Paragraphs.Add(canvas);
                Aspose.Pdf.Drawing.Ellipse circle = new Aspose.Pdf.Drawing.Ellipse(tf.Rectangle.ToRect().Left - (float)page.PageInfo.Margin.Left - 3, tf.Rectangle.ToRect().Bottom + (float)page.PageInfo.Margin.Bottom - (float)tf.Rectangle.Height - 3, (float)tf.Rectangle.Width + 5, (float)tf.Rectangle.Height + 5);
           
                var c = ColorTranslator.FromHtml("#FFFF00");
                circle.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
                canvas.Shapes.Add(circle);

@chennampraveen

Would you please also share the sample source PDF document for our reference so that we can test the scenario in our environment and address it accordingly.

sample.pdf (144.0 KB)

Please find attached sample pdf file. Also can you please send formulae for circular annotation for existing pdf document. With this we need Rectangle ,Line, Arrow annotations. Could please provide any formulae defined by aspose.

@chennampraveen

Please use Page.MediaBox in order to get correct height and width of the page and try the below sample code snippet to draw circle around text in PDF using C#:

Document doc = new Document(dataDir + "sample.pdf");
Page page = doc.Pages[1];

TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber(@"DOWNLOADING");
doc.Pages[1].Accept(textAbsorber);

var tf = textAbsorber.TextFragments[1];

var canvas = new Drawing.Graph((float)page.MediaBox.Width, (float)page.MediaBox.Height);
canvas.Margin = new MarginInfo(0, 0, 0, 0);
page.Paragraphs.Add(canvas);
Aspose.Pdf.Drawing.Ellipse circle = new Drawing.Ellipse(tf.Rectangle.ToRect().Left - (float)page.PageInfo.Margin.Left - 3, tf.Rectangle.ToRect().Bottom + (float)page.PageInfo.Margin.Bottom - (float)tf.Rectangle.Height - 3, (float)tf.Rectangle.Width + 5, (float)tf.Rectangle.Height + 5);

var c = ColorTranslator.FromHtml("#FFFF00");
circle.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
canvas.Shapes.Add(circle);
doc.Save(dataDir + "CircleGraph.pdf");

CircleGraph.pdf (144.0 KB)

Thank you asad ali. I will check and update you.

I am getting selected text with rectangular coordinates. I have top, left, width, Hight of selected text by using these can i draw a circle, line, arrow. Do you have any code snippets to draw circle, line, arrow to a selected text (here text will come based on coordinates).

In the above given code if “DOWNLOADING” text is more than once in a pdf file all text’s are highlighted. But I want only one selected text should be highlighted.[quote=“asad.ali, post:5, topic:229363, full:true”]
@chennampraveen

Please use Page.MediaBox in order to get correct height and width of the page and try the below sample code snippet to draw circle around text in PDF using C#:

Document doc = new Document(dataDir + "sample.pdf");
Page page = doc.Pages[1];

TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber(@"DOWNLOADING");
doc.Pages[1].Accept(textAbsorber);

var tf = textAbsorber.TextFragments[1];

var canvas = new Drawing.Graph((float)page.MediaBox.Width, (float)page.MediaBox.Height);
canvas.Margin = new MarginInfo(0, 0, 0, 0);
page.Paragraphs.Add(canvas);
Aspose.Pdf.Drawing.Ellipse circle = new Drawing.Ellipse(tf.Rectangle.ToRect().Left - (float)page.PageInfo.Margin.Left - 3, tf.Rectangle.ToRect().Bottom + (float)page.PageInfo.Margin.Bottom - (float)tf.Rectangle.Height - 3, (float)tf.Rectangle.Width + 5, (float)tf.Rectangle.Height + 5);

var c = ColorTranslator.FromHtml("#FFFF00");
circle.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
canvas.Shapes.Add(circle);
doc.Save(dataDir + "CircleGraph.pdf");

CircleGraph.pdf (144.0 KB)
[/quote]

Can we save the file name "sample.pdf " instead of saving CircleGraph.pdf.
I tried saving with “CircleGraph.pdf” working fine. When i replaced the filename with sample.pdf the circle is misplaced. Please find the attached screensample.pdf (419.1 KB)

@chennampraveen

You can save the file with any name of your desire, however, if you want to save using the same name as of input, you can try saving the file in a different location.

Furthermore, we were unable to notice multiple addition of Circles while testing the scenario in our environment. Please share the complete code snippet that you are trying so that we may also replicate the issue at our end and address it accordingly.

Please check the below code to draw line and arrow in a PDF based on the coordinates:

// Draw Line
Point startH = new Point(tf.Rectangle.LLX, tf.Rectangle.LLY);
Point endH = new Point(tf.Rectangle.URX, tf.Rectangle.URY);
Aspose.Pdf.Annotations.LineAnnotation AsposeLineAnnH = new Aspose.Pdf.Annotations.LineAnnotation(page, tf.Rectangle, startH, endH);
Border border = new Border(AsposeLineAnnH);
border.Width = 2;
AsposeLineAnnH.Border = border;
page.Annotations.Add(AsposeLineAnnH);

// DrawArrow
LineAnnotation line2 = new LineAnnotation(page, tf.Rectangle, startH, endH)
{
 Intent = LineIntent.LineDimension,
 Color = Aspose.Pdf.Color.Black,
 EndingStyle = LineEnding.OpenArrow,
 StartingStyle = LineEnding.None
};
Border border2 = new Border(line2);
border2.Width = 2;
line2.Border = border2;
page.Annotations.Add(line2);

@chennampraveen

We also noticed that you are using trial version of the API. Please use it with a valid or 30-days free temporary license in order to get desired results.

Hi asad ali,

Yes, we are using trail version of the API. We have some queries on features. Will check and get back to you.

Hi asad ali,

Thank you, it is very useful for us.
Could you please share sticky note code snippet also.

@chennampraveen

Please check the below article in the API documentation to add text annotation which will act like a sticky note:

Hi Asad ali,
I am able to draw rectangle annotation on pdf,but the font sizes are different in multiple paragraphs .So the rectangle is drawn wrong location for bigger fonts. Is there any solution to draw a rectangle on different font sizes of uploaded PDFsample.pdf (3.0 KB)
PDF.

The initial features we are looking
We need to draw a rectangle, Circle, Arrow annotations on pdf viewer with coordinates. It should drawn event in zoom in and zoom out mode also. Please update on this as early as possible.

@chennampraveen

We have already shared a code snippet with you in our previous response that adds circle, arrow, and line in PDF against the searched text. However, please share how you are getting coordinates of text and what values you are obtaining to draw them?

Please share the values of coordinates which you have and share the word in PDF around which you want to draw annotation or shape. We will further try to create a sample code snippet and share with you.

@chennampraveen

We have already shared a code snippet with you in our previous response that adds circle, arrow, and line in PDF against the searched text. However, please share how you are getting coordinates of text and what values you are obtaining to draw them?

Please share the values of coordinates which you have and share the word in PDF around which you want to draw annotation or shape. We will further try to create a sample code snippet and share with you.

Hi asad ali,
Please find attached screen. I am trying to annotate the word
“This is a small demonstration” on the pdf of first line but it is drawn on second line.
And i am sharing my coordinates and code here.

Coordinates of the first word:(annotated_screen1)-This is drawn correctly
height: 9.333343505859375
left: 260.50189208984375
selectedText: " approva"
top: 232.77084350585938
width: 30.618896484375

Coordinates of the second word:(annotated_screen2)- This is drawn wrongly

  1. height: 9.333328247070312
  2. left: 410.60418701171875
  3. pageNumber: “1”
  4. selectedText: “CTN LOB DIMENHYDRINATE TAB BP 50MG 10”
  5. top: 67.04167175292969
  6. width: 154.4189453125annotated_screen2.png (157.7 KB)
    annotated_screen1.png (201.5 KB)

string contentRootPath = _hostingEnvironment.ContentRootPath;
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(“Aspose.PDF.Product.Family.lic”);
Aspose.Pdf.Document document = new Aspose.Pdf.Document(Path.Combine(contentRootPath + “\ClientApp\src\assets\” + annotate.filename));
double pageHeight = document.Pages[annotate.pageNumber].Rect.Height;
var page = document.Pages[annotate.pageNumber];
document.FitWindow = true;
double X = (annotate.yValue - 30), Y = (annotate.xValue - 10), Height = (annotate.height), Width = annotate.width;
TextAnnotation textAnnotation = new TextAnnotation(document.Pages[annotate.pageNumber], new Aspose.Pdf.Rectangle(X, page.Rect.Height - Y - Height, X + Width, page.Rect.Height - Y));

            TextFragmentAbsorber ta = new TextFragmentAbsorber();
            Border border = new Border(textAnnotation);
            border.Width = 30;
            border.Dash = new Dash(1, 1);
            textAnnotation.Border = border;
            textAnnotation.Color = Aspose.Pdf.Color.DarkRed;
            textAnnotation.Rect = new Aspose.Pdf.Rectangle(X, page.Rect.Height - Y - Height, X + Width, page.Rect.Height - Y);
           
            ta.TextSearchOptions = new TextSearchOptions(textAnnotation.Rect);
            ta.Visit(document.Pages[annotate.pageNumber]);

                document.Pages[annotate.pageNumber].Annotations.Add(textAnnotation);
                var squareAnnotation = new SquareAnnotation(document.Pages[annotate.pageNumber], new Aspose.Pdf.Rectangle(X, page.Rect.Height - Y - Height, X + Width, page.Rect.Height - Y))
                {
                    Title = loggedInUser + "-" + "(" + annotate.department + ")" + ":" + DateTime.Now.ToString(),
                    Color = Aspose.Pdf.Color.Red,
                    InteriorColor = Aspose.Pdf.Color.MistyRose,
                    Contents = annotate.annotatedText,
                    Opacity = 0.5,
                    
                    Popup = new PopupAnnotation(document.Pages[annotate.pageNumber], new Aspose.Pdf.Rectangle(X, page.Rect.Height - Y - Height, X + Width, page.Rect.Height - Y))
                };
                document.Pages[annotate.pageNumber].Annotations.Add(squareAnnotation);
                document.Save(System.IO.Path.Combine(Path.Combine(contentRootPath + "\\ClientApp\\src\\assets\\" + annotate.filename)));
                pdfsave.SaveAnnotation(annotate);

What i observed here is, If i selecte bigger font size word on PDF the rect or circles are drawn above the text.

@chennampraveen

We are testing the scenario in our environment and will get back to you shortly.

Hi asad ali,

Do you have any update on above issue.
Along with this we need to know aspose supports bellow features
1.PDF comparison
2.PDF Layer seperation
3. Measurement Tool.

@chennampraveen

We have tried to run the code snippet that you have shared but were unable to do so as there were some undefined variables in it. Furthermore, the shared PDF file was different than the one you showed in the screenshots. Nevertheless, we have tried following code snippet to add Square and Text Annotation in the sample.pdf around text “This is a small demonstration .pdf file”. We did not notice any issue and the output PDF file was fine.

Aspose.Pdf.Document document = new Aspose.Pdf.Document(dataDir + "sample.pdf");
double pageHeight = document.Pages[1].Rect.Height;
var page = document.Pages[1];
document.FitWindow = true; 
            
TextFragmentAbsorber ta = new TextFragmentAbsorber("This is a small demonstration .pdf file");
ta.Visit(document.Pages[1]);

TextAnnotation textAnnotation = new TextAnnotation(document.Pages[1], ta.TextFragments[1].Rectangle);

Border border = new Border(textAnnotation);
border.Width = 30;
border.Dash = new Dash(1, 1);
textAnnotation.Border = border;
textAnnotation.Color = Aspose.Pdf.Color.DarkRed;
textAnnotation.Rect = ta.TextFragments[1].Rectangle;

document.Pages[1].Annotations.Add(textAnnotation);
var squareAnnotation = new SquareAnnotation(document.Pages[1], ta.TextFragments[1].Rectangle)
{
 Title = DateTime.Now.ToString(),
 Color = Aspose.Pdf.Color.Red,
 InteriorColor = Aspose.Pdf.Color.MistyRose,
 Contents = "Test Text",
 Opacity = 0.5,

 Popup = new PopupAnnotation(document.Pages[1], ta.TextFragments[1].Rectangle)
};
document.Pages[1].Annotations.Add(squareAnnotation);
document.Save(dataDir + "result.pdf");

result.pdf (3.1 KB)

We are afraid that PDF comparison is not yet supported in the API. You can extract text from two PDFs using Aspose.PDF and compare them by implementing your custom method in C#.

Please elaborate a bit more about your above requirements so that we can share our feedback with you accordingly.