How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Drawing Library
How to show, display, add, delete, update PDF file drawings & shapes in C#.net, winform, wpf, asp.net


Sample Codes for C#.NET Users to draw shapes annotations on PDF in C#.NET Class. ASP.NET Annotate PDF function is based on C# PDF Annotate SDK.





In this C# tutorial, you will learn how to add, edit PDF path and shapes on PDF document using C# in ASP.NET MVC Web, Windows applications.

  • Quick to draw point, line, arrow, ellipse, rectangle, polygon, polyline shapes to an existing PDF file
  • Customize the shapes of PDF with position, start and end point, line color, line width, fill color





C# add path & shape annotation to pdf document


The C# example source code below will show how to draw, add point, line, arrow, ellipse, rectangle, polygon, polyline shapes to an existing PDF file using C#



String inputFilePath = Program.RootPath + "\\" + "2.pdf";
String outputFilePath = Program.RootPath + "\\" + "Annot_6.pdf";

//  open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);
//  get the 1st page
PDFPage page = (PDFPage)doc.GetPage(0);

{
    //  create the annotation
    PDFAnnotLine annot = new PDFAnnotLine();

    annot.Start = new PointF(100F, 100F);
    annot.End = new PointF(200F, 200F);

    //  add annotation to the page
    PDFAnnotHandler.AddAnnotation(page, annot);
}
{
    //  create the annotation
    PDFAnnotArrow annot = new PDFAnnotArrow();

    annot.Start = new PointF(300F, 100F);
    annot.End = new PointF(400F, 150F);

    //  add annotation to the page
    PDFAnnotHandler.AddAnnotation(page, annot);
}
{
    //  create the annotation
    PDFAnnotEllipse annot = new PDFAnnotEllipse();

    annot.Location = new PointF(300, 300);
    annot.Width = 50F;
    annot.Height = 50F;

    //  add annotation to the page
    PDFAnnotHandler.AddAnnotation(page, annot);
}
{
    //  create the annotation
    PDFAnnotRectangle annot = new PDFAnnotRectangle();

    annot.Location = new PointF(400, 400);
    annot.Width = 50F;
    annot.Height = 50F;

    //  add annotation to the page
    PDFAnnotHandler.AddAnnotation(page, annot);
}
{
    //  create the annotation
    PDFAnnotPolygon annot = new PDFAnnotPolygon();

    annot.Points = new PointF[5] {
        new PointF(50F, 450F),
        new PointF(100F, 450F),
        new PointF(120F, 480F),
        new PointF(100F, 500F),
        new PointF(50F, 500F)
    };

    //  add annotation to the page
    PDFAnnotHandler.AddAnnotation(page, annot);
}
{
    //  create the annotation
    PDFAnnotPolyline annot = new PDFAnnotPolyline();

    annot.Points = new PointF[] {
        new PointF(250F, 450F),
        new PointF(300F, 450F),
        new PointF(320F, 480F),
        new PointF(300F, 500F),
        new PointF(250F, 500F)
    };

    //  add annotation to the page
    PDFAnnotHandler.AddAnnotation(page, annot);
}

//  save to a new file
doc.Save(outputFilePath);




C# read path & shape annotation from pdf document


The C# source code below will explain how to read, extract path, shapes data from PDF, including position, start and end point, line color, line width, fill color.



String inputFilePath = Program.RootPath + "\\" + "Annot_6.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(doc);
foreach (IPDFAnnot annot in annots)
{
    if (annot is PDFAnnotLine)
    {
        PDFAnnotLine obj = (PDFAnnotLine)annot;
        Console.WriteLine("Line Annotation: ");
        Console.WriteLine("Start Point: " + obj.Start.ToString());
        Console.WriteLine("End Point: " + obj.End.ToString());
        Console.WriteLine("Line Color: " + obj.LineColor.ToString());
        Console.WriteLine("Line Width: " + obj.LineWidth);
    }
    else if (annot is PDFAnnotArrow)
    {
        PDFAnnotArrow obj = (PDFAnnotArrow)annot;
        Console.WriteLine("Arrow Annotation: ");
        Console.WriteLine("Start Point: " + obj.Start.ToString());
        Console.WriteLine("End Point: " + obj.End.ToString());
        Console.WriteLine("Line Color: " + obj.LineColor.ToString());
        Console.WriteLine("Line Width: " + obj.LineWidth);
    }
    else if (annot is PDFAnnotEllipse)
    {
        PDFAnnotEllipse obj = (PDFAnnotEllipse)annot;
        Console.WriteLine("Ellipse Annotation: ");
        Console.WriteLine("Location: " + obj.Location.ToString());
        Console.WriteLine("Width: " + obj.Width);
        Console.WriteLine("Height: " + obj.Height);
        Console.WriteLine("Line Color: " + obj.LineColor.ToString());
        Console.WriteLine("Line Width: " + obj.LineWidth);
        Console.WriteLine("Fill Color: " + obj.FillColor.ToString());
    }
    else if (annot is PDFAnnotRectangle)
    {
        PDFAnnotRectangle obj = (PDFAnnotRectangle)annot;
        Console.WriteLine("Rectangle Annotation: ");
        Console.WriteLine("Location: " + obj.Location.ToString());
        Console.WriteLine("Width: " + obj.Width);
        Console.WriteLine("Height: " + obj.Height);
        Console.WriteLine("Line Color: " + obj.LineColor.ToString());
        Console.WriteLine("Line Width: " + obj.LineWidth);
        Console.WriteLine("Fill Color: " + obj.FillColor.ToString());
    }
    else if (annot is PDFAnnotPolygon)
    {
        PDFAnnotPolygon obj = (PDFAnnotPolygon)annot;
        Console.WriteLine("Polygon Annotation: ");
        Console.WriteLine("End Points: ");
        foreach (PointF pt in obj.Points)
        {
            Console.WriteLine("  " + pt.ToString());
        }
        Console.WriteLine("Line Color: " + obj.LineColor.ToString());
        Console.WriteLine("Line Width: " + obj.LineWidth);
        Console.WriteLine("Fill Color: " + obj.FillColor.ToString());
    }
    else if (annot is PDFAnnotPolyline)
    {
        PDFAnnotPolyline obj = (PDFAnnotPolyline)annot;
        Console.WriteLine("Polyline Annotation: ");
        Console.WriteLine("End Points: ");
        foreach (PointF pt in obj.Points)
        {
            Console.WriteLine("  " + pt.ToString());
        }
        Console.WriteLine("Line Color: " + obj.LineColor.ToString());
        Console.WriteLine("Line Width: " + obj.LineWidth);
        Console.WriteLine("Fill Color: " + obj.FillColor.ToString());
    }
    else if (annot is PDFAnnotPen)
    {
        PDFAnnotPen obj = (PDFAnnotPen)annot;
        Console.WriteLine("Pen Annotation: ");
        Console.WriteLine("Points: ");
        foreach (PointF pt in obj.Points)
        {
            Console.WriteLine("  " + pt.ToString());
        }
        Console.WriteLine("Line Color: " + obj.LineColor.ToString());
        Console.WriteLine("Line Width: " + obj.LineWidth);
        Console.WriteLine("Path: " + obj.Path.ToString());
    }
}




C# delete all annotations from pdf document


String inputFilePath = Program.RootPath + "\\" + "1_Annots.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
//  get all annotations in the 1st page
PDFPage page = (PDFPage)doc.GetPage(0);
List<IPDFAnnot> annots = PDFAnnotHandler.GetAllAnnotations(page);
//  remove all annotations in the 1st page
PDFAnnotHandler.DeleteAnnotation(doc, annots);