void CreateDocumentVisualTree(C1WordDocument rtf, FrameworkElement targetElement) { // set up to render var font = new Font("Courier", 14); var img = new WriteableBitmap(CreateBitmap(targetElement)); // go render bool firstPage = true; foreach (Stretch stretch in new Stretch[] { Stretch.Fill, Stretch.None, Stretch.Uniform, Stretch.UniformToFill }) { // add page break if (!firstPage) { //rtf.NewPage(); } firstPage = false; // set up to render var alignment = ContentAlignment.TopLeft; //var rc = WordUtils.Inflate(rtf.PageRectangle, -72, -72); var sz = rtf.PageSize; var rc = new Rect(72, 72, sz.Width - 144, sz.Height - 144); rc.Height /= 2; // render element as image rtf.DrawString("Element as Image, Stretch: " + stretch.ToString(), font, Colors.Black, rc); rc = WordUtils.Inflate(rc, -20, -20); //rtf.DrawImage(img, rc, alignment, stretch); rtf.DrawImage(img, rc); rtf.DrawRectangle(Colors.Green, rc); rc = WordUtils.Inflate(rc, +20, +20); rtf.DrawRectangle(Colors.Green, rc); // move to bottom of the page rc = WordUtils.Offset(rc, 0, rc.Height + 20); // render element rtf.DrawString("Element as VisualTree, Stretch: " + stretch.ToString(), font, Colors.Black, rc); rc = WordUtils.Inflate(rc, -20, -20); rtf.DrawElement(targetElement, rc, alignment, stretch); rtf.DrawRectangle(Colors.Green, rc); rc = WordUtils.Inflate(rc, +20, +20); rtf.DrawRectangle(Colors.Green, rc); } }
static void CreateDocumentGraphics(C1WordDocument word) { // set up to draw word.Landscape = true; Rect rc = new Rect(0, 100, 300, 300); string text = Strings.DocumentGraphicsText; Font font = new Font("Segoe UI Light", 16, RtfFontStyle.Italic); // add warning paragraph word.AddParagraph(Strings.GraphicsWarning, font, Colors.DarkGray, RtfHorizontalAlignment.Left); var paragraph = (RtfParagraph)word.Current; paragraph.BottomBorderColor = Colors.Red; paragraph.BottomBorderStyle = RtfBorderStyle.Dotted; paragraph.BottomBorderWidth = 1f; word.AddParagraph(string.Empty); // draw to word document int penWidth = 0; byte penRGB = 0; word.FillPie(Colors.DarkRed, rc, 0, 20f); word.FillPie(Colors.Green, rc, 20f, 30f); word.FillPie(Colors.Teal, rc, 60f, 12f); word.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); word.DrawArc(pen, rc, startAngle, 40f); } word.DrawRectangle(Colors.Red, rc); word.DrawString(text, font, Colors.Black, rc); // show a Bezier curve var pts = new Point[] { new Point(400, 100), new Point(420, 30), new Point(500, 140), new Point(530, 20), }; // draw Bezier word.DrawBeziers(new Pen(Colors.Green, 4), pts); // show Bezier control points word.DrawPolyline(Colors.Gray, pts); foreach (Point pt in pts) { word.FillRectangle(Colors.Orange, pt.X - 2, pt.Y - 2, 4, 4); } // title word.DrawString(Strings.Bezier, font, Colors.Black, new Rect(500, 150, 100, 100)); }
static Rect RenderTableRow(C1WordDocument word, Font font, Font hdrFont, Rect rcPage, Rect rc, string[] fields, DataRow dr) { // calculate cell width (same for all columns) Rect rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) rcCell = WordUtils.Inflate(rcCell, -4, 0); foreach (string field in fields) { string text = dr[field].ToString(); var height = word.MeasureString(text, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } rcCell = WordUtils.Inflate(rcCell, 4, 0); // add 4 point margin rcCell.Height += 2; // break page if we have to if (rcCell.Bottom > rcPage.Bottom) { word.PageBreak(); rc = RenderTableHeader(word, hdrFont, rcPage, fields); rcCell.Y = rc.Y; } // center vertically just to show how StringFormat fmt = new StringFormat(); fmt.LineAlignment = VerticalAlignment.Center; // render data cells foreach (string field in fields) { // get content string text = dr[field].ToString(); // set horizontal alignment double d; fmt.Alignment = (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out d)) ? HorizontalAlignment.Right : HorizontalAlignment.Left; // render cell word.DrawRectangle(Colors.LightGray, rcCell); rcCell = WordUtils.Inflate(rcCell, -4, 0); word.DrawString(text, font, Colors.Black, rcCell, fmt); rcCell = WordUtils.Inflate(rcCell, 4, 0); rcCell = WordUtils.Offset(rcCell, rcCell.Width, 0); } // update rectangle and return it return(WordUtils.Offset(rc, 0, rcCell.Height)); }
//--------------------------------------------------------------------------------- #region ** graphics static void CreateDocumentGraphics(C1WordDocument rtf) { // 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)); }
private void _btGraphics_Click(object sender, System.EventArgs e) { // create document C1WordDocument c1Word = new C1WordDocument(); c1Word.Info.Title = "Graphics primitives sample"; _statusBar.Text = "Creating document..."; RectangleF rc = new RectangleF(250, 100, 200, 200); Bitmap image = new Bitmap(GetManifestResource("Word.picture.jpg")); c1Word.DrawImage(image, rc); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; rc = new RectangleF(250, 100, 150, 20); Font font = new Font("Arial", 14, FontStyle.Italic); c1Word.DrawString(c1Word.Info.Title, font, Color.DeepPink, rc, sf); c1Word.DrawLine(Pens.Green, 200, 190, 400, 190); rc = new RectangleF(150, 150, 190, 80); using (Pen pen = new Pen(Brushes.Blue, 5.0f)) { c1Word.DrawRectangle(pen, rc); } c1Word.FillRectangle(Color.Gold, rc); c1Word.ShapeFillOpacity(50); c1Word.ShapeRotation(25); rc = new RectangleF(300, 150, 80, 80); c1Word.DrawEllipse(Pens.Red, rc); c1Word.FillEllipse(Color.Pink, rc); c1Word.ShapeFillOpacity(70); PointF[] pts = new PointF[4]; pts[0] = new PointF(200, 200); pts[1] = new PointF(250, 300); pts[2] = new PointF(330, 250); pts[3] = new PointF(340, 140); c1Word.DrawPolyline(Pens.BlueViolet, pts); sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; sf.FormatFlags |= StringFormatFlags.DirectionVertical; rc = new RectangleF(450, 150, 25, 75); font = new Font("Verdana", 12, FontStyle.Bold); c1Word.DrawString("Vertical", font, Color.Black, rc, sf); pts = new PointF[4]; pts[0] = new PointF(372, 174); pts[1] = new PointF(325, 174); pts[2] = new PointF(325, 281); pts[3] = new PointF(269, 281); c1Word.DrawBeziers(Pens.HotPink, pts); _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "graphics.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }
private void _btComplex_Click(object sender, System.EventArgs e) { // create document C1WordDocument c1Word = new C1WordDocument(); c1Word.Info.Title = "Complex sample"; _statusBar.Text = "Creating document..."; // add title c1Word.AddParagraph(c1Word.Info.Title, new Font("Tahoma", 24, FontStyle.Italic), Color.BlueViolet); // add image c1Word.AddParagraph("picture:", new Font("Courier New", 9, FontStyle.Regular), Color.Black); Bitmap img = new Bitmap(GetManifestResource("picture.jpg")); c1Word.AddPicture(img, RtfHorizontalAlignment.Center); // add table c1Word.LineBreak(); int rows = 7; int cols = 2; RtfTable table = new RtfTable(rows, cols); table.BottomBorderStyle = table.LeftBorderStyle = table.RightBorderStyle = table.TopBorderStyle = RtfBorderStyle.Single; table.BottomBorderWidth = table.LeftBorderWidth = table.RightBorderWidth = table.TopBorderWidth = 1; c1Word.Add(table); for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { var cell = table.Rows[row].Cells[col]; cell.BottomBorderStyle = cell.LeftBorderStyle = cell.RightBorderStyle = cell.TopBorderStyle = RtfBorderStyle.Single; cell.BottomBorderWidth = cell.LeftBorderWidth = cell.RightBorderWidth = cell.TopBorderWidth = 1; RtfParagraph paragraph = new RtfParagraph(); paragraph.Content.Add(new RtfString(string.Format("table cell {0}:{1}.", row, col))); cell.Content.Add(paragraph); } } // add graphics c1Word.LineBreak(); c1Word.DrawLine(Pens.Green, 200, 90, 400, 90); var rc = new RectangleF(150, 170, 90, 40); using (Pen pen = new Pen(Brushes.Blue, 5.0f)) { c1Word.DrawRectangle(pen, rc); } c1Word.FillRectangle(Color.Gold, rc); c1Word.ShapeFillOpacity(50); c1Word.ShapeRotation(25); rc = new RectangleF(300, 120, 80, 80); c1Word.DrawEllipse(Pens.Red, rc); c1Word.FillEllipse(Color.Pink, rc); c1Word.ShapeFillOpacity(70); _statusBar.Text = "Saving document..."; string fileName = GetFileName(c1Word, "complex.rtf"); c1Word.Save(fileName); Process.Start(fileName); _statusBar.Text = "Ready."; }