Find & Annotate Text

This demo uses the New DevExpress PDF Document API (PdfDocument) 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 markup annotation color and type (highlight, strikethrough, underline, or squiggly line). Click Find & Annotate to update the document and download the result.

Select a Document
Sample_Main.pdf



using DevExpress.Docs.Pdf;

Stream FindHighlightText(Stream documentStream, string searchText, System.Drawing.Color color, TextMarkupAnnotationType annotationType) {
    using var doc = new PdfDocument(documentStream);

    foreach(var result in doc.FindText(searchText)) {
        foreach(var match in result.Matches)  {
            var annotation = new TextMarkupAnnotation(match.MatchFragments.Select(f => f.Rectangle).ToArray()) {
                    Color  = PdfColor.FromRgb(color.R, color.G, color.B),
                    CreationDate = DateTime.UtcNow,
                    Title  = "DX Demo",
                    Type   = annotationType
                };
            doc.Pages[result.PageIndex].Annotations.Add(annotation);
        }
    }
    var outputStream = new MemoryStream();
    doc.Save(outputStream);

    return outputStream;
}