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#
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
- Create a new PDFContext object with width and height
- Call method PDFContext.DrawRectangle() to draw a rectangle with border color and position on PDFContext
- 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);
document.Add(figure);
...
Add context to PDF document
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 rectangle on PDF using C#: