Create layers in PDF and draw on each layer

Generate PDF, Shapes
1/30/2014

Downloads

Create a new PDF document with different layers and draw on each layer. This code sample shows how to create a document and adds two new layers. We draw on each layer by creating a LayerShape for each layer and adding shapes.

C# Create layer in PDF

1 Document document = new Document(); 2 Page page = new Page(600, 600); 3 document.Pages.Add(page); 4 5 // create a Drawing and make it occupy all space within the margins of the page 6 ShapeCollection shapes = new ShapeCollection(100, 100, 400, 400); 7 page.Overlay.Add(shapes); 8 9 // Create layers 10 Layer gridLayer = new Layer("Grid"); 11 LayerShape gridLayerShape = new LayerShape(gridLayer); 12 document.Layers.Add(gridLayer); 13 14 Layer graphicsLayer = new Layer("Graphics"); 15 LayerShape graphicsLayerShape = new LayerShape(graphicsLayer); 16 document.Layers.Add(graphicsLayer); 17 18 shapes.Add(gridLayerShape); 19 shapes.Add(graphicsLayerShape);

VB.NET Create layer in PDF

1 Dim document As New Document() 2 Dim page As New Page(600, 600) 3 document.Pages.Add(page) 4 5 ' create a Drawing and make it occupy all space within the margins of the page 6 Dim shapes As New ShapeCollection(100, 100, 400, 400) 7 page.Overlay.Add(shapes) 8 9 ' Create layers 10 Dim gridLayer As New Layer("Grid") 11 Dim gridLayerShape As New LayerShape(gridLayer) 12 document.Layers.Add(gridLayer) 13 14 Dim graphicsLayer As New Layer("Graphics") 15 Dim graphicsLayerShape As New LayerShape(graphicsLayer) 16 document.Layers.Add(graphicsLayer) 17 18 shapes.Add(gridLayerShape) 19 shapes.Add(graphicsLayerShape)

Lets draw something on the layers.

C# draw on each layer

1 // coordinate (0, 0) lies at the bottom left of the shapes. 2 3 // draw a grid of 25 x 25 points in the GridLayer 4 Pen gridPen = new Pen(System.Drawing.Color.LightGray, 1); 5 for (double x = 0; x < shapes.Width; x += 25) 6 { 7 for (double y = 0; y < shapes.Height; y += 25) 8 { 9 // Draw the lines 10 gridLayerShape.Add(new LineShape(0, y, shapes.Width, y, gridPen)); 11 gridLayerShape.Add(new LineShape(x, 0, x, shapes.Height, gridPen)); 12 13 // Add axis numbering 14 gridLayerShape.Add(new TextShape(0, y, y.ToString(), Font.Helvetica, 8)); 15 gridLayerShape.Add(new TextShape(x, 0, x.ToString(), Font.Helvetica, 8)); 16 } 17 } 18 19 //Add shapes to the graphics layer 20 // add a line 21 graphicsLayerShape.Add(new LineShape(25, 25, 75, 50)); 22 23 // add an ellipse 24 graphicsLayerShape.Add(new EllipseShape(150, 50, 50, 25)); 25 26 // add a rectangle 27 graphicsLayerShape.Add(new RectangleShape(250, 25, 75, 50, new Pen(System.Drawing.Color.Black), new SolidBrush(System.Drawing.Color.Yellow))); 28 29 // draw one bezier without the helper lines 30 graphicsLayerShape.Add(new BezierShape(325, 150, 375, 200, 325, 100, 375, 150)); 31 32 // add a few arcshapes 33 graphicsLayerShape.Add(new ArcShape(50, 225, 25, 25)); 34 graphicsLayerShape.Add(new ArcShape(125, 225, 25, 25, 0, 45)); 35 graphicsLayerShape.Add(new ArcShape(200, 225, 25, 25, 0, 90)); 36 graphicsLayerShape.Add(new ArcShape(275, 225, 25, 25, 90, 180)); 37 38 // add a few pieshapes 39 graphicsLayerShape.Add(new PieShape(50, 300, 25, 25)); 40 graphicsLayerShape.Add(new PieShape(125, 300, 25, 25, 0, 45)); 41 graphicsLayerShape.Add(new PieShape(200, 300, 25, 25, 0, 45)); 42 graphicsLayerShape.Add(new PieShape(275, 300, 25, 25, 90, 270)); 43 44 // add a multiline textshape 45 MultilineTextShape multiLineText = new MultilineTextShape(); 46 multiLineText.Fragments.Add(new Fragment( 47 "This example show how you can create multiple layers using PDF Kit")); 48 multiLineText.HorizontalAlignment = HorizontalAlignment.Center; 49 multiLineText.Dock = DockStyle.Top; 50 multiLineText.Margin.Left = multiLineText.Margin.Right = 50; 51 shapes.Add(multiLineText); 52 53 using (FileStream file = new FileStream(@"..\..\createlayers.pdf", FileMode.Create, FileAccess.Write)) 54 { 55 document.Write(file); 56 }

VB.NET draw on each layer

1 ' coordinate (0, 0) lies at the bottom left of the shapes. 2 3 ' draw a grid of 25 x 25 points in the GridLayer 4 Dim gridPen As New TallComponents.PDF.Pens.Pen(Color.LightGray, 1) 5 For x As Double = 0 To shapes.Width - 1 Step 25 6 For y As Double = 0 To shapes.Height - 1 Step 25 7 ' Draw the lines 8 gridLayerShape.Add(New LineShape(0, y, shapes.Width, y, gridPen)) 9 gridLayerShape.Add(New LineShape(x, 0, x, shapes.Height, gridPen)) 10 11 ' Add axis numbering 12 gridLayerShape.Add(New TextShape(0, y, y.ToString(), Fonts.Font.Helvetica, 8)) 13 gridLayerShape.Add(New TextShape(x, 0, x.ToString(), Fonts.Font.Helvetica, 8)) 14 Next 15 Next 16 17 'Add shapes to the graphics layer 18 ' add a line 19 graphicsLayerShape.Add(New LineShape(25, 25, 75, 50)) 20 21 ' add an ellipse 22 graphicsLayerShape.Add(New EllipseShape(150, 50, 50, 25)) 23 24 ' add a rectangle 25 graphicsLayerShape.Add(New RectangleShape(250, 25, 75, 50, New TallComponents.PDF.Pens.Pen(Color.Black), New TallComponents.PDF.Brushes.SolidBrush(Color.Yellow))) 26 27 ' draw one bezier without the helper lines 28 graphicsLayerShape.Add(New BezierShape(325, 150, 375, 200, 325, 100, 29 375, 150)) 30 31 ' add a few arcshapes 32 graphicsLayerShape.Add(New ArcShape(50, 225, 25, 25)) 33 graphicsLayerShape.Add(New ArcShape(125, 225, 25, 25, 0, 45)) 34 graphicsLayerShape.Add(New ArcShape(200, 225, 25, 25, 0, 90)) 35 graphicsLayerShape.Add(New ArcShape(275, 225, 25, 25, 90, 180)) 36 37 ' add a few pieshapes 38 graphicsLayerShape.Add(New PieShape(50, 300, 25, 25)) 39 graphicsLayerShape.Add(New PieShape(125, 300, 25, 25, 0, 45)) 40 graphicsLayerShape.Add(New PieShape(200, 300, 25, 25, 0, 45)) 41 graphicsLayerShape.Add(New PieShape(275, 300, 25, 25, 90, 270)) 42 43 ' add a multiline textshape 44 Dim multiLineText As New MultilineTextShape() 45 multiLineText.Fragments.Add(New Fragment("This example show how you can create multiple layers using PDF Kit")) 46 multiLineText.HorizontalAlignment = HorizontalAlignment.Center 47 multiLineText.Dock = DockStyle.Top 48 multiLineText.Margin.Left = multiLineText.Margin.Right = 50 49 shapes.Add(multiLineText) 50 51 Using file As New FileStream("..\..\createlayers.pdf", FileMode.Create, FileAccess.Write) 52 document.Write(file) 53 End Using

Here is the result:

create-layers-in-pdf.png

Switch the grid layer off:

create-layers-in-pdf-without-grid.png