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 illustrations of a theory or concept.
PDF for .NET allows you to draw graphics in a document using various Draw methods, such as DrawArc, DrawEllipse, DrawLines and more. These methods use the Brush and Pen classes to control the color and style of the lines and filled areas as shown in the following image.
To add graphics in the document, use the following code. The following example draws a rectangle, arc, pies, and splines in a Pdf document.
C# |
Copy Code
|
---|---|
// Set up to draw. RectangleF 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; pdf.FillPie(Brushes.Red, rect, 0, 20f); pdf.FillPie(Brushes.Green, rect, 20f, 30f); pdf.FillPie(Brushes.Blue, rect, 60f, 12f); pdf.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; pdf.DrawArc(pen, rect, startAngle, 40f); } // Draw a rectangle and some bezier splines. pdf.DrawRectangle(Pens.Red, rect); pdf.DrawBeziers(Pens.Blue, bezierPoints); pdf.DrawString(text, font, Brushes.Black, rect); |