Annotations allow users to add comments, reminders, or additional information to specific sections of the document. They enhance collaboration and communication in working with PDFs, enabling users to annotate, comment on, and provide context for shared content.


Get started with IronPDF!

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Add Annotations Example

PDF annotations allow the addition of "sticky note"-like comments to PDF pages. By using the Add method of the Annotations property, annotations can be programmatically added.

Tips
All page indexes follow zero-based indexing.

:path=/static-assets/pdf/content-code-examples/how-to/annotation-add-annotation.cs
// Import the necessary namespaces for working with the IronPdf library
using IronPdf;
using IronPdf.Annotations;

// Create an instance of ChromePdfRenderer to create PDFs from HTML content
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render an HTML string as a PDF document
// This example renders a simple h1 HTML element as a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>");

// Create a text annotation object to add a 'sticky note' like comment on the specified page index
TextAnnotation annotation = new TextAnnotation(0) // Page index 0, representing the first page
{
    // Set the title that will appear on the annotation note
    Title = "This is the title",
    // Define the main content or body of the annotation note
    Contents = "This is the long 'sticky note' comment content...",
    // Specify the location (X, Y coordinates) of the annotation on the page
    X = 50, // X-coordinate position from the left (in points)
    Y = 700 // Y-coordinate position from the bottom (in points)
};

// Add the created annotation to the annotations collection of the PDF document
pdf.Annotations.Add(annotation);

// Save the PDF with the annotated text to a specific file
// The saved file will be named 'annotation.pdf' in the working directory
pdf.SaveAs("annotation.pdf");
' Import the necessary namespaces for working with the IronPdf library

Imports IronPdf

Imports IronPdf.Annotations



' Create an instance of ChromePdfRenderer to create PDFs from HTML content

Private renderer As New ChromePdfRenderer()



' Render an HTML string as a PDF document

' This example renders a simple h1 HTML element as a PDF

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>")



' Create a text annotation object to add a 'sticky note' like comment on the specified page index

Private annotation As New TextAnnotation(0) With {

	.Title = "This is the title",

	.Contents = "This is the long 'sticky note' comment content...",

	.X = 50,

	.Y = 700

}



' Add the created annotation to the annotations collection of the PDF document

pdf.Annotations.Add(annotation)



' Save the PDF with the annotated text to a specific file

' The saved file will be named 'annotation.pdf' in the working directory

pdf.SaveAs("annotation.pdf")
$vbLabelText   $csharpLabel

PDF with an Annotation

The annotation in the PDF document above can be viewed with the Chrome browser.


Retrieve and Edit Annotations Example

Retrieving and editing PDF annotations improves collaboration by enhancing clarity, accuracy, and usability. Access the annotation collection through the Annotations property and update properties such as Title, Contents, X, Y, and more with new information.

:path=/static-assets/pdf/content-code-examples/how-to/annotation-edit-annotation.cs
using IronPdf;
using IronPdf.Annotations;
using System;
using System.Linq;

// Load an existing PDF document that contains annotations
PdfDocument pdf = PdfDocument.FromFile("annotation.pdf");

// Retrieve the collection of annotations from the PDF document
PdfAnnotationCollection annotationCollection = pdf.Annotations;

// Ensure that there is at least one annotation in the collection
if (annotationCollection.Any())
{
    // Select the first annotation in the collection and cast it to a TextAnnotation
    TextAnnotation annotation = annotationCollection.First() as TextAnnotation;

    // Check if the annotation is successfully cast to a TextAnnotation
    if (annotation != null)
    {
        // Edit the annotation's properties
        annotation.Title = "New title"; // Set a new title for the annotation
        annotation.Contents = "New content..."; // Set new contents for the annotation
        annotation.X = 150; // Set the X-coordinate for the position of the annotation
        annotation.Y = 800; // Set the Y-coordinate for the position of the annotation

        // Save the modified PDF document with a new name
        pdf.SaveAs("editedAnnotation.pdf");

        // Output a confirmation message about the successful modification
        Console.WriteLine("The PDF annotation has been successfully edited and saved as 'editedAnnotation.pdf'.");
    }
    else
    {
        // Output a message if the annotation is not of the type TextAnnotation
        Console.WriteLine("The first annotation is not a text annotation.");
    }
}
else
{
    // Output a message if there are no annotations to edit
    Console.WriteLine("No annotations found in the PDF document.");
}
Imports IronPdf

Imports IronPdf.Annotations

Imports System

Imports System.Linq



' Load an existing PDF document that contains annotations

Private pdf As PdfDocument = PdfDocument.FromFile("annotation.pdf")



' Retrieve the collection of annotations from the PDF document

Private annotationCollection As PdfAnnotationCollection = pdf.Annotations



' Ensure that there is at least one annotation in the collection

If annotationCollection.Any() Then

	' Select the first annotation in the collection and cast it to a TextAnnotation

	Dim annotation As TextAnnotation = TryCast(annotationCollection.First(), TextAnnotation)



	' Check if the annotation is successfully cast to a TextAnnotation

	If annotation IsNot Nothing Then

		' Edit the annotation's properties

		annotation.Title = "New title" ' Set a new title for the annotation

		annotation.Contents = "New content..." ' Set new contents for the annotation

		annotation.X = 150 ' Set the X-coordinate for the position of the annotation

		annotation.Y = 800 ' Set the Y-coordinate for the position of the annotation



		' Save the modified PDF document with a new name

		pdf.SaveAs("editedAnnotation.pdf")



		' Output a confirmation message about the successful modification

		Console.WriteLine("The PDF annotation has been successfully edited and saved as 'editedAnnotation.pdf'.")

	Else

		' Output a message if the annotation is not of the type TextAnnotation

		Console.WriteLine("The first annotation is not a text annotation.")

	End If

Else

	' Output a message if there are no annotations to edit

	Console.WriteLine("No annotations found in the PDF document.")

End If
$vbLabelText   $csharpLabel

PDF with an Edited Annotation

The annotation in the PDF document above can be viewed with the Chrome browser.


Remove Annotation Example

Easily remove unnecessary or outdated annotations using the following methods: RemoveAt, RemoveAllAnnotationsForPage, and Clear.

  • RemoveAt: Remove a single annotation with a specified index.
  • RemoveAllAnnotationsForPage: Remove all annotations on a specified page.
  • Clear: Remove all annotations in the document.

Remove a Single Annotation

To remove a single annotation, use the RemoveAt method with the corresponding index based on the annotation collection index.

:path=/static-assets/pdf/content-code-examples/how-to/annotation-remove-single-annotation.cs
using IronPdf;

// Load the PDF document from the specified file path
PdfDocument pdf = PdfDocument.FromFile("multipleAnnotation.pdf");

// Check if there are enough annotations to remove the one at index 1
if (pdf.Annotations.Count > 1)
{
    // Remove a single annotation from the loaded PDF document using the specified index
    // Here, we assume that the annotations list is zero-indexed, hence the second annotation is removed
    pdf.Annotations.RemoveAt(1);
}
else
{
    // Log or handle the scenario where there are not enough annotations to remove
    Console.WriteLine("The PDF does not contain enough annotations to remove one at index 1.");
}

// Save the modified PDF to a new file with the specified path
pdf.SaveAs("removeSingleAnnotation.pdf");
Imports IronPdf



' Load the PDF document from the specified file path

Private pdf As PdfDocument = PdfDocument.FromFile("multipleAnnotation.pdf")



' Check if there are enough annotations to remove the one at index 1

If pdf.Annotations.Count > 1 Then

	' Remove a single annotation from the loaded PDF document using the specified index

	' Here, we assume that the annotations list is zero-indexed, hence the second annotation is removed

	pdf.Annotations.RemoveAt(1)

Else

	' Log or handle the scenario where there are not enough annotations to remove

	Console.WriteLine("The PDF does not contain enough annotations to remove one at index 1.")

End If



' Save the modified PDF to a new file with the specified path

pdf.SaveAs("removeSingleAnnotation.pdf")
$vbLabelText   $csharpLabel

Removed a Single Annotation on PDF

The annotation in the PDF document above can be viewed with the Chrome browser.

Remove All Annotations

To remove all annotations on a particular page, use the RemoveAllAnnotationsForPage method and specify the page index. If you want to remove all annotations in the entire document, simply call the Clear method on the Annotations property.

:path=/static-assets/pdf/content-code-examples/how-to/annotation-remove-all-annotation.cs
using IronPdf;

// Load a PDF document from a file
// This creates a PdfDocument object from the specified PDF file.
PdfDocument pdf = PdfDocument.FromFile("multipleAnnotation.pdf");

// Remove all annotations on the first page (index 0) of the PDF document.
// Page indexes are zero-based, which means the first page is at index 0.
// This line removes any annotations present on the first page only.
pdf.Annotations.RemoveAllAnnotationsForPage(0);

// Remove all annotations from the entire document.
// This operation removes every annotation from every page of the PDF.
// After executing this, the document will have no annotations left.
pdf.Annotations.Clear();

// Save the modified PDF document to a new file.
// This overwrites any existing file with the same name ("removeAllAnnotation.pdf").
pdf.SaveAs("removeAllAnnotation.pdf");
Imports IronPdf



' Load a PDF document from a file

' This creates a PdfDocument object from the specified PDF file.

Private pdf As PdfDocument = PdfDocument.FromFile("multipleAnnotation.pdf")



' Remove all annotations on the first page (index 0) of the PDF document.

' Page indexes are zero-based, which means the first page is at index 0.

' This line removes any annotations present on the first page only.

pdf.Annotations.RemoveAllAnnotationsForPage(0)



' Remove all annotations from the entire document.

' This operation removes every annotation from every page of the PDF.

' After executing this, the document will have no annotations left.

pdf.Annotations.Clear()



' Save the modified PDF document to a new file.

' This overwrites any existing file with the same name ("removeAllAnnotation.pdf").

pdf.SaveAs("removeAllAnnotation.pdf")
$vbLabelText   $csharpLabel

Frequently Asked Questions

How do I add annotations to a PDF using a C# library?

To add annotations, use IronPDF by first downloading the C# Library for PDF Annotations. Load an existing or new PDF, and use the 'Add' method of the Annotations property to insert annotations.

How can I retrieve and edit annotations in a PDF?

Retrieve and edit annotations with IronPDF by accessing the Annotations property of a PDF page. Update properties such as Title and Contents to modify the annotation.

What methods can I use to remove annotations from a PDF?

With IronPDF, you can use the 'RemoveAt' method to remove a single annotation, 'RemoveAllAnnotationsForPage' to remove all annotations from a page, or 'Clear' to remove all annotations from the document.

Is it possible to edit the properties of an annotation?

Yes, with IronPDF, you can edit properties like Title, Contents, X, and Y of an annotation using the Annotations property of the PDF page.

Can annotations be viewed using a browser?

Yes, annotations added to PDF documents with IronPDF can be viewed using browsers like Chrome.

What are some use cases for adding annotations to PDFs?

Annotations, when added using IronPDF, are useful for adding comments, reminders, or additional information to specific sections of a PDF, enhancing collaboration and communication.

Where can I find documentation for IronSecureDoc?

You can explore the IronSecureDoc documentation at https://ironsoftware.com/enterprise/securedoc/docs/.

Chaknith related to Remove All Annotations
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.