How to search text and apply highlight text markup annotation for the desired page using WPF PDFViewer
In the WPF PDF Viewer, there is no direct support to search for text and apply a highlight markup annotation. However, this can be achieved through a workaround by navigating to the desired page using the GoToPage API and using the FindText API to search for the target text with its bounds. Using these bounds, create a PdfTextMarkupAnnotation, set its annotation type to Highlight
, and add it to the corresponding page to visually highlight the found text.
Steps to search text and apply highlight text markup annotation on a desired page in the PDF
Step 1:
Navigate to the desired page with the API GoToPage by passing the page number
pdfViewer.GoTopage(desiredPage+1);
Step 2:
Use the FindText API to search for a specific text on a given page. It returns a Boolean indicating whether a match was found and provides the bounding rectangles of the matched text.
List<RectangleF> textbounds = new List<RectangleF>();
bool isMatchFound = pdfViewer.FindText("F#", desiredPageIndex, out textbounds);
Step 3:
If matches are found, create and apply highlight annotations. Use the boolean variable isMatchFound
, to verify whether the text search was successful. If it’s true, we iterate through each rectangle in the textBounds collection. For each matching region, we create a PdfTextMarkupAnnotation, set its PdfTextMarkupAnnotationType to Highlight
, and add it to the corresponding page where the text was found.
if (isMatchFound)
{
foreach(RectangleF rect in textbounds)
{
//Creating the markup annotation
PdfTextMarkupAnnotation markUpannotation = new PdfTextMarkupAnnotation(rect);
PdfLoadedPage page = pdfViewer.LoadedDocument.Pages[desiredPageIndex] as PdfLoadedPage;
/Assigningg the type of TextMarkupp annotation
markUpannotation.TextMarkupAnnotationType = PdfTextMarkupAnnotationType.Highlight;
//Adding themarkupp annotation to the particular page
page.Annotations.Add(markUpannotation);
}
}
This approach allows you to highlight all occurrences of a specific text string on a desired PDF page using the PdfTextMarkupAnnotation.
A complete working sample to search text and apply highlight text markup annotation on a desired page in the PDF can be downloaded from GitHub
Conclusion
You can refer to our WPF PDF Viewer page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarification, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!