XDoc.PDF
Features
Tech Specs
How-to C#
How-to VB.NET
Pricing
C# PDF: How to PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
Drawings and shapes
  |  
Home ›› XDoc.PDF ›› C# PDF: Drawings & Shapes

How to add, remove PDF drawings and shapes (line, arrow, ...) using C#.NET


Sample Codes for C#.NET Users to draw shapes annotations on PDF in C#.NET Class






C# add path & shape annotation to pdf document





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





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);