IronPDF How-Tos Draw Line & Rectangle How to Draw Lines and Rectangles on PDFs ByChaknith Bin January 7, 2024 Updated June 22, 2025 Share: Drawing lines and rectangles onto a PDF document refers to the process of adding geometric shapes, specifically lines and rectangles, to the content of a PDF file. This is often done programmatically using a programming language like C# or VB.NET and a library like IronPDF.When you draw a line, you create a visible line segment with specified starting and ending points. Similarly, when you draw a rectangle, you define a four-sided shape with specified dimensions and positions. View the IronPDF YouTube Playlist Get started with IronPDF Start using IronPDF in your project today with a free trial. First Step: Start for Free How to Draw Lines and Rectangles on PDFs in C# Download IronPDF C# Library from NuGet Import the target PDF document or render it from HTML Use the DrawLine Method for Adding Lines to PDFs Use the DrawRectangle Method for Adding Rectangles Export the edited PDF document Draw Line Example By utilizing the DrawLine method available for the PdfDocument object, you can add lines to an existing PDF. Using the Color class offered by IronDrawing API Documentation opens up the possibility to apply a line with a color from a HEX color code. :path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-line.cs using IronPdf; using IronSoftware.Drawing; // Initialize a renderer for generating PDF from HTML ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render HTML content as a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Configure the line drawing parameters int pageIndex = 0; // Zero-based index of the page on which to draw the line PointF start = new PointF(200, 150); // Starting coordinates of the line PointF end = new PointF(1000, 150); // Ending coordinates of the line int width = 10; // Width of the line in pixels Color color = ColorTranslator.FromHtml("#000000"); // Color of the line, specified in hexadecimal HTML format // Draw line on the specified PDF page pdf.DrawLine(pageIndex, start, end, width, color); // Save the PDF document to a file pdf.SaveAs("drawLine.pdf"); Imports IronPdf Imports IronSoftware.Drawing ' Initialize a renderer for generating PDF from HTML Private renderer As New ChromePdfRenderer() ' Render HTML content as a PDF document Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Configure the line drawing parameters Private pageIndex As Integer = 0 ' Zero-based index of the page on which to draw the line Private start As New PointF(200, 150) ' Starting coordinates of the line Private [end] As New PointF(1000, 150) ' Ending coordinates of the line Private width As Integer = 10 ' Width of the line in pixels Private color As Color = ColorTranslator.FromHtml("#000000") ' Color of the line, specified in hexadecimal HTML format ' Draw line on the specified PDF page pdf.DrawLine(pageIndex, start, [end], width, color) ' Save the PDF document to a file pdf.SaveAs("drawLine.pdf") $vbLabelText $csharpLabel Output PDF Draw Rectangle Example To add rectangles to PDFs, use the DrawRectangle method. Once the PDF document is opened or rendered, this method is available for the PdfDocument object. Easily configure the coordinates, width, and height for the rectangle with the RectangleF class offered by IronDrawing API Documentation. :path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-rectangle.cs using IronPdf; using IronSoftware.Drawing; // Instantiate a ChromePdfRenderer to render HTML content as a PDF. ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render simple HTML content as a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Configure parameters for drawing a rectangle on the PDF int pageIndex = 0; // The index of the page where the rectangle will be drawn. Zero-based index. var rectangle = new RectangleF(200, 100, 1000, 100); // The positioning and size of the rectangle. var lineColor = Color.FromHtml("#000000"); // The color of the rectangle's border in hex. var fillColor = Color.FromHtml("#32AB90"); // The fill color of the rectangle in hex. int lineWidth = 5; // The width of the rectangle's border. // Draw the configured rectangle onto the first page of the PDF document pdf.DrawRectangle(pageIndex, rectangle, lineColor, fillColor, lineWidth); // Save the updated PDF with the rectangle to a file. pdf.SaveAs("drawRectangle.pdf"); Imports IronPdf Imports IronSoftware.Drawing ' Instantiate a ChromePdfRenderer to render HTML content as a PDF. Private renderer As New ChromePdfRenderer() ' Render simple HTML content as a PDF document Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Configure parameters for drawing a rectangle on the PDF Private pageIndex As Integer = 0 ' The index of the page where the rectangle will be drawn. Zero-based index. Private rectangle = New RectangleF(200, 100, 1000, 100) ' The positioning and size of the rectangle. Private lineColor = Color.FromHtml("#000000") ' The color of the rectangle's border in hex. Private fillColor = Color.FromHtml("#32AB90") ' The fill color of the rectangle in hex. Private lineWidth As Integer = 5 ' The width of the rectangle's border. ' Draw the configured rectangle onto the first page of the PDF document pdf.DrawRectangle(pageIndex, rectangle, lineColor, fillColor, lineWidth) ' Save the updated PDF with the rectangle to a file. pdf.SaveAs("drawRectangle.pdf") $vbLabelText $csharpLabel Output PDF Frequently Asked Questions What is this library for creating and editing PDFs in C#? IronPDF is a C# library that allows developers to generate, edit, and manipulate PDF documents programmatically using .NET languages like C# and VB.NET. How do you draw a line on a PDF using C#? To draw a line on a PDF using C#, use the DrawLine method of the PdfDocument object in IronPDF. You need to specify the line color, starting and ending coordinates, and line thickness. How can you draw a rectangle on a PDF using C#? To draw a rectangle on a PDF using C#, use the DrawRectangle method of the PdfDocument object in IronPDF. Define the rectangle's color, position, dimensions, and thickness using the RectangleF class. What is the DrawLine method? The DrawLine method is a function of the PdfDocument object in IronPDF that allows users to add lines to a PDF by specifying parameters like color, start and end positions, and thickness. What is the DrawRectangle method? The DrawRectangle method is a feature of the PdfDocument object in IronPDF that enables users to add rectangles to a PDF. Users can specify the color, position, size, and thickness of the rectangle. Where can I find the drawing API documentation for this library? The IronDrawing API Documentation can be found at https://ironsoftware.com/open-source/csharp/drawing/docs/. It provides detailed information on using drawing functionalities available in IronPDF. How do you define the color of a line or rectangle in this library? In IronPDF, you can define the color of a line or rectangle using the Color class in conjunction with the ColorTranslator.FromHtml method, where you can specify a HEX color code. What languages can be used with this PDF library? IronPDF primarily supports .NET languages such as C# and VB.NET, allowing developers to create and modify PDF documents programmatically. How can you save a PDF after editing it with this library? After editing a PDF with IronPDF, you can save it using the SaveAs method of the PdfDocument object, specifying the desired output file path. Chaknith Bin Chat with engineering team now Software Engineer Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking. Ready to Get Started? Free NuGet Download Total downloads: 14,143,061 View Licenses