Vector graphics in PDF

Shapes
7/18/2014

Downloads

This code sample shows how to draw different vector graphics such as line, ellipses and bezier curves. It also demonstrates fills and line styles.

Vector graphics in PDF.png

C# code sample

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 // coordinate (0, 0) lies at the bottom left of the shapes. 10 11 // draw a grid of 25 x 25 points 12 Pen gridPen = new Pen( System.Drawing.Color.LightGray, 1 ); 13 for( double x = 0; x < shapes.Width; x += 25 ) 14 { 15 for( double y = 0; y < shapes.Height; y += 25 ) 16 { 17 // Draw the lines 18 shapes.Add( new LineShape( 0, y, shapes.Width, y, gridPen ) ); 19 shapes.Add( new LineShape( x, 0, x, shapes.Height, gridPen ) ); 20 21 // Add axis numbering 22 shapes.Add( new TextShape( 0, y, y.ToString(), Font.Helvetica, 8 ) ); 23 shapes.Add( new TextShape( x, 0, x.ToString(), Font.Helvetica, 8 ) ); 24 } 25 } 26 27 // add a line 28 shapes.Add( new LineShape( 25, 25, 75, 50 ) ); 29 30 // add an ellipse 31 shapes.Add( new EllipseShape( 150, 50, 50, 25 ) ); 32 33 // add a rectangle 34 shapes.Add( new RectangleShape( 250, 25, 75, 50, new Pen( System.Drawing.Color.Black ), new SolidBrush( System.Drawing.Color.Yellow ) ) ); 35 36 // add a few beziershapes 37 AddBezier( shapes, 25, 125, 25, 150, 50, 175, 75, 175 ); 38 AddBezier( shapes, 100, 125, 100, 175, 125, 175, 150, 175 ); 39 AddBezier( shapes, 175, 150, 175, 200, 225, 100, 225, 150 ); 40 AddBezier( shapes, 250, 150, 300, 200, 250, 100, 300, 150 ); 41 42 // draw one bezier without the helper lines 43 shapes.Add( new BezierShape( 325, 150, 375, 200, 325, 100, 375, 150 ) ); 44 45 // add a few arcshapes 46 shapes.Add( new ArcShape( 50, 225, 25, 25 ) ); 47 shapes.Add( new ArcShape( 125, 225, 25, 25, 0, 45 ) ); 48 shapes.Add( new ArcShape( 200, 225, 25, 25, 0, 90 ) ); 49 shapes.Add( new ArcShape( 275, 225, 25, 25, 90, 180 ) ); 50 51 // add a few pieshapes 52 shapes.Add( new PieShape( 50, 300, 25, 25 ) ); 53 shapes.Add( new PieShape( 125, 300, 25, 25, 0, 45 ) ); 54 shapes.Add( new PieShape( 200, 300, 25, 25, 0, 45 ) ); 55 shapes.Add( new PieShape( 275, 300, 25, 25, 90, 270 ) ); 56 57 // add a multiline textshape 58 MultilineTextShape multiLineText = new MultilineTextShape(); 59 multiLineText.Fragments.Add( new Fragment( 60 "This example shows how to use the shapes primitives of PDFKit.NET. " + 61 "Shapes that are used in this sample are LineShape, EllipseShape, RectangleShape, " + 62 "BezierShape, ArcShape, PieShape, TextShape and MultilineTextShape.") ); 63 multiLineText.HorizontalAlignment = HorizontalAlignment.Center; 64 multiLineText.Dock = DockStyle.Top; 65 multiLineText.Margin.Left = multiLineText.Margin.Right = 50; 66 shapes.Add( multiLineText ); 67 68 using( FileStream file = new FileStream( @"..\..\vectorgraphics.pdf", FileMode.Create, FileAccess.Write ) ) 69 { 70 document.Write( file); 71 }

VB.NET code sample

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 ' coordinate (0, 0) lies at the bottom left of the shapes. 10 11 ' draw a grid of 25 x 25 points 12 Dim gridPen As New Pen(System.Drawing.Color.LightGray, 1) 13 For x As Double = 0 To shapes.Width - 1 Step 25 14 For y As Double = 0 To shapes.Height - 1 Step 25 15 ' Draw the lines 16 shapes.Add(New LineShape(0, y, shapes.Width, y, gridPen)) 17 shapes.Add(New LineShape(x, 0, x, shapes.Height, gridPen)) 18 19 ' Add axis numbering 20 shapes.Add(New TextShape(0, y, y.ToString(), Font.Helvetica, 8)) 21 shapes.Add(New TextShape(x, 0, x.ToString(), Font.Helvetica, 8)) 22 Next 23 Next 24 25 ' add a line 26 shapes.Add(New LineShape(25, 25, 75, 50)) 27 28 ' add an ellipse 29 shapes.Add(New EllipseShape(150, 50, 50, 25)) 30 31 ' add a rectangle 32 shapes.Add(New RectangleShape(250, 25, 75, 50, New Pen(System.Drawing.Color.Black), New SolidBrush(System.Drawing.Color.Yellow))) 33 34 ' add a few beziershapes 35 AddBezier(shapes, 25, 125, 25, 150, 50, _ 36 175, 75, 175) 37 AddBezier(shapes, 100, 125, 100, 175, 125, _ 38 175, 150, 175) 39 AddBezier(shapes, 175, 150, 175, 200, 225, _ 40 100, 225, 150) 41 AddBezier(shapes, 250, 150, 300, 200, 250, _ 42 100, 300, 150) 43 44 ' draw one bezier without the helper lines 45 shapes.Add(New BezierShape(325, 150, 375, 200, 325, 100, _ 46 375, 150)) 47 48 ' add a few arcshapes 49 shapes.Add(New ArcShape(50, 225, 25, 25)) 50 shapes.Add(New ArcShape(125, 225, 25, 25, 0, 45)) 51 shapes.Add(New ArcShape(200, 225, 25, 25, 0, 90)) 52 shapes.Add(New ArcShape(275, 225, 25, 25, 90, 180)) 53 54 ' add a few pieshapes 55 shapes.Add(New PieShape(50, 300, 25, 25)) 56 shapes.Add(New PieShape(125, 300, 25, 25, 0, 45)) 57 shapes.Add(New PieShape(200, 300, 25, 25, 0, 45)) 58 shapes.Add(New PieShape(275, 300, 25, 25, 90, 270)) 59 60 ' add a multiline textshape 61 Dim multiLineText As New MultilineTextShape() 62 multiLineText.Fragments.Add(New Fragment("This example shows how to use the shapes primitives of PDFKit.NET. " & "Shapes that are used in this sample are LineShape, EllipseShape, RectangleShape, " & "BezierShape, ArcShape, PieShape, TextShape and MultilineTextShape.")) 63 multiLineText.HorizontalAlignment = HorizontalAlignment.Center 64 multiLineText.Dock = DockStyle.Top 65 multiLineText.Margin.Left = 50 66 multiLineText.Margin.Right = 50 67 shapes.Add(multiLineText) 68 69 Using file As New FileStream("..\..\vectorgraphics.pdf", FileMode.Create, FileAccess.Write) 70 document.Write(New BinaryWriter(file)) 71 End Using

Above, we make use of utility method for drawing bezier curves with additional helper lines.

C# code sample

1 private static void AddBezier( ShapeCollection shapes, 2 double x , double y, 3 double x1, double y1, 4 double x2, double y2, 5 double x3, double y3 ) 6 { 7 // Create the new bezier 8 BezierShape bezier = new BezierShape( x, y, x1, y1, x2, y2, x3, y3 ); 9 10 // Add helper lines. 11 AddBezierHelperShapes( shapes, bezier ); 12 13 // Add the bezier to the shapes 14 shapes.Add( bezier ); 15 } 16 17 private static void AddBezierHelperShapes( ShapeCollection shapes, BezierShape bezier ) 18 { 19 // Helper line from start point to start control point 20 LineShape startLine = new LineShape( bezier.X0, bezier.Y0, bezier.X1, bezier.Y1 ); 21 startLine.Pen = new Pen( System.Drawing.Color.Green, 1, new DashPattern( 1, new double[] { 3, 1 } ) ); 22 shapes.Add( startLine ); 23 24 // Helper line from end control point to end point 25 LineShape endLine = new LineShape( bezier.X2, bezier.Y2, bezier.X3, bezier.Y3 ); 26 endLine.Pen = new Pen( System.Drawing.Color.Red, 1, new DashPattern( 1, new double[] { 3, 1 } ) ); 27 shapes.Add( endLine ); 28 }

VB.NET code sample

1 Private Shared Sub AddBezier(ByVal shapes As ShapeCollection, ByVal x As Double, ByVal y As Double, ByVal x1 As Double, ByVal y1 As Double, ByVal x2 As Double, _ 2 ByVal y2 As Double, ByVal x3 As Double, ByVal y3 As Double) 3 ' Create the new bezier 4 Dim bezier As New BezierShape(x, y, x1, y1, x2, y2, _ 5 x3, y3) 6 7 ' Add helper lines. 8 AddBezierHelperShapes(shapes, bezier) 9 10 ' Add the bezier to the shapes 11 shapes.Add(bezier) 12 End Sub 13 14 Private Shared Sub AddBezierHelperShapes(ByVal shapes As ShapeCollection, ByVal bezier As BezierShape) 15 ' Helper line from start point to start control point 16 Dim startLine As New LineShape(bezier.X0, bezier.Y0, bezier.X1, bezier.Y1) 17 startLine.Pen = New Pen(System.Drawing.Color.Green, 1, New DashPattern(1, New Double() {3, 1})) 18 shapes.Add(startLine) 19 20 ' Helper line from end control point to end point 21 Dim endLine As New LineShape(bezier.X2, bezier.Y2, bezier.X3, bezier.Y3) 22 endLine.Pen = New Pen(System.Drawing.Color.Red, 1, New DashPattern(1, New Double() {3, 1})) 23 shapes.Add(endLine) 24 End Sub