Modify and Format Annotation in PDF in C#, VB.NET

Text annotation is frequently used in PDF for creating a comment or explaining more about an item. Using Spire.PDF you can create a text mark-up annotation, edit an existing annotation and delete specified or all annotations. This article is aimed to demonstrate how to modify and format an existing text annotation in a PDF document.

Test File:

Modify and Format Annotation in PDF in C#, VB.NET

Changes we want to make:

  • Modify the annotation text.
  • Change the color of the annotation icon.
  • Fix the annotation flag including its position, color, and type.

Code Snippet

Step 1: Create a new PDF document and load the test file.

PdfDocument pdf = new PdfDocument("test.pdf");

Step 2: Get the annotation from the document.

PdfAnnotation annotation = pdf.Pages[0].AnnotationsWidget[0];

Step 3: Set the text, border, color of the annotation and lock the annotation flag.

annotation.Text = "Revised annotation";
annotation.Border = new PdfAnnotationBorder(0.75f);
annotation.Color = new PdfRGBColor(Color.White);
annotation.Flags = PdfAnnotationFlags.Locked;

Step 4: Save and launch the file.

pdf.SaveToFile("result.pdf");
Process.Start("result.pdf");

Target Effect:

Modify and Format Annotation in PDF in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Diagnostics;
using System.Drawing;

namespace ModifyAndFormatAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument("test.pdf");
            PdfAnnotation annotation = pdf.Pages[0].AnnotationsWidget[0];

            annotation.Text = "Revised annotation";
            annotation.Border = new PdfAnnotationBorder(0.75f);
            annotation.Color = new PdfRGBColor(Color.White);
            annotation.Flags = PdfAnnotationFlags.Locked;

            pdf.SaveToFile("result.pdf");
            Process.Start("result.pdf");
        }

    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports Spire.Pdf.Graphics
Imports System.Diagnostics
Imports System.Drawing

Namespace ModifyAndFormatAnnotation
	Class Program
		Private Shared Sub Main(args As String())
			Dim pdf As New PdfDocument("test.pdf")
			Dim annotation As PdfAnnotation = pdf.Pages(0).AnnotationsWidget(0)

			annotation.Text = "Revised annotation"
			annotation.Border = New PdfAnnotationBorder(0.75F)
			annotation.Color = New PdfRGBColor(Color.White)
			annotation.Flags = PdfAnnotationFlags.Locked

			pdf.SaveToFile("result.pdf")
			Process.Start("result.pdf")
		End Sub

	End Class
End Namespace