This demo uses the DevExpress PDF Document API (PdfDocumentProcessor) to search for text in a PDF document and annotate search results. You can process the predefined sample file or supply your own document. To do the latter, select Upload a File in the file selection drop-down menu.
Specify the search text and select the annotation type and color. Click Find & Annotate to update the document and download the result.
using DevExpress.Pdf;
Stream FindHighlightText(MemoryStream documentStream, string searchText, PdfTextMarkupAnnotationType annotationType, System.Drawing.Color annotationColor) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
PdfTextSearchResults search;
do {
search = processor.FindText(
searchText,
new PdfTextSearchParameters() {
CaseSensitive = false,
WholeWords = false
});
if(search.Status == PdfTextSearchStatus.Found) {
var annotation = processor.DocumentFacade.Pages[search.Page.GetPageIndex()].AddTextMarkupAnnotation(search.Rectangles, annotationType);
annotation.Author = "DX Demo";
annotation.CreationDate = DateTime.UtcNow;
annotation.Color = new PdfRGBColor(annotationColor.R / 255.0, annotationColor.G / 255.0, annotationColor.B / 255.0);
}
} while(search.Status == PdfTextSearchStatus.Found);
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}