How to add free text annotation to PDF in C#, VB.NET

Free text annotations are often being used for adding text notes to PDF pages. Unlike regular text annotation, free text annotation displays the text directly on page and allows to custom its appearance with unique styling. In this article, we'll show you how to create a customized free text annotation in PDF using Spire.PDF.

Code Snippet:

Step 1: Create an object of PdfDocument class, add a blank page in it.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Initialize a new instance of PdfFreeTextAnnotation, specifying the contents of the annotation.

RectangleF rect = new RectangleF(0, 40, 150, 50);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
textAnnotation.Text = "Free Text Annotation";

Step 3: Set the properties of the annotation including font name, fill color, border color, opacity, etc.

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
textAnnotation.Font = font;
textAnnotation.Border = border;
textAnnotation.BorderColor = Color.Purple;
textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
textAnnotation.Color = Color.Green;
textAnnotation.Opacity = 0.8f;

Step 4: Add the annotation to page.

page.AnnotationsWidget.Add(textAnnotation);

Step 5: Save the file.

doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);

Output:

How to add free text annotation to PDF in C#, VB.NET

Full Code:

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


namespace FreeTextAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            //create a new PDF document
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();
            //initialize a PdfFreeTextAnnotation
            RectangleF rect = new RectangleF(0, 40, 150, 50);
            PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
            //specify content
            textAnnotation.Text = "Free Text Annotation";
            //set properties of annotation
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
            PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
            textAnnotation.Font = font;
            textAnnotation.Border = border;
            textAnnotation.BorderColor = Color.Purple;
            textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
            textAnnotation.Color = Color.Green;
            textAnnotation.Opacity = 0.8f;
            //add annotation to page
            page.AnnotationsWidget.Add(textAnnotation);
            //save file
            doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace FreeTextAnnotation
	Class Program
		Private Shared Sub Main(args As String())
			'create a new PDF document
			Dim doc As New PdfDocument()
			Dim page As PdfPageBase = doc.Pages.Add()
			'initialize a PdfFreeTextAnnotation
			Dim rect As New RectangleF(0, 40, 150, 50)
			Dim textAnnotation As New PdfFreeTextAnnotation(rect)
			'specify content
			textAnnotation.Text = "Free Text Annotation"
			'set properties of annotation
			Dim font As New PdfFont(PdfFontFamily.TimesRoman, 10)
			Dim border As New PdfAnnotationBorder(1F)
			textAnnotation.Font = font
			textAnnotation.Border = border
			textAnnotation.BorderColor = Color.Purple
			textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle
			textAnnotation.Color = Color.Green
			textAnnotation.Opacity = 0.8F
			'add annotation to page
			page.AnnotationsWidget.Add(textAnnotation)
			'save file
			doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF)
			System.Diagnostics.Process.Start("FreeTextAnnotation.pdf")
		End Sub
	End Class
End Namespace