Annotations added at the wrong position

I am modifying case in strings. Each time I either upper or lower case a character I want to highlight it. I am able to get some documents to work but others do not work at all. Instead the annotation is no where near the text. Here is the code:

    private void AnnoatateUpdates(string before, TextFragment textFragment, int startIndex)
    {
        string letter = before.Substring(startIndex, 1);

        for (int segNum = 1; segNum <= textFragment.Segments.Count; segNum++)
        {
            TextSegment segment = textFragment.Segments[segNum];

            if (segment.EndCharIndex > startIndex)
            {
                var leftChar = segment.Characters[startIndex + 1];

                Aspose.Pdf.Rectangle rect = leftChar.Rectangle;
                Aspose.Pdf.Annotations.HighlightAnnotation highlightText = new Aspose.Pdf.Annotations.HighlightAnnotation(textFragment.Page, rect);
                highlightText.Opacity = 0.5;
                highlightText.Color = Aspose.Pdf.Color.Red;
                textFragment.Page.Annotations.Add(highlightText);

            }
        }

@mjanulaitis

Would you please share a sample PDF document and details of TextFragments you want to highlight. We will further test the scenario in our environment and address it accordingly.

Here’s the file. I scan the file then make capitalization changes. On other docs the annotations show up correctly but on this doc they are all over the place.

Edited1010175Guidance on international data trans_71478527.pdf (82.2 KB)

@mjanulaitis

As you did not provide any information about which word or sentence you tried to highlight, we tested the scenario by extracting all text of PDF document and highlighting it using Aspose.PDF for .NET 19.3. We have noticed that coordinates of extracted text fragments were not correct thus highlight annotations were also not added correctly. Following is the code snippet that we tried and attached is the output for your reference.

Document document = new Document(dataDir + "Edited1010175Guidance on international data trans_71478527.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
document.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection1 = textFragmentAbsorber.TextFragments;
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;
 textFragment.Page.Annotations.Add(highlightText);
}
document.Save(dataDir + "SampleForHighlighting_out.pdf");

SampleForHighlighting_out.pdf (392.4 KB)

We have logged an issue as PDFNET-46213 in our issue tracking system for the sake of correction. We will further look into details of the issue and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.