ComponentOne PDF for .NET
Adding Graphics
Using ComponentOne PDF for .NET > Adding Graphics

The C1PdfDocument class exposes several methods that allow you to add graphical elements to your documents, including lines, rectangles, ellipses, pies, arcs, rounded rectangles, polygons, Bezier curves, and so on.

The methods are a subset of those found in the .NET Graphics class, and use the same Brush and Pen classes to control the color and style of the lines and filled areas.

It is important to remember that PDF for .NET uses a coordinate system based on points, with the origin located at the top left of the page. (The default coordinate system for the .NET Graphics class is pixel-based.)

The example below illustrates how similar the graphics methods are between PDF for .NET and the .NET Graphics class. The sample declares a C1PdfDocument class called 'g' and calls methods to draw pies, splines, and other graphical elements.

The point of the sample is that if you replaced the C1PdfDocument class with a regular .NET Graphics object, you would be able to compile the code and get the same results:

To write code in Visual Basic

Visual Basic
Copy Code
' Create PDF document.
Dim g As New C1.C1Pdf.C1PdfDocument()
 ' Set up to draw.
Dim rect As New RectangleF(0, 0, 300, 200)
Dim text As String = "Hello world of .NET Graphics and PDF." + ControlChars.Cr + ControlChars.Lf + "Nice to meet you."
Dim font As New Font("Times New Roman", 12, FontStyle.Italic Or FontStyle.Underline)
Dim bezierPoints() As PointF = {New PointF(10F, 100F), New PointF(20F, 10F), New PointF(35F, 50F), New PointF(50F, 100F), New PointF(60F, 150F), New PointF(65F, 100F), New PointF(50F, 50F)}
 ' Draw some pie slices.
Dim penWidth As Integer = 0
Dim penRGB As Integer = 0
g.FillPie(Brushes.Red, rect, 0, 20F)
g.FillPie(Brushes.Green, rect, 20F, 30F)
g.FillPie(Brushes.Blue, rect, 60F, 12F)
g.FillPie(Brushes.Gold, rect, - 80F, - 20F)
 ' Draw some arcs.
Dim startAngle As Single
For startAngle = 0 To 360 - 40 Step 40
    Dim penColor As Color = Color.FromArgb(penRGB, penRGB, penRGB)
    penWidth = penWidth + 1
    Dim pen As New Pen(penColor, penWidth)
    penRGB = penRGB + 20
    g.DrawArc(pen, rect, startAngle, 40F)
Next
 ' Draw a rectangle and some bezier splines.
g.DrawRectangle(Pens.Red, rect)
g.DrawBeziers(Pens.Blue, bezierPoints)
g.DrawString(text, font, Brushes.Black, rect)

To write code in C#

C#
Copy Code
// Create PDF document.
C1.C1Pdf.C1PdfDocument g = new C1.C1Pdf.C1PdfDocument();
 // Set up to draw.
Rectangle rect = new RectangleF(0,0,300,200);
string text = "Hello world of .NET Graphics and PDF.\r\n" + "Nice to meet you.";
Font font = new Font("Times New Roman", 12, FontStyle.Italic | FontStyle.Underline);
PointF[] bezierPoints = new PointF[]
{
    new PointF(10f, 100f), new PointF(20f, 10f), new PointF(35f, 50f), new PointF(50f, 100f), new PointF(60f, 150f), new PointF(65f, 100f), new PointF(50f, 50f)
};
 // Draw some pie slices.
int penWidth = 0;
int penRGB = 0;
g.FillPie(Brushes.Red, rect, 0, 20f);
g.FillPie(Brushes.Green, rect, 20f, 30f);
g.FillPie(Brushes.Blue, rect, 60f, 12f);
g.FillPie(Brushes.Gold, rect, -80f, -20f);
 // Draw some arcs.
for (float startAngle = 0; startAngle < 360; startAngle += 40)
{
    Color penColor = Color.FromArgb(penRGB, penRGB, penRGB);
    Pen pen = new Pen(penColor, penWidth++);
    penRGB = penRGB + 20;
    g.DrawArc(pen, rect, startAngle, 40f);
} 
// Draw a rectangle and some bezier splines.
g.DrawRectangle(Pens.Red, rect);
g.DrawBeziers(Pens.Blue, bezierPoints);
g.DrawString(text, font, Brushes.Black, rect);

Here is the resulting PDF document: