How to create a tiling for shapes in PDF

Shapes
10/22/2015

Here we will show you how you can create a rectangle and fill it using a TilingBrush containing dots.

The pattern we will be using is in a 10p*10p area. To keep equal distance between the dots, we place them at (2.5,2.5) and at (7.5,7.5). Next we specify a TilingBrush and add the dots to it and finally we use the tiling brush when drawing the rectangle. The resulting rectangle can be viewed in the following image:

PDFKit-fill-rectangle-with-tiling.png

Document doc = new Document();
Page page = new Page(PageSize.A4);
doc.Pages.Add(page);

ShapeCollection shapes = new ShapeCollection(page.Width, page.Height);

//The tiles will be 10*10 p so we will create in each tiling
//two dots located at (2.5,2.5) and at (7.5,7.5) so the dots are equally spaced.
Pen p = new Pen(System.Drawing.Color.Black, 0.5);
Brush brush = new SolidBrush();
EllipseShape dot1 = new EllipseShape(2.5, 2.5, 0.5, 0.5, p, brush);
EllipseShape dot2 = new EllipseShape(7.5, 7.5, 0.5, 0.5, p, brush);

//create a pattern of 10*10 using the 2 dots.
TilingBrush tiling = new TilingBrush();
tiling.Pattern.Width = 10;
tiling.Pattern.Height = 10;
tiling.Pattern.Add(dot1);
tiling.Pattern.Add(dot2);

//create a rectangleshape using the specified tiling as a filling
shapes.Add(new RectangleShape(250, page.Height - 200, 150, 150, new Pen(System.Drawing.Color.Black), tiling));
page.Overlay.Add(shapes);

//write PDF to output
using (FileStream fs = new FileStream("output.pdf", FileMode.Create, FileAccess.Write))
{
    doc.Write(fs);
}
Dim doc As New Document()
Dim page As New Page(PageSize.A4)
doc.Pages.Add(page)

Dim shapes As New ShapeCollection(page.Width, page.Height)

'The tiles will be 10*10 p so we will create in each tiling
'two dots located at (2.5,2.5) and at (7.5,7.5) so the dots are equally spaced.
Dim p As New Pen(System.Drawing.Color.Black, 0.5)
Dim brush As Brush = New SolidBrush()
Dim dot1 As New EllipseShape(2.5, 2.5, 0.5, 0.5, p, brush)
Dim dot2 As New EllipseShape(7.5, 7.5, 0.5, 0.5, p, brush)

'create a pattern of 10*10 using the 2 dots.
Dim tiling As New TilingBrush()
tiling.Pattern.Width = 10
tiling.Pattern.Height = 10
tiling.Pattern.Add(dot1)
tiling.Pattern.Add(dot2)

'create a rectangleshape using the specified tiling as a filling
shapes.Add(New RectangleShape(250, page.Height - 200, 150, 150, New Pen(System.Drawing.Color.Black), tiling))
page.Overlay.Add(shapes)

'write PDF to output
Using fs As New FileStream("output.pdf", FileMode.Create, FileAccess.Write)
    doc.Write(fs)
End Using