Highlighting Selected Text in PDF

Discussions about annotation support.
Post Reply
cbingram
Posts: 4
Joined: Fri Oct 22, 2021 4:00 pm

Highlighting Selected Text in PDF

Post by cbingram » Fri Oct 22, 2021 4:07 pm

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!

User avatar
Fabio
Posts: 173
Joined: Thu Aug 27, 2020 9:57 am

Re: Highlighting Selected Text in PDF

Post by Fabio » Mon Oct 25, 2021 2:31 pm

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

cbingram
Posts: 4
Joined: Fri Oct 22, 2021 4:00 pm

Re: Highlighting Selected Text in PDF

Post by cbingram » Thu Nov 11, 2021 3:04 pm

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();
}

User avatar
Fabio
Posts: 173
Joined: Thu Aug 27, 2020 9:57 am

Re: Highlighting Selected Text in PDF

Post by Fabio » Fri Nov 12, 2021 4:11 pm

Hello there,

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

Have a nice day Sir,
Fabio

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest