Articles in this section
Category / Section

How to fill rectangle with striped colors in PDF document using C# and VB.NET in WinForms?

6 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can fill a rectangle with striped colors in a PDF document using C# and VB.NET.

Steps to fill a rectangle with striped colors in PDF document programmatically:

  1. Create a new C# console application project.

Create a new console application project

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.

NuGet package reference

  1. Include the following namespace in Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing

 

  1. Use the following code sample to fill a rectangle with striped colors in a PDF document.

C#

//Create a PDF document. 
PdfDocument document = new PdfDocument();
 
//Add a new page.
PdfPage page = document.Pages.Add();
 
//Create a template. 
PdfTemplate brush = new PdfTemplate(new RectangleF(0, 0, 200, 10));
brush.Graphics.SkewTransform(0, 20);
 
int count = (int)(brush.Width / ((brush.Height) * 2));
float x = -10;
 
//Draw a rectangle in the template.
for (int i = 0; i < count; i++)
{
    brush.Graphics.DrawRectangle(PdfBrushes.Red, new RectangleF(x, 0, brush.Width, brush.Height));
    brush.Graphics.DrawRectangle(PdfBrushes.Green, new RectangleF(x + brush.Height, 0, brush.Height, brush.Height));
    x += ((brush.Height) * 2);
}
 
//Draw a rectangle in the page graphics.
page.Graphics.DrawRectangle(PdfPens.Gold, new RectangleF(0, 50, 200, 10));
 
//Draw a template in the page graphics.
page.Graphics.DrawPdfTemplate(brush, new PointF(0, 50));
 
//Save the PDF document.
document.Save("Sample.pdf");
 
//Close the document. 
document.Close(true);
 
//This will open the PDF file and the result will be seen in the default PDF Viewer.
Process.Start("Sample.pdf");

 

VB.NET

'Create a PDF document. 
Dim document As PdfDocument = New PdfDocument()
 
'Add a new page. 
Dim page As PdfPage = document.Pages.Add()
 
'Create a template. 
Dim brush As PdfTemplate = New PdfTemplate(New RectangleF(0, 0, 200, 10))
brush.Graphics.SkewTransform(0, 20)
Dim count As Integer = CInt((brush.Width / ((brush.Height) * 2)))
Dim x As Single = -10
 
'Draw a rectangle in the template.
For i As Integer = 0 To count - 1
    brush.Graphics.DrawRectangle(PdfBrushes.Red, New RectangleF(x, 0, brush.Width, brush.Height))
    brush.Graphics.DrawRectangle(PdfBrushes.Green, New RectangleF(x + brush.Height, 0, brush.Height, brush.Height))
    x += ((brush.Height) * 2)
Next
 
'Draw a rectangle in the page graphics.
page.Graphics.DrawRectangle(PdfPens.Gold, New RectangleF(0, 50, 200, 10))
 
'Draw a template in the page graphics.
page.Graphics.DrawPdfTemplate(brush, New PointF(0, 50))
 
'Save the PDF document.
document.Save("Sample.pdf")
 
'Close the document. 
document.Close(True)
 
'This will open the PDF file and the result will be seen in the default PDF Viewer.
Process.Start("Sample.pdf")
 

 

A complete working sample can be download from AddShapeSample.zip.

 

By executing the program, you will get the output document as follows,

 

Output document screenshot

 

Take a moment to peruse the documentation. You can find the options of adding shapes like Polygon, Line, Curve, Path, Text, Rectangle, Pie, Arc, Bezier, Ellipse, and shape pagination.

 

Refer to this link to explore a rich set of Syncfusion Essential® PDF features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the 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 a trial message.

 

Also see:

Draw shapes in PDF document

Conclusion:

I hope you enjoyed learning about how to fill rectangle with striped colors in PDF document using C# and VB.NET in WinForms.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples 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 explore 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 forums 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