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));
        }
        // ***********************************************************************
        // C1WordDocuments for measure a paragraph
        // ***********************************************************************

        //public static double MeasureText(this C1WordDocument doc, string text, Font font, Rect rcPage, double height, RtfHorizontalAlignment align = RtfHorizontalAlignment.Undefined)
        //{
        //    // if it won't fit this page, do a page break
        //    var sf = new StringFormat();
        //    switch (align)
        //    {
        //        case RtfHorizontalAlignment.Center:
        //            sf.Alignment = HorizontalAlignment.Center;
        //            break;
        //        case RtfHorizontalAlignment.Justify:
        //            sf.Alignment = HorizontalAlignment.Stretch;
        //            break;
        //        case RtfHorizontalAlignment.Right:
        //            sf.Alignment = HorizontalAlignment.Right;
        //            break;
        //    }
        //    //sf.FormatFlags |= StringFormatFlags.
        //    rc.Height = doc.MeasureString(text, font, rcPage.Width, sf).Height;
        //    if (rc.Bottom > rcPage.Bottom)
        //    {
        //        doc.PageBreak();
        //        rc.Y = rcPage.Top;
        //    }

        //    // add the paragraph
        //    doc.AddParagraph(text, font, clr, align);
        //    //doc.DrawString(text, font, Colors.Black, rc);

        //    // add headings to outline
        //    if (outline)
        //    {
        //        // top line
        //        var paragraph = (RtfParagraph)doc.Current;
        //        paragraph.TopBorderColor = clr;
        //        paragraph.TopBorderStyle = RtfBorderStyle.Single;
        //        paragraph.TopBorderWidth = 1f;

        //        //doc.DrawLine(Colors.Black, rc.X, rc.Y, rc.Right, rc.Y);
        //        doc.AddBookmark(text);
        //    }

        //    // add link target
        //    if (linkTarget)
        //    {
        //        //doc.AddLink(text);
        //    }

        //    // update rectangle for next time
        //    rc = Offset(rc, 0, rc.Height);
        //    return rc;
        //}

        // ***********************************************************************
        // C1WordDocuments for Rect
        // ***********************************************************************

        // measure a paragraph, skip a page if it won't fit, render it into a rectangle,
        // and update the rectangle for the next paragraph.
        //
        // optionally mark the paragraph as an outline entry and as a link target.
        //
        // this routine will not break a paragraph across pages. for that, see the Text Flow sample.
        //
        public static Rect RenderParagraph(this C1WordDocument doc, string text, Font font, Rect rcPage, Rect rc, bool outline, bool linkTarget)
        {
            // if it won't fit this page, do a page break
            rc.Height = doc.MeasureString(text, font, rc.Width).Height;
            if (rc.Bottom > rcPage.Bottom)
            {
                doc.PageBreak();
                rc.Y = rcPage.Top;
            }

            // draw the string
            doc.DrawString(text, font, Colors.Black, rc);

            // show bounds (to check word wrapping)
            //var p = Pen.GetPen(Colors.Orange);
            //doc.DrawRectangle(p, rc);

            // add headings to outline
            if (outline)
            {
                doc.DrawLine(Colors.Black, rc.X, rc.Y, rc.Right, rc.Y);
                //doc.AddBookmark(text, 0, rc.Y);
            }

            // add link target
            if (linkTarget)
            {
                //doc.AddTarget(text, rc);
            }

            // update rectangle for next time
            rc = Offset(rc, 0, rc.Height);
            return(rc);
        }
Esempio n. 3
0
        static Rect RenderTableHeader(C1WordDocument word, Font font, Rect rc, string[] fields)
        {
            // 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)
            foreach (string field in fields)
            {
                var height = word.MeasureString(field, font, rcCell.Width).Height;
                rcCell.Height = Math.Max(rcCell.Height, height);
            }
            rcCell.Height += 6; // add 6 point margin

            // render header cells
            var fmt = new StringFormat();

            fmt.LineAlignment = VerticalAlignment.Center;
            foreach (string field in fields)
            {
                word.FillRectangle(Colors.Black, rcCell);
                word.DrawString(field, font, Colors.White, rcCell, fmt);
                rcCell = WordUtils.Offset(rcCell, rcCell.Width, 0);
            }

            // update rectangle and return it
            return(WordUtils.Offset(rc, 0, rcCell.Height));
        }
        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);
            }
        }
Esempio n. 5
0
        //---------------------------------------------------------------------------------
        #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));
        }
Esempio n. 6
0
        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));
        }
Esempio n. 7
0
        private void _btCurve_Click(object sender, System.EventArgs e)
        {
            // create document
            C1WordDocument c1Word = new C1WordDocument();

            c1Word.Info.Title = "Various curves sample";
            _statusBar.Text   = "Creating document...";

            StringFormat sf = new StringFormat();

            sf.Alignment     = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            RectangleF rc   = new RectangleF(250, 100, 150, 20);
            Font       font = new Font("Tachoma", 12, FontStyle.Italic | FontStyle.Underline);

            c1Word.DrawString("Curves sample", font, Color.Black, rc, sf);

            //// curve
            //PointF[] pts = new PointF[7];
            //pts[0] = new PointF(191.1f, 172.3f);
            //pts[1] = new PointF(229.1f, 205.3f);
            //pts[2] = new PointF(267.15f, 238.3f);
            //pts[3] = new PointF(296.4f, 235.3f);
            //pts[4] = new PointF(325.65f, 232.3f);
            //pts[5] = new PointF(346.1f, 193.3f);
            //pts[6] = new PointF(366.6f, 154.3f);
            //c1Word.DrawBeziers(Pens.HotPink, pts);

            //// pie
            //rc = new RectangleF(120, 100, 150, 80);
            //c1Word.FillPie(Brushes.Yellow, rc, 0, 90);
            //c1Word.DrawPie(Pens.Indigo, rc, 0, 90);

            // arc
            rc = new RectangleF(120, 100, 150, 80);
            c1Word.DrawArc(Pens.Red, rc, 0, 90);

            //// arc
            //rc = new RectangleF(320, 300, 90, 45);
            //c1Word.DrawArc(Pens.Blue, rc, 270, 90);

            _statusBar.Text = "Saving document...";
            string fileName = GetFileName(c1Word, "curves.rtf");

            c1Word.Save(fileName);
            Process.Start(fileName);
            _statusBar.Text = "Ready.";
        }
Esempio n. 8
0
        //---------------------------------------------------------------------------------
        #region ** table of contents

        static void CreateDocumentTOC(C1WordDocument word)
        {
            // create pdf document
            word.Info.Title = "Document with Table of Contents";

            // add title
            Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold);
            Rect rcPage    = WordUtils.PageRectangle(word);
            Rect rc        = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rcPage, false);

            rc.Y += 12;

            // create nonsense document
            var  bkmk       = new List <string[]>();
            Font headerFont = new Font("Arial", 14, RtfFontStyle.Bold);
            Font bodyFont   = new Font("Times New Roman", 11);

            for (int i = 0; i < 30; i++)
            {
                // create ith header (as a link target and outline entry)
                string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle());
                rc = WordUtils.RenderParagraph(word, header, headerFont, rcPage, rc, true, true);

                // save bookmark to build TOC later
                int pageNumber = 1;
                bkmk.Add(new string[] { pageNumber.ToString(), header });

                // create some text
                rc.X     += 36;
                rc.Width -= 36;
                for (int j = 0; j < 3 + _rnd.Next(20); j++)
                {
                    string text = BuildRandomParagraph();
                    rc    = WordUtils.RenderParagraph(word, text, bodyFont, rcPage, rc);
                    rc.Y += 6;
                }
                rc.X     -= 36;
                rc.Width += 36;
                rc.Y     += 20;
            }

            // start Table of Contents
            word.PageBreak();                   // start TOC on a new page
            //int tocPage = word.CurrentPage;	// save page index (to move TOC later)
            //int tocPage = 1;	// save page index (to move TOC later)
            rc        = WordUtils.RenderParagraph(word, "Table of Contents", titleFont, rcPage, rcPage, true);
            rc.Y     += 12;
            rc.X     += 30;
            rc.Width -= 40;

            // render Table of Contents
            Pen dottedPen = new Pen(Colors.Gray, 1.5f);

            dottedPen.DashStyle = DashStyle.Dot;
            StringFormat sfRight = new StringFormat();

            sfRight.Alignment = HorizontalAlignment.Right;
            rc.Height         = bodyFont.Size * 1.2;
            foreach (string[] entry in bkmk)
            {
                // get bookmark info
                string page   = entry[0];
                string header = entry[1];

                // render header name and page number
                word.DrawString(header, bodyFont, Colors.Black, rc);
                word.DrawString(page, bodyFont, Colors.Black, rc, sfRight);

#if true
                // connect the two with some dots (looks better than a dotted line)
                string dots = ". ";
                var    wid  = word.MeasureString(dots, bodyFont).Width;
                var    x1   = rc.X + word.MeasureString(header, bodyFont).Width + 8;
                var    x2   = rc.Right - word.MeasureString(page, bodyFont).Width - 8;
                var    x    = rc.X;
                for (rc.X = x1; rc.X < x2; rc.X += wid)
                {
                    word.DrawString(dots, bodyFont, Colors.Gray, rc);
                }
                rc.X = x;
#else
                // connect with a dotted line (another option)
                var x1 = rc.X + word.MeasureString(header, bodyFont).Width + 5;
                var x2 = rc.Right - word.MeasureString(page, bodyFont).Width - 5;
                var y  = rc.Top + bodyFont.Size;
                word.DrawLine(dottedPen, x1, y, x2, y);
#endif
                // add local hyperlink to entry
                //rtf.AddLink("#" + header, rc);

                // move on to next entry
                rc = WordUtils.Offset(rc, 0, rc.Height);
                if (rc.Bottom > rcPage.Bottom)
                {
                    word.PageBreak();
                    rc.Y = rcPage.Y;
                }
            }

            // move table of contents to start of document
            //var arr = new WordPage[rtf.Pages.Count - tocPage];
            //rtf.Pages.CopyTo(tocPage, arr, 0, arr.Length);
            //rtf.Pages.RemoveRange(tocPage, arr.Length);
            //rtf.Pages.InsertRange(0, arr);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // get stream to save to
            var dlg = new SaveFileDialog();

            dlg.DefaultExt = ".rtf";
            var dr = dlg.ShowDialog();

            if (!dr.HasValue || !dr.Value)
            {
                return;
            }

            // get sender button
            var btn = sender as Button;

            // create document
            var rtf = new C1WordDocument();

            rtf.Clear();

            // set document info
            var di = rtf.Info;

            di.Author  = "ComponentOne";
            di.Subject = "C1.WPF.Word demo.";
            di.Title   = "Experimental VisualTree Exporter for RTF";
            var count = 5;

            // walk visual tree
            CreateDocumentVisualTree(rtf, content);

            // render footers
            // this reopens each page and adds content to them (now we know the page count).
            var font = new Font("Arial", 8, RtfFontStyle.Bold);
            var fmt  = new StringFormat();

            fmt.Alignment     = HorizontalAlignment.Right;
            fmt.LineAlignment = VerticalAlignment.Bottom;
            for (int page = 0; page < count; page++)
            {
                //rtf.CurrentPage = page;
                var sz   = rtf.PageSize;
                var rc   = new Rect(72, 72, sz.Width - 144, sz.Height - 144);
                var text = string.Format("C1.WPF.Rtf: {0}, page {1} of {2}",
                                         di.Title,
                                         page + 1,
                                         count);
                var r = new Rect(72, 36, sz.Width - 144, sz.Height - 72);
                rtf.DrawString(
                    text,
                    font,
                    Colors.DarkGray,
                    r,
                    fmt);
            }

            // save document
            using (var stream = dlg.OpenFile())
            {
                rtf.Save(stream, FileFormat.Rtf);
            }
            MessageBox.Show("Word Document saved to " + dlg.SafeFileName);
        }
Esempio n. 10
0
        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.";
        }
        public static void SetDocumentInfo(this C1WordDocument doc, string title, bool graphicFooter = false)
        {
            // set document info
            var di = doc.Info;

            di.Author  = Strings.DocumentAuthor;
            di.Subject = Strings.DocumentSubject;
            di.Title   = title;

            // footer font
            var font = new Font("Arial", 8, RtfFontStyle.Bold);
            var fmt  = new StringFormat();

            fmt.Alignment     = HorizontalAlignment.Right;
            fmt.LineAlignment = VerticalAlignment.Bottom;

            // render footers
            if (graphicFooter)
            {
                // this reopens each page and adds content to them (now we know the page count).
                for (int page = 0; page < doc.PageCount(); page++)
                {
                    doc.CurrentPage(page);
                    var text = string.Format(Strings.Documentfooter,
                                             di.Title,
                                             page + 1,
                                             doc.PageCount());
                    doc.DrawString(
                        text,
                        font,
                        Colors.DarkGray,
                        WordUtils.Inflate(doc.PageRectangle(), -72, -36),
                        fmt);
                }
            }
            else
            {
                // standard footer
                var text      = string.Format(Strings.Documentfooter, di.Title, "|", "|");
                var paragraph = new RtfParagraph(doc.CurrentSection.Footer);
                paragraph.Alignment = RtfHorizontalAlignment.Right;
                int count = 0;
                foreach (var part in text.Split('|'))
                {
                    if (!string.IsNullOrEmpty(part))
                    {
                        paragraph.Add(new RtfString(part));
                    }
                    switch (count)
                    {
                    case 0:
                        paragraph.Add(new RtfPageField());
                        break;

                    case 1:
                        paragraph.Add(new RtfNumPagesField());
                        break;
                    }
                    count++;
                }
                doc.CurrentSection.Footer.Add(paragraph);
            }
        }