Page 1 of 1

Highlighting Selected Text in PDF

Posted: Fri Oct 22, 2021 4:07 pm
by cbingram
Is there a way to highlight the selected text within a PDF document. When looking at a document, the user can select the text and do things like Copy but I would like them to be able to highlight that selected text and burn it to the document, like you can do within a standard PDF Viewer.

Currently the only way we can do it is by using multiple rectangle annotations but this causes a lot of bloat in the size of the PDF. The SearchText feature is able to highlight text but there is no way that I know of to save this highlighting so that it will be there on future opens of the document.

Does anything like this exist in GdPicture to highlight selected text? Can you point me to any examples?

Thank you!

Re: Highlighting Selected Text in PDF

Posted: Mon Oct 25, 2021 2:31 pm
by Fabio
Hello,

You can highlight the text using the mouse using the GetRectCoordinatesOnDocument and the AddRegion methods.
Here is the link to our documentation with a complete code snippet: https://www.gdpicture.com/guides/gdpict ... mouse.html

You can also use the GetSelectedTextRegion method to directly get the selected text coordinates.

With best,
Fabio

Re: Highlighting Selected Text in PDF

Posted: Thu Nov 11, 2021 3:04 pm
by cbingram
Thank you very much for this information, Fabio. I was able to combine what you provided and information from another post I found for a successful implementation. In case this helps anyone else, since I wanted an actual Annotation to be added rather than a region I used the AnnotationManager to add a RectangleHighlighterAnnotation to the screen.

Sample code:

Code: Select all

RectangleF[] selectedRegions = gdViewer.GetSelectedTextRegions();
if (selectedRegions != null && selectedRegions.Length > 0)
{
     AnnotationManager manager = gdViewer.GetAnnotationManager();
     foreach (RectangleF rect in selectedRegions)
     {
          var annot = manager.AddRectangleHighlighterAnnot(colorToUse, rect.Left, rect.Top, rect.Width, rect.Height);
     }
     gdViewer.ClearSelectedText();
}

Re: Highlighting Selected Text in PDF

Posted: Fri Nov 12, 2021 4:11 pm
by Fabio
Hello there,

I'm glad if you achieved what you wanted to do!

Have a nice day Sir,
Fabio