News Category

Add a Polygon Annotation to PDF in C#/VB.NET

2016-12-30 08:33:14 Written by  support iceblue
Rate this item
(0 votes)

Polygon annotations are displayed as a series of connected vertices and can be double-clicked to display its note. Users can customize the shape of polygon by setting the vertex coordinates. This article will introduce how to add a polygon annotation to PDF using Spire.PDF.

Step 1: Create a PDF document object, add a new page.

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

Step 2: Initialize an instance of PdfPolygonAnnotation, specifying all vertex coordinates which can form a complete shape.

PdfPolygonAnnotation polygon = new PdfPolygonAnnotation(page, new PointF[] { new PointF(0, 30), new PointF(30, 15), new PointF(60, 30),
new PointF(45, 50), new PointF(15, 50), new PointF(0, 30)});

Step 3: Set the border color, text, border effect and other properties of polygon annotation.

polygon.Color = Color.PaleVioletRed;
polygon.Text = "This is a polygon annotation";
polygon.Author = "E-ICEBLUE";
polygon.Subject = "polygon annotation demo";
polygon.BorderEffect = PdfBorderEffect.BigCloud;
polygon.ModifiedDate = DateTime.Now;

Step 4: Add the annotation to PDF page and save the document.

page.Annotations.Add(polygon);
pdf.SaveToFile("Polygon_Annotation.pdf");

Result:

How to Add a Polygon Annotation to PDF in C#, VB.NET

Full Code:

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


namespace PolygonAnnotation
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            PdfPolygonAnnotation polygon = new PdfPolygonAnnotation(page, new PointF[] { new PointF(0, 30), new PointF(30, 15), new PointF(60, 30),
    new PointF(45, 50), new PointF(15, 50), new PointF(0, 30)});
            polygon.Color = Color.PaleVioletRed;
            polygon.Text = "This is a polygon annotation";
            polygon.Author = "E-ICEBLUE";
            polygon.Subject = "polygon annotation demo";
            polygon.BorderEffect = PdfBorderEffect.BigCloud;
            polygon.ModifiedDate = DateTime.Now;

            page.Annotations.Add(polygon);
            pdf.SaveToFile("Polygon_Annotation.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports System.Drawing


Namespace PolygonAnnotation
	Class Program
		Private Shared Sub Main(args As String())
			Dim pdf As New PdfDocument()
			Dim page As PdfPageBase = pdf.Pages.Add()

			Dim polygon As New PdfPolygonAnnotation(page, New PointF() {New PointF(0, 30), New PointF(30, 15), New PointF(60, 30), New PointF(45, 50), New PointF(15, 50), New PointF(0, 30)})
			polygon.Color = Color.PaleVioletRed
			polygon.Text = "This is a polygon annotation"
			polygon.Author = "E-ICEBLUE"
			polygon.Subject = "polygon annotation demo"
			polygon.BorderEffect = PdfBorderEffect.BigCloud
			polygon.ModifiedDate = DateTime.Now

			page.Annotations.Add(polygon)
			pdf.SaveToFile("Polygon_Annotation.pdf")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Add a Polygon Annotation to PDF in C#, VB.NET
Last modified on Monday, 04 March 2024 07:29