How to Insert Area Annotation to PDF using C#

This tutorial offers an extensive walkthrough on how to insert area annotation to PDF using C#. Including area annotations in a PDF can prove advantageous in various applications, enabling you to emphasize specific areas of the document and attach comments, notes, or other details. Additionally, we will furnish you with a code snippet as a reference for adding area annotations to your PDF using C#. The subsequent section presents in-depth directions to add area annotation to PDF in C#.

Steps to Insert Area Annotation to PDF using C#

  1. To add an area annotation into PDF document, you can use the NuGet package manager to install GroupDocs.Annotation for .NET
  2. Add reference to the GroupDocs.Annotation namespace
  3. Instantiate object of Annotator class by passing path of PDF file to its constructor
  4. Instantiate object of AreaAnnotation class and set desired properties e.g. position, page number, etc.
  5. Call the Annotator.Add method and provide it with an AreaAnnotation object as a parameter
  6. To save the PDF to the disk, call the Annotator.Save method

By adhering to the aforementioned steps, you can effortlessly create area annotation in PDF using C# with a handful of API calls from the annotation library. The annotation library operates seamlessly on Windows, macOS, and Linux operating systems, without the requirement of any supplementary software. Below is a sample C# program that incorporates an area annotation into a PDF file.

Code to Insert Area Annotation to PDF using C#

using GroupDocs.Annotation.Models.AnnotationModels;
using GroupDocs.Annotation.Models;
using GroupDocs.Annotation;
using System;
using System.Collections.Generic;
namespace InsertAreaAnnotationtoPDFusingCSharp
{
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 by passing path of PDF
// file to its constructor
using (Annotator annotator = new Annotator("input.pdf"))
{
// Create an instance of AreaAnnotation class
// and set some properties
AreaAnnotation area = new AreaAnnotation
{
BackgroundColor = 65535,
Box = new Rectangle(100, 100, 100, 100),
CreatedOn = DateTime.Now,
Message = "This is area annotation",
Opacity = 0.7,
PageNumber = 0,
PenColor = 65535,
PenStyle = PenStyle.Dot,
PenWidth = 3,
Replies = new List<Reply>
{
new Reply
{
Comment = "First comment",
RepliedOn = DateTime.Now
},
new Reply
{
Comment = "Second comment",
RepliedOn = DateTime.Now
}
}
};
// Add area annotation to Annotator
annotator.Add(area);
// Save the final PDF to disk
annotator.Save("result.pdf");
}
}
}
}

In the previous section, we discussed the process of incorporating area annotation in PDF C#, along with a sample code example. The section also elaborated on how to configure the annotation package. Once you have installed the annotation library and adjusted the file paths as required, integrating the code example into your applications should be a straightforward and hassle-free task. Congratulations on gaining knowledge about how to add area annotations to PDF documents.

An article we published recently about adding watermark into Excel document, have a look at how to add watermark to Excel worksheets using C# guide for more information.

 English