Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to find and highlight text in PDF files in C#

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes, we need to search the PDF file to get specific text, but there may be numerous results returned. In order to confirm the location more conveniently, we need to highlight the selected text with background color while searching. In this article, we will give solutions of how to find and highlight selected text in PDF files programmatically in C#.

Highlight selected text

We can use page.FindText(string) method and foreach sentence to find specified text in entire PDF pages , save the search results in a PdfTextFind array, then highlight selected text by invoking find.ApplyHighLight() method:

using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.General.Find;


namespace HighlightPDFText
{
   class Program
   {
       static void Main(string[] args)
       {
           //load the sample document
           PdfDocument pdf = new PdfDocument();
           pdf.LoadFromFile("Sample.pdf");
           
           //find the text and set the highlight color
           PdfTextFind[] results = pdf.Pages[0].FindText("New Zealand", TextFindParameter.CrossLine).Finds;
           foreach (PdfTextFind result in results)
           {
               result.ApplyHighLight(Color.Yellow);
           }
           //save the document to file
           pdf.SaveToFile("FindandHighlight.pdf", FileFormat.PDF);      
        }
   }
}




Markup text

Highlight the selected text by adding markup annotation to specific range of text. We can go to the highlighted text directly by clicking the annotation.

using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using Spire.Pdf.General.Find;



namespace HighlightPDFText
{
   class Program
   {
       static void Main(string[] args)
       {
           //Load the sample PDF document
           PdfDocument doc = new PdfDocument();
           doc.LoadFromFile("Sample.pdf");

           PdfPageBase page = doc.Pages[0];

           //Locate the text that you want to markup
           PdfTextFind ptf = page.FindText("During its long isolation").Finds[0];
         
           //Create a Text Markup annotation
           PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("", "", ptf.Bounds);
           annotation.TextMarkupColor = Color.Yellow;
               
           //Add annotation to PDF page    
           page.AnnotationsWidget.Add(annotation);
           
           doc.SaveToFile("MarkupText.pdf", FileFormat.PDF);
       }
   }
}



Conclusion

It is quite easy for us to use find and highlight the text on the PDF files in C# with the help of Spire.PDF for .NET. Thanks for your reading and wish it helps.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!