Articles in this section
Category / Section

How to draw rectangle with visible of existing text in PDF document

8 mins read

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.

Create Console application

2. Install the Syncfusion.PDF.WinForms NuGet packages as a reference to your .NET Framework application from NuGet.org.

Install NuGet packages

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.

output document

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.

 

Note:

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



Conclusion
I hope you enjoyed learning how to draw rectangle with visible of existing text in PDF document.
You can refer to our WinForms PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied