Add a Line Annotation to PDF in C#

Line annotation appears as a straight line on PDF page, we can use it to insert text or give directions to readers. When moving onto the line annotation, it will display a pop up window with the comment text we added. This article introduces how to add a line annotation to PDF document programmatically using Spire.PDF and C#.

Code snippet:

Step 1: Create a PDF document and add a new page.

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

Step 2: Create a line annotation using the PdfLineAnnotation(int[] linePoints, string text) constructor.

int[] linePoints = new int[] { 100, 450, 180, 450 };
PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(linePoints, "Comment Text");

Step 3: Set the properties of the line annotation.

//Set the line border
lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid;
lineAnnotation.lineBorder.BorderWidth = 1;

//Set the line intent
lineAnnotation.LineIntent = PdfLineIntent.LineDimension;

//Set the line style
lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Butt;
lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond;

lineAnnotation.Flags = PdfAnnotationFlags.Default;

//Set the line color
lineAnnotation.InnerLineColor = new PdfRGBColor(Color.Green);
lineAnnotation.BackColor = new PdfRGBColor(Color.Green);

//Set the leader line
lineAnnotation.LeaderLineExt = 0;
lineAnnotation.LeaderLine = 0;

Step 4: Add the line annotation to the page.

page.AnnotationsWidget.Add(lineAnnotation);

Step 5: Save the document.

document.SaveToFile("LineAnnotation.pdf");

Screenshot:

Add a Line Annotation to PDF in C#

Full code:

using Spire.Pdf;
using Spire.Pdf.Annotations;
using System.Drawing;
using Spire.Pdf.Graphics;
using Spire.Pdf.Fields;

namespace Add_line_annotation_to_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PDF document and add a new page
            PdfDocument document = new PdfDocument();
            PdfPageBase page = document.Pages.Add();

            int[] linePoints = new int[] { 100, 450, 180, 450 };
            //Create a line annotation
            PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(linePoints, "Comment Text");

            //Set the line border
            lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid;
            lineAnnotation.lineBorder.BorderWidth = 1;

            //Set the line intent
            lineAnnotation.LineIntent = PdfLineIntent.LineDimension;

            //Set the line style
            lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Butt;
            lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond;

            lineAnnotation.Flags = PdfAnnotationFlags.Default;

            //Set the line color
            lineAnnotation.InnerLineColor = new PdfRGBColor(Color.Green);
            lineAnnotation.BackColor = new PdfRGBColor(Color.Green);

            //Set the leader line
            lineAnnotation.LeaderLineExt = 0;
            lineAnnotation.LeaderLine = 0;

            //Add the line annotation to the page
            page.AnnotationsWidget.Add(lineAnnotation);

            //Save the document
            document.SaveToFile("LineAnnotation.pdf");
        }
    }
}