Free Support Forum -

Highlighting text with color in pdf



We are using Groupdocs.Viewer dll version ,which is being implemented in our project for viewing pdf file.In our project ,we have a requirement in which we need to highlight the pdf particular text with color ,which is matching with the textbox value which is entered by user.



Thanks for using GroupDocs.Viewer.

GroupDocs.Viewer does not provide any built-in feature to highlight the particular text in the output, however, its flexibility allows you to implement highlighting easily. You can highlight your desired word in the HTML representation of document pages using the < mark > tag. The following code sample shows how to highlight a particular word by inserting mark tag in the HTML content of document pages.

ViewerConfig config = new ViewerConfig();
config.StoragePath = "D:\\storage";
config.UseCache = true;

ViewerHtmlHandler htmlHandler = new ViewerHtmlHandler(config);
string guid = "candy.pdf";

var htmlOptions = new GroupDocs.Viewer.Converter.Options.HtmlOptions()
   IsResourcesEmbedded = true,

List<PageHtml> pages = htmlHandler.GetPages(guid, htmlOptions);

// text to highlight
string text = "candy";

foreach (PageHtml page in pages)
   // highlight text
   string HtmlContent = page.HtmlContent.Replace(text, "<mark>" + text + "</mark>");

   // save as HTML file 
   String fname = Path.Combine("OutputDir", 
   Path.GetFileNameWithoutExtension(page.PageNumber + "_" + guid) + ".html");
   System.IO.File.WriteAllText(fname, HtmlContent);

Hope it helps.