How to Highlight Text in PDF using C#

This tutorial aims to guide you how to highlight text in PDF using C#, with a straightforward code example to make the process clear and easy to follow. Additionally, it will provide instructions on how to utilize an annotation library to highlight text using C# in PDF. GroupDocs Annotation is a powerful library that allows you to annotate and collaborate on PDF documents in real-time. One of the key features of GroupDocs Annotation is the ability to highlight text within PDF documents. Here are the steps to highlight text in PDF using this library.

Steps to Highlight Text in PDF using C#

  1. Install GroupDocs.Annotation for .NET package from the NuGet
  2. To highlight text in PDF, you must include a reference to the GroupDocs.Annotation namespace
  3. Instantiate Annotator object with input document path
  4. Instantiate HighlightAnnotation object and set some properties of it
  5. Call Annotator.Add method and pass HighlightAnnotation object to it
  6. Call Annotator.Save method with resultant document path

By following these steps, you will be able to effectively highlight text within a PDF document. This can be a valuable way to emphasize critical information, make annotations or comments, or simply improve the organization of your reading. You can use the aforementioned steps to highlight text of PDF in C# on any system that supports .NET, without the need for additional software installation. The following code example demonstrates how to highlight text within a PDF document.

Code to Highlight Text in PDF using C#

using GroupDocs.Annotation.Models.AnnotationModels;
using GroupDocs.Annotation.Models;
using GroupDocs.Annotation;
using System;
using System.Collections.Generic;
namespace HighlightTextinPDFusingCSharp
{
internal class Program
{
static void Main(string[] args)
{
// Set License to avoid the limitations of Annotation library
License lic = new License();
lic.SetLicense(@"GroupDocs.Annotation.lic");
// Instantiate Annotator object with input PDF path
using (Annotator annotator = new Annotator("input.pdf"))
{
// Initialize new instance of HighlightAnnotation class
HighlightAnnotation highlight = new HighlightAnnotation
{
BackgroundColor = 65535,
CreatedOn = DateTime.Now,
FontColor = 0,
Message = "This is highlight annotation",
Opacity = 0.5,
PageNumber = 0,
Points = new List<Point>
{
new Point(80, 730), new Point(240, 730), new Point(80, 650), new Point(240, 650)
},
Replies = new List<Reply>
{
new Reply
{
Comment = "First comment",
RepliedOn = DateTime.Now
},
new Reply
{
Comment = "Second comment",
RepliedOn = DateTime.Now
}
}
};
// Add highlight annotation
annotator.Add(highlight);
// Save final PDF to disk
annotator.Save("result.pdf");
}
}
}
}

In the previous section, we presented a comprehensive guide to the process of highlighting text in PDF using C#, along with a simple code example. The code is brief and involves just a few API calls to execute text highlighting. Once you have installed the recommended annotation library and configured the file paths, it’s effortless to incorporate this code into your projects.

We previously published an article on adding dropdown in PDF; for more information, see how to add dropdown component in PDF using C#.

 English