How to add a popup note to PDF in C#, VB.NET

Popup note is one type of annotations, which pops up a message that the document author leaves for the readers when moving cursor over the icon. The article aims at introducing how to add a popup note into PDF page at specified location in C# and VB.NET.

Code Snippet:

Step 1: Create a PDF document and add a page to it.

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

Step 2: Construct an object of PdfPopupAnnotation based a RectangleF, which represents the annotation position.

RectangleF rect = new RectangleF(0, 40, 20, 20);
PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect);

Step 3: Set properties of the annotation including text, icon type, and icon color.

popupAnnotation.Text = "Warning: No changes are allowed.";
popupAnnotation.Icon = PdfPopupIcon.Comment;
popupAnnotation.Color = Color.Red;

Step 4: Add the annotation to the page.

page.AnnotationsWidget.Add(popupAnnotation);

Step 5: Save the file.

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

Output:

How to add a popup note to PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

RectangleF rect = new RectangleF(0, 40, 20, 20);
PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect);
popupAnnotation.Text = "Warning: No changes are allowed.";
popupAnnotation.Icon = PdfPopupIcon.Comment;
popupAnnotation.Color = Color.Red;      
page.AnnotationsWidget.Add(popupAnnotation);

doc.SaveToFile("PopupAnnotation.pdf", FileFormat.PDF);
[VB.NET]
Dim doc As New PdfDocument()
Dim page As PdfPageBase = doc.Pages.Add()

Dim rect As New RectangleF(0, 40, 20, 20)
Dim popupAnnotation As New PdfPopupAnnotation(rect)
popupAnnotation.Text = "Warning: No changes are allowed."
popupAnnotation.Icon = PdfPopupIcon.Comment
popupAnnotation.Color = Color.Red
page.AnnotationsWidget.Add(popupAnnotation)

doc.SaveToFile("PopupAnnotation.pdf", FileFormat.PDF)