How to draw rectangle with visible of existing text in PDF document
The Syncfusion Essential® PDF is a feature-rich and high-performance .NET PDF library that is used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can draw a rectangle with visible of existing text in the PDF document using C# and VB.NET.
Steps to draw a rectangle with visible of existing text in PDF document using C# programmatically
1. Create a new C# console application project.
2. Install the Syncfusion.PDF.WinForms NuGet packages as a reference to your .NET Framework application from NuGet.org.
3. Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics
4. The following code example shows how to draw a rectangle with visible of existing text in the PDF document using C#.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Add a page to the document PdfPage page = document.Pages.Add(); //Set the standard font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Draw the text. page.Graphics.DrawString("Welcome to Syncfusion", font, PdfBrushes.Black, new PointF(0, 0)); //Initialize PdfSolidBrush for drawing the rectangle PdfSolidBrush brush = new PdfSolidBrush(Color.LightBlue); //Set the bounds for rectangle RectangleF bounds = new RectangleF(0, 0, 300, 300); //Set the page Transparency page.Graphics.Save(); page.Graphics.SetTransparency(1, 1, PdfBlendMode.Multiply); //Draw the rectangle on the PDF document page.Graphics.DrawRectangle(brush, bounds); //Save and closes the PDF document document.Save("Sample.pdf"); document.Close(true); Process.Start("Sample.pdf");
VB.NET
'Create a New PDF document Dim document As PdfDocument = New PdfDocument() 'Add a page to the document Dim Page As PdfPage = document.Pages.Add() 'Set the standard font. Dim Font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20) 'Draw the text. Page.Graphics.DrawString("Welcome to Syncfusion", Font, PdfBrushes.Black, New PointF(0, 0)) 'Initialize PdfSolidBrush for drawing the rectangle Dim Brush As PdfSolidBrush = New PdfSolidBrush(Color.LightBlue) 'Set the bounds for rectangle Dim bounds As RectangleF = New RectangleF(0, 0, 300, 300) 'Set the page Transparency Page.Graphics.Save() Page.Graphics.SetTransparency(1, 1, PdfBlendMode.Multiply) 'Draw the rectangle on the PDF document Page.Graphics.DrawRectangle(Brush, bounds) 'Save And closes the PDF document document.Save("Sample.pdf") document.Close(True) Process.Start("Sample.pdf")
By executing the program, you will get the PDF document as follows.
A complete working sample can be downloaded from PdfSample.zip.
Take a moment to peruse the documentation. You can find the options like add a polygon with a linear gradient brush and shape with pagination.
Refer to here to explore a rich set of Syncfusion Essential® PDF features.
An online sample link to drawing the shapes such as Ellipse, Arcs, Polygon, Pie, and Rectangle in the PDF document with gradient and color spaces.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.
See Also:
https://help.syncfusion.com/file-formats/pdf/working-with-text
https://help.syncfusion.com/file-formats/pdf/working-with-shapes
https://support.syncfusion.com/kb/article/8399/how-to-draw-shapes-in-a-pdf-using-c-and-vb-net