PDF annotations are the additional objects used to mark up the content in PDFs. Since PDF files are not easily editable, the annotations are used to add notes about the content. The PDF annotations include comments, popups, and various other graphical objects. In this article, you will learn how to work with PDF annotations programmatically. Particularly, we will cover how to add and remove annotations in PDF files using C#.
C# API to Add PDF Annotations – Free Download
In order to work with PDF documents from within your .NET applications, Aspose provides Aspose.PDF for .NET. Using the API, you can create new as well as manipulate existing PDF files seamlessly. Furthermore, the API also allows you to add and remove PDF annotations. You can either download the API or install it within your .NET applications using NuGet.
PM> Install-Package Aspose.Pdf
Add Annotations to PDF using C#
PDF format supports various types of annotations such as text, line, circle, square, redaction and etc. In order to work with each PDF annotation, Aspose.PDF for .NET provides separate classes. For example, LineAnnotation class is used for adding a line whereas HighlightAnnotation class is used to add a highlight annotation. Let’s have a look at the steps of adding any type of annotation to a PDF.
Steps to Add Annotation in PDF using C#
- Load the PDF file using Document class.
- Create the instance of the desired annotation class i.e. HighlightAnnotation or LineAnnotation.
- Set the properties of the annotation such as position, color, size and etc.
- Add annotation to the Annotations collection of the particular PDF page using Document.Pages[1].Annotations.Add(Annotation) method.
- Save the PDF document using Document.Save(String) method.
For demonstration, the following code sample shows how to add a line annotation to the PDF using C#.
For the code samples of other annotation types, you can visit the following documentation articles:
- Free text Annotation
- Link Annotation
- Line Annotation
- Square and Circle Annotations
- Polygon and Polyline Annotations
- Text Markup Annotation
- Widget Annotation
- Caret Annotation
- Ink Annotation
- Popup Annotation
- Multimedia Annotation
- Watermark Annotation
Remove Annotations from PDF in C#
You can also remove the PDF annotations from existing PDF documents using Aspose.PDF for .NET. You can either remove all or a particular annotation from a PDF. The following are the steps to remove PDF annotations.
- Load the PDF file using Document class.
- Delete all annotations from a particular page using Document.Pages[index].Annotations.Delete() method.
- Or delete a particular annotation using Document.Pages[index].Annotations.Delete(Int index) or Document.Pages[index].Annotations.Delete(Annotation) methods.
- Save the document using Document.Save(String) method.
The following code sample shows how to remove annotations from a PDF file using C#.
Conclusion
PDF annotations are used to annotate the documents in order to insert text, graphics, and multimedia objects. Furthermore, the annotations help you add additional information or explanation about the content. In accordance with that, this article covered how to add and remove annotations from PDF using C#. In case you want to explore more about working with PDF annotations, visit documentation.