[]
Graphics are visual elements that can be displayed in the form of different shapes, lines, curves or images in a document. These are generally used to supplement text for better illustration of a theory or concept.
GcPdf allows you to draw graphics in a document using methods such as DrawRectangle, DrawEllipse, etc., available in GcGraphics class. These methods use an object of GcPdfGraphics class to draw graphics on a page. Following is a list of graphic elements supported by GcPdf:
Line
Rectangle
Ellipse
Polygon
Path
To add a shape in a PDF document:
Create an object of GcPdfDocument class.
Add a blank page to the document using GcPdfDocument object.
Draw an ellipse using DrawEllipse method provided by GcGraphics class.
The following code snippet shows how to add a shape in a PDF document.
public void CreatePDF(Stream stream)
{
// Create a new PDF document:
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
// Pen used to draw shape
var pen = new Pen(Color.Orange, 2);
// Draw a shape
g.DrawEllipse(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), pen);
// Save document
doc.Save(stream);
}
To fill a shape:
Create an object of GcPdfDocument class.
Add a blank page to document using the GcPdfDocument object.
Draw an ellipse using the DrawEllipse method provided by the GcGraphics class.
Fill the shape using the FillEllipse method of GcGraphics class.
public void CreatePDF(Stream stream)
{
// Create a new PDF document:
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
// Pen used to draw shape
var pen = new Pen(Color.Orange, 2);
// Draw a Shape
g.DrawEllipse(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), pen);
// Fill a Shape
g.FillEllipse(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), Color.Orange);
// Save document
doc.Save(stream);
}
To use gradients in a PDF document:
Draw a shape by creating an instance of class corresponding to shape you want to add to a page, for example, DrawRectangle class.
Create a linear gradient brush by initializing the LinearGradientBrush class and specify the start color and end color for the gradient.
Fill the shape by passing the object of LinearGradientBrush in the corresponding fill method, for example, FillRectangle.
public void CreatePDF(Stream stream)
{
// Create a new PDF document:
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
// Pen used for Drawing
var pen = new Pen(Color.Orange, 2);
// Draw a Shape
g.DrawRectangle(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), pen);
// Create a linear gradient brush
LinearGradientBrush linearGradBrush = new LinearGradientBrush(Color.Red, Color.Blue);
// Fill a Shape
g.FillRectangle(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), linearGradBrush);
// Save document
doc.Save(stream);
}
Similarly, the RadialGradientBrush class provides radial gradient brush and the HatchBrush class provides hatch patterns to fill the shapes.
To perform transformations using GcPdf, set the Transform property provided by the GcGraphics class. In this example, we have transformed the rectangle box, which is scaled by 0.7, translated by (3',5'), and rotated 70 degrees counterclockwise. The values for transformation are calculated with the help of different methods provided by the Matrix3x2 class.
public void CreatePDF(Stream stream)
{
// Create a PDF document
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;
// Translation Values
var translate0 = Matrix3x2.CreateTranslation(72 * 3, 72 * 5);
var scale0 = Matrix3x2.CreateScale(0.7F);
var rotate0 = Matrix3x2.CreateRotation((float)(-70 * Math.PI) / 180F);
//Draw Rectangle and Apply Transformations
var box = new RectangleF(0, 0, 72 * 4, 72 * 2);
g.Transform = rotate0 * translate0 * scale0;
g.FillRectangle(box, Color.FromArgb(100, Color.DarkSeaGreen));
g.DrawRectangle(box, Color.DarkOrange, 1);
g.DrawString("Sample Box: Text drawn at (0,0) in a 4\"x2\" box"+
", scaled by 0.7, translated by (3\",5\"), " +
"and rotated 70 degrees counterclockwise.",
new TextFormat() { Font = StandardFonts.Times, FontSize = 14, }, box);
// Save document
doc.Save(stream);
}
For more information about implementation of graphics using GcPdf, see GcPdf sample browser.