Free Support Forum -

HighlightAnnotation in pdf is not accurate


hi to all

i use highlight annotation to highlight some text in pdf but is not accurate on the big text going a little bit up i’ll attach file pdf for example
wwwmetronewsit190107sanitC3A0garofalohcsaleoltre83casacuranobilihtmlGAROFALOHEALTHCAREWEB.pdf (184.6 KB)



Thanks for contacting support.

Could you please share respective source PDF document along with sample code snippet. We will test the scenario in our environment and address it accordingly.


here you are sample code :

Dim licence As Aspose.Pdf.License = New Aspose.Pdf.License
licence.SetLicense(Application.StartupPath + “\lib\Aspose.Pdf.lic”)

        'Preprocessing keywords
        KeyWord = KeyWord.Trim()
        KeyWord = KeyWord.Replace("(?i) ", "(?i)")
        KeyWord = KeyWord.Replace(" |", "|")
        KeyWord = KeyWord.Replace(" ", "\s+")
        KeyWord = KeyWord.Replace("(?i)", "\b(?i)")
        KeyWord = KeyWord.Replace("|", "\b|")
        KeyWord = KeyWord + "\b"

        Dim Document As New Document(PathPDFSource)

        Dim TextFragmentAbsorber As New TextFragmentAbsorber(KeyWord)

        'Set text search option to specify regular expression usage
        Dim TextSearchOptions As New TextSearchOptions(True)

        TextFragmentAbsorber.TextSearchOptions = TextSearchOptions


        Dim TextFragmentCollection1 As TextFragmentCollection = TextFragmentAbsorber.TextFragments
        If TextFragmentCollection1.Count > 0 Then
            Dim Objxml As New ClsXml
            For Each TextFragment As TextFragment In TextFragmentCollection1

                Dim FreeText As New HighlightAnnotation(TextFragment.Page, New Aspose.Pdf.Rectangle(TextFragment.Position.XIndent, TextFragment.Position.YIndent, TextFragment.Position.XIndent + TextFragment.Rectangle.Width, TextFragment.Position.YIndent + TextFragment.Rectangle.Height))
                Objxml.createNode(TextFragment.Page.Number.ToString, TextFragment.Position.XIndent.ToString, TextFragment.Position.YIndent.ToString, (TextFragment.Position.XIndent + TextFragment.Rectangle.Width).ToString, (TextFragment.Position.YIndent + TextFragment.Rectangle.Height).ToString)

                FreeText.Opacity = 0.9
                FreeText.Color = Aspose.Pdf.Color.FromArgb(255, 255, 0)

        End If




Thanks for sharing sample code snippet.

Would you please share your input PDF document with us. This would help us testing the scenario accordingly.


here you are , thank you
wwwmetronewsit190107sanitC3A0garofalohcsaleoltre83casacuranobilihtmlGAROFALOHEALTHCAREWEB.pdf (189.1 KB)



Thanks for providing sample PDF document.

We have tested the scenario using following code snippet with Aspose.PDF for .NET 19.1 and were unable to replicate the issue you have mentioned. For your kind reference, an output PDF is also attached.

Document document = new Document(dataDir + "SampleForHighlighting.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(@"(?i)Garofalo(?i)");
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
TextFragmentCollection textFragmentCollection1 = textFragmentAbsorber.TextFragments;
if (textFragmentCollection1.Count > 0)
 foreach (TextFragment textFragment in textFragmentCollection1)
  Aspose.Pdf.Annotations.HighlightAnnotation highlightText = new Aspose.Pdf.Annotations.HighlightAnnotation(textFragment.Page, new Aspose.Pdf.Rectangle(textFragment.Position.XIndent, textFragment.Position.YIndent, textFragment.Position.XIndent + textFragment.Rectangle.Width, textFragment.Position.YIndent + textFragment.Rectangle.Height));
 highlightText.Opacity = 0.5;
 highlightText.Color = Aspose.Pdf.Color.Yellow;

SampleForHighlighting_out.pdf (197.0 KB)

Would you please try using latest version of the API and in case you still face any issue, please feel free to let us know.