How to Add Text Annotations to PDF files using .NET

Text annotations

PDF is a good way to share documents with people. It guarantees you that your document looks on any computer exactly the same as it is on yours. But working with documents may sometimes require an ability to manipulate with a text. To do it programmatically you can use our Annotation API for .NET. We support a various text annotations, such as:

In this article we will cover just a few of them - highlight and strikeout annotations. All others works in exactly the same way.

Highlight annotation

Highlight annotation highlights and comments selected text. This is a good way to “highlight” an import text, to bring it to the user’s attention. Highlighting is used everywhere, so this is unquestionably text annotation number one.

You have ability to specify the next properties for HighlightAnnotation type:

  • BackgroundColor - describes annotation background color;
  • FontColor - color of annotation text;
  • Opacity - allows to set annotation opacity;
  • Points - annotation positions set by array of points.

Follow these steps to add Highlight annotation to document:

  • Instantiate Annotator object with input document path or stream;
  • Instantiate HighlightAnnotation object with desired properties (position, page number, etc);
  • Call Add method and pass HighlightAnnotation object;
  • Call Save method with resultant document path or stream.

The following code demonstrates how to add HighlightAnnotation to the document:

//Add highlight annotation to the document from local disk
using (Annotator annotator = new Annotator("input.pdf"))
{
	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
            }
        }
    };
    annotator.Add(highlight);
    annotator.Save("result.pdf");
} 

Strikeout annotation

Strikeout annotation marks text fragment with a strikethrough styling. When you receive a document for review and find errors or inconsistencies in it, it is very important to be able to draw the author’s attention to them. We recommend to you strikeout annotation for these purposes. Whenever the highlight annotation draws attention to the correct and important parts of the document, the strikeout annotation is usually used to cross out the wrong and unimportant parts of the document. You can check an example below.

There is an ability to specify the next properties for StrikeoutAnnotation type:

  • FontColor – color of annotation text;
  • Opacity – allows to set annotation opacity;
  • Points – annotation positions set by array of points.

Follow these steps to add Strikeout annotation to document:

  • Instantiate Annotator object with input document path or stream;
  • Instantiate StrikeoutAnnotation object with desired properties (position, page number, etc);
  • Call Add method and pass StrikeoutAnnotation object;
  • Call Save method with resultant document path or stream.

The following code demonstrates how to add StrikeoutAnnotation to the document:

//Add strikeout annotation to the document from local disk
using (Annotator annotator = new Annotator("input.pdf"))
{
	StrikeoutAnnotation strikeout = new StrikeoutAnnotation
    {
    	CreatedOn = DateTime.Now,
        FontColor = 65535,
        BackgroundColor = 16761035,
        Message = "This is strikeout annotation",
        Opacity = 0.7,
        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
            }
        }
     };
     annotator.Add(strikeout);
     annotator.Save("result.pdf");
}

Conclusion

In short, we have learned how to add text annotations to a PDF file within .NET applications. We have introduced all the text annotations that our API supports and illustrated how and when to use them with an example. Now, you should be confident to build your own document annotator .NET application.

More resources

Advanced Usage Topics

To learn more about document annotating features, please refer to the advanced usage section.

Free Online App

Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to annotate your PDF, DOC or DOCX, XLS or XLSX, PPT or PPTX, PNG and other documents with free to use online GroupDocs Annotation App.