This section covers the following topics:
In order to paint shapes or draw text onto a page, you will need to use pens and brushes. A pen resembles a real-world pen and is used to stroke, or in other words; you draw lines with a pen. To fill shapes with a colour, you will use a brush.
A pen is used to draw lines. Just like a real-world pen, it has a colour and a line thickness. Pens also have some other properties that facilitate the drawing of dashed lines, for instance. To use a pen, create a new instance of the Pen class. Below is a simple example of pen usage:
This draws a red line across the page. The line will have a default thickness of 1 point, which is 1/72nd of an inch. For more information on the units used in graphics operations, see the Page units sub-section of the Advanced Use section. To create a pen with a different width, you can use an overloaded constructor and specify the width directly:
This creates a pen that is 1/4th of a point wide.
A brush is used to fill shapes and draw text. In its most simple form, a brush has a colour and no other properties; it is a solid brush. The section on Gradients covers other kinds of brushes. The SolidBrush class implements the functionality of a solid-colour brush. See the example below:
This fills a square with a solid green colour. Note that we could have written line 2 as follows:
The SolidBrush class inherits from Brush, the base class for all brushes.
For a shape that is outlined as well as filled in, perform the two operations consecutively:
In the examples above, we created coloured brushes and pens using a short-hand notation; for a number of common colours, PDF Graphics .NET offers pre-created brushes and pens. To create our own brushes and pens, we need to create the colours manually. See the example below (lines omitted for brevity):
The example code creates a new RgbColour instance and uses it to create a brush to fill the rectangle with. The RgbColour class represent a RGB colour, defined by red, green and blue components. We will be using RGB colours for the moment, because they are familiar. The Colour schemes section will provide full coverage of the colour schemes supported by PDF Graphics .NET.
In the above sections we used the Pen class to draw lines. So far, we've only used solid lines. The Pen class can be used to draw dotted or dashed lines, too. See the example code below:
This produces the following output:
The numbers (4,2) specify the length, in points, of the dashes and the spaces in between (no matter the thickness of the pen stroke). The pattern is not limited to two numbers; more elaborate ones are possible, such as the following dash-dot one:
It is possible to specify a list of numbers of uneven length, too; the pen's alternating strokes and spaces are determined by cycling through the numbers so a list of uneven length will simply result in a less predictable pattern.
The Pen class has two more properties that determine the appearance of lines; LineCapStyle and LineJoinStyle. These allow fine-tuning of the appearance of lines at their end points or when two lines meet at an angle.
The LineCapStyle property controls the appearance of end points of a line:
Line cap styles | ||
---|---|---|
Value of LineCapStyle | Example | Description |
LineCapStyle.Butt | ![]() |
A line with butt endings has no projection; the line ends exactly at its end points, and the endings are square. This is the default value. |
LineCapStyle.Rounded | ![]() |
A line with rounded endings extends beyond its endpoints by half its width, and the endings are demi-circles. |
LineCapStyle.ProjectingSquare | ![]() |
A line with projecting square endings extends beyond its endpoints by half its width, and the endings are square. |
The LineJoinStyle property controls the appearance of lines when they meet an angle:
Line join styles | ||
---|---|---|
Value of LineJoinStyle | Example | Description |
LineJoinStyle.Miter | ![]() |
When lines are joined using the Miter style, their outer edges join at an angle, creating a sharp pointed ending. This is the default value. |
LineJoinStyle.Round | ![]() |
When lines are joined using the Round style, the outer edges join through an arc, giving the join a rounded look. |
LineJoinStyle.Bevel | ![]() |
When lines are joined using the Bevel style, their outer edges are joined by a straight piece similar to the squared end of a Butt cap. |