How to draw a rectangle in PDF document
The example demonstrates how to draw rectangles using ByteScout PDF SDK.
Program.vb:
VB
'******************************************************************* ' ByteScout PDF SDK ' ' Copyright © 2016 Bytescout, https://bytescout.com ' ALL RIGHTS RESERVED ' '******************************************************************* Imports System.Drawing Imports Bytescout.PDF ''' <summary> ''' This example demonstrates how to draw rectangles. ''' </summary> Class Program Shared Sub Main() ' Create new document Dim pdfDocument = New Document() pdfDocument.RegistrationName = "demo" pdfDocument.RegistrationKey = "demo" ' Add page Dim page = New Page(PaperFormat.A4) pdfDocument.Pages.Add(page) Dim canvas = page.Canvas ' Prepare pens and brushes Dim borderPen = New SolidPen(New ColorGray(128), 2.0F) Dim brush1 = New Bytescout.PDF.SolidBrush(New ColorRGB(255, 0, 0)) Dim brush2 = New Bytescout.PDF.SolidBrush(New ColorRGB(0, 255, 255)) ' Draw transparent rectangle with border only canvas.DrawRectangle(borderPen, 100, 100, 100, 50) ' Draw rounded rectangle with broder and filling canvas.DrawRoundedRectangle(borderPen, brush1, 250, 100, 100, 50, 10) ' Draw rectangle as polygon canvas.DrawPolygon(borderPen, brush2, New PointF() {New PointF(400, 100), New PointF(500, 100), New PointF(500, 150), New PointF(400, 150)}) ' Save document to file pdfDocument.Save("result.pdf") ' Cleanup pdfDocument.Dispose() ' Open document in default PDF viewer app Process.Start("result.pdf") End Sub End Class
Program.cs:
C#
//******************************************************************* // ByteScout PDF SDK // // Copyright © 2016 ByteScout - https://bytescout.com // ALL RIGHTS RESERVED // //******************************************************************* using System.Diagnostics; using System.Drawing; using Bytescout.PDF; using Brush = Bytescout.PDF.Brush; using Pen = Bytescout.PDF.Pen; using SolidBrush = Bytescout.PDF.SolidBrush; namespace Rectangles { /// <summary> /// This example demonstrates how to draw rectangles. /// </summary> class Program { static void Main() { // Create new document Document pdfDocument = new Document(); pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; // Add page Page page = new Page(PaperFormat.A4); pdfDocument.Pages.Add(page); Canvas canvas = page.Canvas; // Prepare pens and brushes Pen borderPen = new SolidPen(new ColorGray(128), 2f); Brush brush1 = new SolidBrush(new ColorRGB(255, 0, 0)); Brush brush2 = new SolidBrush(new ColorRGB(0, 255, 255)); // Draw transparent rectangle with border only canvas.DrawRectangle(borderPen, 100, 100, 100, 50); // Draw rounded rectangle with broder and filling canvas.DrawRoundedRectangle(borderPen, brush1, 250, 100, 100, 50, 10); // Draw rectangle as polygon canvas.DrawPolygon(borderPen, brush2, new PointF[] { new PointF(400, 100), new PointF(500, 100), new PointF(500, 150), new PointF(400, 150) }); // Save document to file pdfDocument.Save("result.pdf"); // Cleanup pdfDocument.Dispose(); // Open document in default PDF viewer app Process.Start("result.pdf"); } } }