How to highlight a user selected text in PDF programatically? Also how to change its colour?

'Hi There,

I was following the documentation given in the link https://help.syncfusion.com/wpf/pdf-viewer/. I wanted to highlight a user-selected text programmatically after doing the search. I also would like to change the highlight color or highlight with a color set programmatically 

What I have done

  • Loaded a sample pdf in pdfView Control 
pdfViewerControl1.Load("F:/sample_page.pdf");
  • Initialised the Text SelectionCompleted Event
private void text_selection_completed(object sender, TextSelectionCompletedEventArgs args)
        {
            
            string selectedText = args.SelectedText;
           
        }
  • I wanted to highlight this text 'selectedText' to a color of my choice.
I was checking the solution from https://help.syncfusion.com/wpf/pdf-viewer/working-with-annotations/text-annotation. But it doesn't resolve the need. Here the article it is written that the user has to tap on pdf to highlight. (it was not working)
 Whereas I wanted to highlight a user selection with colour of my choice. Please find the attachment for the source code. Xaml and C# file


I'm using VB 2017 .

Attachment: SyncfusionWpfApp1_9765dfdb.7z

7 Replies 1 reply marked as answer

DD Divya Dhayalan Syncfusion Team February 28, 2021 08:24 PM UTC

Hi Akhil, 
 
Thank you for contacting Syncfusion support. 
 
We are currently checking the possibilities to achieve your requirement, we will update further details on 2nd March 2021. 
 
Regards, 
Divya  



VS Vikas Sekar Syncfusion Team March 2, 2021 10:43 PM UTC

Hi Akhil,   
  
We are checking the possibilities to achieve your requirement with high priority, we will update further details as early as possible.  
  
Regards,  
Vikas  



DD Divya Dhayalan Syncfusion Team March 4, 2021 08:52 AM UTC

Hi Akhil, 
 
Thank you for your patience. 
 
On analyzed further, if your requirement is to set the custom highlight color of text selection programmatically, we can achieve this by setting the TextSelectionBrushColor. Please refer the below code snippet for the same. 
 
private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
        { 
            pdfViewerControl1.TextSelectionBrushColor = System.Windows.Media.Color.FromRgb(0,255,0); 
        } 
 
Please download the sample from the below link,  
 
Kindly check and let us know if our observation is different from your requirement. This information will be helpful for us to analyze more and provide you a better solution. 
 
Regards, 
Divya 



AN Akhil Nadh March 4, 2021 03:15 PM UTC

Hi Divya,

Sorry, you have not fully answered my requirement. I will state again my requirement. 

  1. a user will be selecting a portion of text in pdf manually. 
  2. I wanted to capture the user selection using your API
  3. Then I wanted to highlight that same portion of the text using your API of a colour of choice.
I was able to do it till step 2. I need to know how to do step 3. As I told you in the previous thread I was able to capture the user's selection using the API Text SelectionCompleted Event.

Now I have the required user-selected text from pdf in a string variable selectedText as shown in the previous thread. I wanted to highlight this user selection (ie the contents of string variable selectedText) in red colour. 

In your response. you have shown  pdfViewerControl1.TextSelectionBrushColor = System.Windows.Media.Color.FromRgb(0,255,0);  to choose the brush colour but inorder to apply it a user will have to manually highligh the document right? What I wanted is the API to highlight the `selectedText` . 

It would be great if you ccan show me the code snippet to perform the task


DD Divya Dhayalan Syncfusion Team March 5, 2021 09:28 PM UTC

Hi Akhil, 
 
Thanks for the update. 
 
As you have requested, we have created a sample to highlight the text which has been selected by user. Please download the sample from the below link, 
 
We have also created a video for your reference which can be downloaded from the below link, 
 
Please find the below code snippet for creating the highlight annotation with color of our choice using TextMarkupColor. 
 
//Add text markup annotation on the bounds of highlighting text 
                    PdfTextMarkupAnnotation textmarkup = new PdfTextMarkupAnnotation(selectedtext.Value); 
                    //Sets the markup annotation type as HighLight 
                    textmarkup.TextMarkupAnnotationType = PdfTextMarkupAnnotationType.Highlight; 
                    //Sets the content of the annotation 
                    textmarkup.Text = selectedText; 
                    //Sets the highlighting color 
                    textmarkup.TextMarkupColor = new PdfColor(System.Drawing.Color.OrangeRed); 
                    //Add annotation into page 
                    page.Annotations.Add(textmarkup); 
 
In the above sample, the selected text information is captured using the TextSelectionCompleted event. In the event handler, we have created PdfTextMarkupAnnotation of highlighted type for the selected text with color of our choice. So, the created annotation will be added into the PdfLoadedDocument object of the PdfViewerControl and save into stream, which will be reloaded to the control. 
 
Please let us know if the provided solution helps you to achiever your requirement or if you need any further assistance on this. 
 
Regards, 
Divya 


Marked as answer

AN Akhil Nadh March 15, 2021 12:09 PM UTC

Hi Divya,

The solution you have provided is perfect. Thank you so much for the input.


DD Divya Dhayalan Syncfusion Team March 16, 2021 05:37 AM UTC

Hi Akhil, 
 
Most Welcome. 
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Divya 


Loader.
Up arrow icon