How to: Highlight Search Results in a PDF File
- 4 minutes to read
This example highlights search results in a document. You can use PDF Graphics to draw filled rectangles around text or add annotations to highlight search results.
#Convert Page Coordinates to World Coordinates
#Simple
The PdfDocumentProcessor.FindText method returns the search results in the page coordinates. To draw filled rectangles that correspond to the document area containing found text, you need to convert page coordinates to world coordinates since the PdfGraphics.FillRectangle method uses world coordinates.
Use the following formula to convert the page coordinates (points) to world coordinates (units):
unitX = (x / 72) * dpiX;
unitY = ((cropBox.Height - y) / 72) * dpiY
In this example, the conversion is made in the following method. Please note that the used PdfGraphics.AddToPageForeground method overload allows you to specify dpiX and dpiY values as parameters. The method converts the given (72) DPI value to its native DPI (96) internally.
static void HighlightResultWithGraphics(PdfDocumentProcessor processor, PdfTextSearchResults result) {
using (PdfGraphics graphics = processor.CreateGraphics()) {
for (int i = 0; i < result.Rectangles.Count; i++) {
RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Page.CropBox.Height - (float)result.Rectangles[i].Top),
new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height));
using (var brush = new DXSolidBrush(Color.FromArgb(130, 55, 155, 255)))
graphics.FillRectangle(brush, rect);
}
graphics.AddToPageForeground(result.Page, 72, 72);
}
}
#Custom DPI
#Use PDF Graphics
Create a RectangleF object around the search result. Use the PdfGraphics.FillRectangle method to draw a rectangle around search text. The PdfGraphics.AddToPageForeground method allows you apply filled rectangles to the page.
}
//This method uses PdfGraphics to highlight text
static void HighlightResultWithGraphics(PdfDocumentProcessor processor, PdfTextSearchResults result)
{
using (PdfGraphics graphics = processor.CreateGraphics())
{
for (int i = 0; i < result.Rectangles.Count; i++)
{
RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Page.CropBox.Height - (float)result.Rectangles[i].Top),
new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height));
using (var brush = new DXSolidBrush(Color.FromArgb(130, 55, 155, 255)))
graphics.FillRectangle(brush, rect);
}
#Use Annotations
Call the PdfDocumentProcessor.AddTextMarkupAnnotation method to add an annotation to the search results. Use the PdfTextSearchResults.Rectangles property to access the document areas that contain search text. The PdfAnnotationData.Color property specifies the annotation color.
}
//This method uses annotations to highlight text
static void HighlightResultWithAnnotations(PdfDocumentProcessor processor, PdfTextSearchResults result)
{
PdfDocumentFacade facade = processor.DocumentFacade;
PdfPageFacade page = facade.Pages[result.Page.GetPageIndex()];
for (int i = 0; i < result.Rectangles.Count; i++)
{
PdfTextMarkupAnnotationFacade annotation =
page.AddTextMarkupAnnotation(result.Rectangles[i], PdfTextMarkupAnnotationType.Highlight);
#Search and Highlight Text
The following code sample calls the PdfDocumentProcessor.FindText method to search text. (If you wish to highlight the results, you can also use the HighlightResult method - as described above.)
{
//Create a PDF document processor.
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor())
{
//Define search words
string[] words = { "Get", "DX-RX809", "HD", "DX-B5000" };
//Load a PDF document
documentProcessor.LoadDocument(@"..\..\..\Document.pdf");
//Specify the search parameters
PdfTextSearchParameters searchParameters = new PdfTextSearchParameters();
searchParameters.CaseSensitive = true;
searchParameters.WholeWords = true;
foreach (string word in words)
{
PdfTextSearchResults result;
//Get the search results from the FindText method call with search text and search parameters
while ((result = documentProcessor.FindText(word, searchParameters)).Status == PdfTextSearchStatus.Found)
{
//HighlightResultWithGraphics(documentProcessor, result);
HighlightResultWithAnnotations(documentProcessor, result);
}
}
//Save the document