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.
DsPdf 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 DsPdf:
To add a shape in a PDF document:
The following code snippet shows how to add a shape in a PDF document.
C# |
Copy Code
|
---|---|
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:
C# |
Copy Code
|
---|---|
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:
C# |
Copy Code
|
---|---|
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.
Back to TopTo perform transformations using DsPdf, 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.
C# |
Copy Code
|
---|---|
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 DsPdf, see DsPdf sample browser.