Adding Vector Graphics TallPDF3.0 in .Net

Code Demonstrates Adding Vector Graphics TallPDF3.0 in .Net


Code Demonstrates Adding Vector Graphics TallPDF3.0 in .Net

 
using
System;

using

System.IO;

using

TallComponents.PDF.Layout;

using

TallComponents.PDF.Layout.Brushes;

using

TallComponents.PDF.Layout.Fonts;

using

TallComponents.PDF.Layout.Paragraphs;

using

TallComponents.PDF.Layout.Pens;

using

TallComponents.PDF.Layout.Shapes;

namespace

TallComponents.Samples.TallPDF

{

public class DrawingVectorGraphics

{

[STAThread]

static void Main()

{

using( FileStream file = new FileStream( @"..\..\drawing.pdf", FileMode.Create, FileAccess.Write ) )

{

Document document =

new Document();

Section section = document.Sections.Add();

section.PageSize =

new PageSize( 600, 600 );

// create a Drawing and make it occupy all space within the margins of the page

Drawing drawing =

new Drawing(

section.PageSize.Width - section.Margin.Left - section.Margin.Right,

section.PageSize.Height - section.Margin.Top - section.Margin.Bottom );

section.Paragraphs.Add( drawing );

// coordinate (0, 0) lies at the bottom left of the drawing.

// draw a grid of 25 x 25 points

Pen gridPen =

new Pen( System.Drawing.Color.LightGray, 1 );

for( double x = 0; x < drawing.Width; x += 25 )

{

for( double y = 0; y < drawing.Height; y += 25 )

{

// Draw the lines

drawing.Shapes.Add(

new LineShape( 0, y, drawing.Width, y, gridPen ) );

drawing.Shapes.Add(

new LineShape( x, 0, x, drawing.Height, gridPen ) );

// Add axis numbering

drawing.Shapes.Add(

new TextShape( 0, y, y.ToString(), Font.Helvetica, 8 ) );

drawing.Shapes.Add(

new TextShape( x, 0, x.ToString(), Font.Helvetica, 8 ) );

}

}

// add a line

drawing.Shapes.Add(

new LineShape( 25, 25, 75, 50 ) );

// add an ellipse

drawing.Shapes.Add(

new EllipseShape( 150, 50, 50, 25 ) );

// add a rectangle

drawing.Shapes.Add(

new RectangleShape( 250, 25, 75, 50, new Pen( System.Drawing.Color.Black ), new SolidBrush( System.Drawing.Color.Yellow ) ) );

// add a few beziershapes

AddBezier( drawing, 25, 125, 25, 150, 50, 175, 75, 175 );

AddBezier( drawing, 100, 125, 100, 175, 125, 175, 150, 175 );

AddBezier( drawing, 175, 150, 175, 200, 225, 100, 225, 150 );

AddBezier( drawing, 250, 150, 300, 200, 250, 100, 300, 150 );

// draw one bezier without the helper lines

drawing.Shapes.Add(

new BezierShape( 325, 150, 375, 200, 325, 100, 375, 150 ) );

// add a few arcshapes

drawing.Shapes.Add(

new ArcShape( 50, 225, 25, 25 ) );

drawing.Shapes.Add(

new ArcShape( 125, 225, 25, 25, 0, 45 ) );

drawing.Shapes.Add(

new ArcShape( 200, 225, 25, 25, 0, 90 ) );

drawing.Shapes.Add(

new ArcShape( 275, 225, 25, 25, 90, 180 ) );

// add a few pieshapes

drawing.Shapes.Add(

new PieShape( 50, 300, 25, 25 ) );

drawing.Shapes.Add(

new PieShape( 125, 300, 25, 25, 0, 45 ) );

drawing.Shapes.Add(

new PieShape( 200, 300, 25, 25, 0, 45, true ) );

drawing.Shapes.Add(

new PieShape( 275, 300, 25, 25, 90, 270, true ) );

// add a multiline textshape

MultilineTextShape multiLineText =

new MultilineTextShape();

multiLineText.Fragments.Add(

new Fragment(

"This example shows how to use the drawing primitives of TallPDF.NET. " +

"Shapes that are used in this sample are LineShape, EllipseShape, RectangleShape, " +

"BezierShape, ArcShape, PieShape, TextShape and MultilineTextShape.") );

multiLineText.HorizontalAlignment = HorizontalAlignment.Center;

multiLineText.Dock = DockStyle.Top;

multiLineText.LeftMargin = multiLineText.RightMargin = 50;

drawing.Shapes.Add( multiLineText );

document.Write( file );

}

}

private static void AddBezier( Drawing drawing,

double x , double y,

double x1, double y1,

double x2, double y2,

double x3, double y3 )

{

// Create the new bezier

BezierShape bezier =

new BezierShape( x, y, x1, y1, x2, y2, x3, y3 );

// Add helper lines.

AddBezierHelperShapes( drawing, bezier );

// Add the bezier to the drawing

drawing.Shapes.Add( bezier );

}

private static void AddBezierHelperShapes( Drawing drawing, BezierShape bezier )

{

// Helper line from start point to start control point

LineShape startLine =

new LineShape( bezier.X, bezier.Y, bezier.X1, bezier.Y1 );

startLine.Pen =

new Pen( System.Drawing.Color.Green, 1, new DashPattern( 1, new double[] { 2, 1, 1, 0 } ) );

drawing.Shapes.Add( startLine );

// Helper line from end point to end control point

LineShape endLine =

new LineShape( bezier.X2, bezier.Y2, bezier.X3, bezier.Y3 );

endLine.Pen =

new Pen( System.Drawing.Color.Red, 1, new DashPattern( 1, new double[] { 2, 1, 1, 0 } ) );

drawing.Shapes.Add( endLine );

}

}

}

Comments

Article rating:
Your rating:

Activity for this knol

This week:

22pageviews

Totals:

284pageviews