How to Start Convert PDF Read PDF Edit PDF PDF Report Builder 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

Using C# PDF Drawing Library
How to draw, add a line path to PDF file using C# in ASP.NET MVC, WinForms, WPF application


C# Demo Code to create, draw, add a line graph to pdf document





In this C# tutorial, you learn how to draw, add new lines to PDF file using C# in ASP.NET WebForm, MVC, Windows applications.

  • Draw lines on PDF file
  • Customize lines with color, star and end point





Draw a line on PDF context in C# code


In C# code below, you learn how to draw, add new lines to PDF file using C#

  • Define a PDFContext with specified width and height, where you can draw line shapes
  • Utilize method PDFContext.DrawLine() to draw lines on PDF context
  • Add the PDF context to the PDF document



...
PDFContext ctx = new PDFContext();
//  set figure size: width 600 pixels; height 100 pixels
ctx.SetWidth(new RELength(600, Units.PX));
ctx.SetHeight(new RELength(100, Units.PX));
//  draw a red line with 5px in width. 
ctx.DrawLine(new REPen(new REColor(255, 0, 0), 5F), 10, 10, 580, 80);
Figure figure = new Figure(ctx);
document.Add(figure);
...




Add context to PDF document in C#


String outputFilePath = Program.RootPath + "\\" + "Sample25.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);
//  open document
document.Open();
//  create Figure
PDFContext ctx = new PDFContext();
...
...
Figure figure = new Figure(ctx);
document.Add(figure);
//  close document and save to the output file
document.Close();


Let's see the result of adding a line on PDF using C#: