Working with Word for WPF > Basic Level Working > Drawing Graphics |
Adding graphics enhances the appearance of a document and make them visually appealing. You can add various types of shapes in your documents such as, Arc, Beizer, Ellipse, Line, Pie, polygon, PolygonLine, and Rectangle. Use the following code to add graphics such as pie, rectangles, polyline, and beziers along with the text:
Dim rtf = New C1WordDocument() ' set up to draw Dim rc As New Rect(100, 100, 300, 200) Dim text As String = "Hello world of .NET Graphics and Word/RTF." & vbCr & vbLf & "Nice to meet you." Dim font As New Font("Times New Roman", 12, RtfFontStyle.Italic Or RtfFontStyle.Underline) ' draw to pdf document Dim penWidth As Integer = 0 Dim penRGB As Byte = 0 rtf.FillPie(Colors.Red, rc, 0, 20F) rtf.FillPie(Colors.Green, rc, 20F, 30F) rtf.FillPie(Colors.Blue, rc, 60F, 12F) rtf.FillPie(Colors.Orange, rc, -80F, -20F) For startAngle As Single = 0 To 359 Step 40 Dim penColor As Color = Color.FromArgb(&Hff, penRGB, penRGB, penRGB) Dim pen As New Pen(penColor, System.Math.Max(System.Threading.Interlocked.Increment(penWidth),penWidth - 1)) penRGB = CByte(penRGB + 20) rtf.DrawArc(pen, rc, startAngle, 40F) Next rtf.DrawRectangle(Colors.Red, rc) rtf.DrawString(text, font, Colors.Black, rc) ' show a Bezier curve Dim pts = New Point() {New Point(400, 200), New Point(420, 130), New Point(500, 240), New Point(530, 120)} ' draw Bezier rtf.DrawBeziers(New Pen(Colors.Blue, 4), pts) ' show Bezier control points rtf.DrawPolyline(Colors.Gray, pts) For Each pt As Point In pts rtf.FillRectangle(Colors.Red, pt.X - 2, pt.Y - 2, 4, 4) Next ' title rtf.DrawString("Simple Bezier", font, Colors.Black, New Rect(500, 150, 100, 100))
var rtf = new C1WordDocument(); // set up to draw Rect rc = new Rect(100, 100, 300, 200); string text = "Hello world of .NET Graphics and Word/RTF.\r\nNice to meet you."; Font font = new Font("Times New Roman", 12, RtfFontStyle.Italic | RtfFontStyle.Underline); // draw to pdf document int penWidth = 0; byte penRGB = 0; rtf.FillPie(Colors.Red, rc, 0, 20f); rtf.FillPie(Colors.Green, rc, 20f, 30f); rtf.FillPie(Colors.Blue, rc, 60f, 12f); rtf.FillPie(Colors.Orange, rc, -80f, -20f); for (float startAngle = 0; startAngle < 360; startAngle += 40) { Color penColor = Color.FromArgb(0xff, penRGB, penRGB, penRGB); Pen pen = new Pen(penColor, penWidth++); penRGB = (byte)(penRGB + 20); rtf.DrawArc(pen, rc, startAngle, 40f); } rtf.DrawRectangle(Colors.Red, rc); rtf.DrawString(text, font, Colors.Black, rc); // show a Bezier curve var pts = new Point[] { new Point(400, 200), new Point(420, 130), new Point(500, 240), new Point(530, 120), }; // draw Bezier rtf.DrawBeziers(new Pen(Colors.Blue, 4), pts); // show Bezier control points rtf.DrawPolyline(Colors.Gray, pts); foreach (Point pt in pts) { rtf.FillRectangle(Colors.Red, pt.X - 2, pt.Y - 2, 4, 4); } // title rtf.DrawString("Simple Bezier", font, Colors.Black, new Rect(500, 150, 100, 100));
In the above code, DrawRectangle, DrawArc, DrawPolyline, and DrawBeziers methods are used to draw graphics of different types such as, line, rectangle, pie, and bezier. Besides, DrawString method is used to draw and show text in the document.
The output of the above code will look similar to the image given below: