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

C# code sample

1 Document doc = new Document(); 2 Page page = new Page(PageSize.A4); 3 doc.Pages.Add(page); 4 5 ShapeCollection shapes = new ShapeCollection(page.Width, page.Height); 6 7 //The tiles will be 10*10 p so we will create in each tiling 8 //two dots located at (2.5,2.5) and at (7.5,7.5) so the dots are equally spaced. 9 Pen p = new Pen(System.Drawing.Color.Black, 0.5); 10 Brush brush = new SolidBrush(); 11 EllipseShape dot1 = new EllipseShape(2.5, 2.5, 0.5, 0.5, p, brush); 12 EllipseShape dot2 = new EllipseShape(7.5, 7.5, 0.5, 0.5, p, brush); 13 14 //create a pattern of 10*10 using the 2 dots. 15 TilingBrush tiling = new TilingBrush(); 16 tiling.Pattern.Width = 10; 17 tiling.Pattern.Height = 10; 18 tiling.Pattern.Add(dot1); 19 tiling.Pattern.Add(dot2); 20 21 //create a rectangleshape using the specified tiling as a filling 22 shapes.Add(new RectangleShape(250, page.Height - 200, 150, 150, new Pen(System.Drawing.Color.Black), tiling)); 23 page.Overlay.Add(shapes); 24 25 //write PDF to output 26 using (FileStream fs = new FileStream("output.pdf", FileMode.Create, FileAccess.Write)) 27 { 28 doc.Write(fs); 29 }

VB.NET code sample

1 Dim doc As New Document() 2 Dim page As New Page(PageSize.A4) 3 doc.Pages.Add(page) 4 5 Dim shapes As New ShapeCollection(page.Width, page.Height) 6 7 'The tiles will be 10*10 p so we will create in each tiling 8 'two dots located at (2.5,2.5) and at (7.5,7.5) so the dots are equally spaced. 9 Dim p As New Pen(System.Drawing.Color.Black, 0.5) 10 Dim brush As Brush = New SolidBrush() 11 Dim dot1 As New EllipseShape(2.5, 2.5, 0.5, 0.5, p, brush) 12 Dim dot2 As New EllipseShape(7.5, 7.5, 0.5, 0.5, p, brush) 13 14 'create a pattern of 10*10 using the 2 dots. 15 Dim tiling As New TilingBrush() 16 tiling.Pattern.Width = 10 17 tiling.Pattern.Height = 10 18 tiling.Pattern.Add(dot1) 19 tiling.Pattern.Add(dot2) 20 21 'create a rectangleshape using the specified tiling as a filling 22 shapes.Add(New RectangleShape(250, page.Height - 200, 150, 150, New Pen(System.Drawing.Color.Black), tiling)) 23 page.Overlay.Add(shapes) 24 25 'write PDF to output 26 Using fs As New FileStream("output.pdf", FileMode.Create, FileAccess.Write) 27 doc.Write(fs) 28 End Using