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

Using C# PDF Drawing Library
C#: How to draw, add a rectangle shape to existing PDF file in ASP.NET, Windows app

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

In this C# tutorial, you learn how to draw, add rectangle paths to PDF file using C# in ASP.NET MVC Web, Windows applications.

  • Draw rectangles on PDF file
  • Support rectangle border color and width

How to draw rectangles on PDF file using C#

  1. Download XDoc.PDF Drawing C# library
  2. Install C# library to draw rectangles on PDF document
  3. Step by Step Tutorial

Draw rectangles on PDF context in C#

Below are the steps and C# sample code to draw and add rectangles on PDF document using C#.

Steps for drawing rectangles on a PDF Document

  1. Create a new PDFContext object with width and height
  2. Call method PDFContext.DrawRectangle() to draw a rectangle with border color and position on PDFContext
  3. Add PDFContext object to PDFDocument object

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 2 rectangles
ctx.DrawRectangle(new REPen(new REColor(0, 255, 0)), 250, 10, 100, 80);
ctx.DrawRectangle(new REPen(new REColor(0, 255, 255), 3F), 200, 20, 300, 60);
Figure figure = new Figure(ctx);

Add context to PDF document

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

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

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