// ***********************************************************************
        // 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);
        }
        static void CreateDocumentPaperSizes(C1WordDocument word)
        {
            // landscape for first page
            bool landscape = true;

            word.Landscape = landscape;

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

            word.AddParagraph(Strings.PaperSizesFirstPage, titleFont);
            var paragraph = (RtfParagraph)word.Current;

            paragraph.BottomBorderColor = Colors.Purple;
            paragraph.BottomBorderStyle = RtfBorderStyle.Dotted;
            paragraph.BottomBorderWidth = 3.0f;

            // view warning
            Font warningFont = new Font("Arial", 14);

            word.AddParagraph(Strings.PaperSizesWarning, warningFont, Colors.Blue);

            // create constant font and StringFormat objects
            Font         font = new Font("Tahoma", 18);
            StringFormat sf   = new StringFormat();

            sf.Alignment     = HorizontalAlignment.Center;
            sf.LineAlignment = VerticalAlignment.Center;
            word.AddParagraph(string.Empty, font, Colors.Black);

            // create one page with each paper size
            foreach (PaperKind pk in Enum.GetValues(typeof(PaperKind)))
            {
                // skip custom page size
                if (pk == PaperKind.Custom)
                {
                    continue;
                }

                // add new page for every page after the first one
                word.PageBreak();

                // set paper kind and orientation
                var section = new RtfSection(pk);
                word.Add(section);
                landscape      = !landscape;
                word.Landscape = landscape;

                // add some content on the page
                string text = string.Format(Strings.StringFormatTwoArg, pk, word.Landscape);
                word.AddParagraph(text);
                paragraph = (RtfParagraph)word.Current;
                paragraph.SetRectBorder(RtfBorderStyle.DashSmall, Colors.Aqua, 2.0f);
                word.AddParagraph(string.Empty);
            }
        }
Beispiel #3
0
        //---------------------------------------------------------------------------------
        #region ** paper sizes

        static void CreateDocumentPaperSizes(C1WordDocument word)
        {
            // landscape for first page
            bool landscape = true;

            word.Landscape = landscape;

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

            word.AddParagraph(word.Info.Title, titleFont);
            var paragraph = (RtfParagraph)word.Current;

            paragraph.BottomBorderColor = Colors.Purple;
            paragraph.BottomBorderStyle = RtfBorderStyle.Dotted;
            paragraph.BottomBorderWidth = 3.0f;

            // create constant font and StringFormat objects
            Font         font = new Font("Tahoma", 18);
            StringFormat sf   = new StringFormat();

            sf.Alignment     = HorizontalAlignment.Center;
            sf.LineAlignment = VerticalAlignment.Center;
            word.AddParagraph("By default used Landscape A4", font);
            word.AddParagraph(string.Empty, font);

            // create one page with each paper size
            foreach (var fi in typeof(PaperKind).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                // Silverlight/Phone doesn't have Enum.GetValues
                PaperKind pk = (PaperKind)fi.GetValue(null);

                // skip custom size
                if (pk == PaperKind.Custom)
                {
                    continue;
                }

                // add new page for every page after the first one
                word.PageBreak();

                // set paper kind and orientation
                var section = new RtfSection(pk);
                word.Add(section);
                landscape      = !landscape;
                word.Landscape = landscape;

                // add some content on the page
                string text = string.Format("PaperKind: [{0}];\r\nLandscape: [{1}];\r\nFont: [Tahoma 18pt]", pk, word.Landscape);
                word.AddParagraph(text);
                paragraph = (RtfParagraph)word.Current;
                paragraph.SetRectBorder(RtfBorderStyle.DashSmall, Colors.Aqua, 2.0f);
                word.AddParagraph(string.Empty);
            }
        }
Beispiel #4
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));
        }
Beispiel #5
0
        static void CreateDocumentQuotes(C1WordDocument word)
        {
            // calculate page rect (discounting margins)
            Rect rcPage = WordUtils.PageRectangle(word);
            var  height = rcPage.Top;

            // initialize fonts
            Font hdrFont   = new Font("Arial", 14, RtfFontStyle.Bold);
            Font titleFont = new Font("Arial", 20, RtfFontStyle.Bold);
            Font txtFont   = new Font("Times New Roman", 10, RtfFontStyle.Italic);

            // add title paragraph
            word.AddParagraph(Strings.QuotesAuthors, titleFont, Colors.DeepPink, RtfHorizontalAlignment.Center);
            word.AddParagraph(string.Empty);
            height += 2 * word.MeasureString(Strings.QuotesAuthors, titleFont, rcPage.Width).Height;

            // build document
            foreach (string s in GetQuotes())
            {
                // quote
                var authorQuote = s.Split('\t');
                var author      = authorQuote[0];
                var text        = authorQuote[1];

                // if it won't fit this page, do a page break
                var h  = word.MeasureString(author, hdrFont, rcPage.Width).Height;
                var sf = new StringFormat();
                sf.Alignment = HorizontalAlignment.Stretch;
                h           += word.MeasureString(text, txtFont, rcPage.Width, sf).Height;
                h           += 20;
                if (height + h > rcPage.Bottom)
                {
                    word.PageBreak();
                    height = rcPage.Top;
                }
                height += h;

                // render header (author)
                word.AddParagraph(author, hdrFont, Colors.Black, RtfHorizontalAlignment.Left);
                var paragraph = (RtfParagraph)word.Current;
                paragraph.TopBorderColor = Colors.Black;
                paragraph.TopBorderStyle = RtfBorderStyle.Single;
                paragraph.TopBorderWidth = 1f;

                // render body text (quote)
                word.AddParagraph(text, txtFont, Colors.DarkSlateGray, RtfHorizontalAlignment.Left);
                paragraph            = (RtfParagraph)word.Current;
                paragraph.LeftIndent = 40.0f;
                word.AddParagraph(string.Empty);
            }
        }
Beispiel #6
0
        //---------------------------------------------------------------------------------
        #region ** tables

        static void CreateDocumentTables(C1WordDocument word)
        {
            // get the data
            var ds = DataAccess.GetDataSet();

            // calculate page rect (discounting margins)
            Rect rcPage = WordUtils.PageRectangle(word);
            Rect rc     = rcPage;

            // add title
            Font titleFont = new Font("Tahoma", 24, RtfFontStyle.Bold);

            rc = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rc, false);

            // render some tables
            RenderTable(word, rc, rcPage, ds.Tables["Customers"], new string[] { "CompanyName", "ContactName", "Country", "Address", "Phone" });
            word.PageBreak();
            rc = rcPage;
            RenderTable(word, rc, rcPage, ds.Tables["Products"], new string[] { "ProductName", "QuantityPerUnit", "UnitPrice", "UnitsInStock", "UnitsOnOrder" });
            word.PageBreak();
            rc = rcPage;
            RenderTable(word, rc, rcPage, ds.Tables["Employees"], new string[] { "FirstName", "LastName", "Country", "Notes" });
        }
Beispiel #7
0
        private void _btMetafile_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter           = "Extended metafile (*.emf)|*.emf|Windows metafile (*.wmf)|*.wmf|All files (*.*)|*.*";
            dlg.FilterIndex      = 1;
            dlg.RestoreDirectory = true;
            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            // create document
            C1WordDocument c1Word = new C1WordDocument();

            c1Word.Info.Title = "Convert metafile to RTF example";
            _statusBar.Text   = "Creating document...";

            Image  img;
            string ext = Path.GetExtension(dlg.FileName);

            if (ext == ".wmf" || ext == ".emf")
            {
                img = Metafile.FromFile(dlg.FileName);
            }
            else
            {
                throw new FormatException("Not metafile.");
            }
            c1Word.DrawMetafile((Metafile)img);

            c1Word.PageBreak();

            c1Word.AddPicture(img, RtfHorizontalAlignment.Left);

            c1Word.LineBreak();

            Font font = new Font("Arial", 10, FontStyle.Regular);

            c1Word.AddParagraph(dlg.FileName, font, Color.Black);

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

            c1Word.Save(fileName);
            Process.Start(fileName);
            _statusBar.Text = "Ready.";
        }
Beispiel #8
0
        //---------------------------------------------------------------------------------
        #region ** quotes

        static void CreateDocumentQuotes(C1WordDocument word)
        {
            // calculate page rect (discounting margins)
            Rect rcPage = WordUtils.PageRectangle(word);
            Rect rc     = rcPage;

            // initialize output parameters
            Font hdrFont   = new Font("Arial", 14, RtfFontStyle.Bold);
            Font titleFont = new Font("Arial", 24, RtfFontStyle.Bold);
            Font txtFont   = new Font("Times New Roman", 10, RtfFontStyle.Italic);

            // add title
            var rcTop = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rc);

            rc = rcTop;

            // build document
            foreach (string s in GetQuotes())
            {
                string[] authorQuote = s.Split('\t');

                // render header (author)
                var author = authorQuote[0];
                rc.Y += 25;
                rc    = WordUtils.RenderParagraph(word, author, hdrFont, rcPage, rc, true);

                // render body text (quote)
                string text = authorQuote[1];
                rc.X     = rcPage.X + 36; // << indent body text by 1/2 inch
                rc.Width = rcPage.Width - 40;
                rc       = WordUtils.RenderParagraph(word, text, txtFont, rcPage, rc);
                rc.X     = rcPage.X; // << restore indent
                rc.Width = rcPage.Width;
                rc.Y    += 12;       // << add 12pt spacing after each quote
                if (rc.Y > rcPage.Height)
                {
                    word.PageBreak();
                    rc = rcTop;
                }
            }
        }
        static void CreateDocumentTOC(C1WordDocument doc)
        {
            // calculate page rect (discounting margins)
            Rect rcPage = WordUtils.PageRectangle(doc);
            var  height = rcPage.Top;

            // initialize fonts
            Font titleFont  = new Font("Tahoma", 20, RtfFontStyle.Bold);
            Font headerFont = new Font("Arial", 14, RtfFontStyle.Bold);
            Font bodyFont   = new Font("Times New Roman", 11);

            // add title
            doc.AddParagraph(Strings.TableOfContentsDocumentTitle, titleFont, Colors.DeepPink, RtfHorizontalAlignment.Center);
            doc.AddParagraph(string.Empty);

            // create context of the document
            var pageIndex = 2;
            var data      = new List <List <string> >();

            for (int i = 0; i < 30; i++)
            {
                // iniialization data
                var list = new List <string>();
                data.Add(list);

                // create it header (as a link target and outline entry)
                string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle());
                var    h      = 2 * doc.MeasureString(header, headerFont, rcPage.Width).Height;

                // create some text
                for (int j = 0; j < 3 + _rnd.Next(20); j++)
                {
                    // some paragraph
                    string text = BuildRandomParagraph();
                    var    sf   = new StringFormat();
                    sf.Alignment = HorizontalAlignment.Stretch;
                    h           += doc.MeasureString(text, bodyFont, rcPage.Width, sf).Height;
                    h           += 6;

                    // test next page
                    if (height + h > rcPage.Bottom)
                    {
                        pageIndex++;
                        height = rcPage.Top;
                    }
                    height += h;
                    h       = 0;

                    // page index for header
                    if (j == 0)
                    {
                        list.Add(pageIndex.ToString());
                        list.Add(header.ToString());
                    }

                    // page index for paragraph
                    list.Add(pageIndex.ToString());
                    list.Add(text.ToString());
                }

                // last line
                height += 20;
            }

            // render Table of Contents
            RtfTable table = new RtfTable(data.Count, 2);

            doc.Add(table);
            for (int row = 0; row < data.Count; row++)
            {
                // get bookmark info
                var list   = data[row];
                var page   = list[0];
                var header = list[1];

                // add cells
                table.Rows[row].Cells[0].Content.Add(new RtfString(header));
                //var hlink = new RtfHyperlink(string.Format("hdr{0}", 1 + row));
                //hlink.Content.Add(new RtfString(page));
                //table.Rows[row].Cells[1].Content.Add(hlink);
                table.Rows[row].Cells[1].Content.Add(new RtfString(page));

                // set attributes
                table.Rows[row].Cells[0].Alignment = ContentAlignment.BottomLeft;
                table.Rows[row].Cells[1].Alignment = ContentAlignment.BottomRight;
                table.Rows[row].BottomBorderWidth  = 1;
                table.Rows[row].BottomBorderStyle  = RtfBorderStyle.Dotted;
            }

            // next page
            doc.PageBreak();
            pageIndex = 1;

            // render contents
            foreach (var list in data)
            {
                for (int i = 0; i < list.Count; i += 2)
                {
                    int page = int.Parse(list[i]);
                    if (page != pageIndex)
                    {
                        doc.PageBreak();
                        pageIndex = page;
                    }
                    var text = list[i + 1];
                    if (i == 0)
                    {
                        // header
                        //doc.AddBookmark(string.Format("hdr{0}", 1 + data.IndexOf(list)));
                        doc.AddParagraph(text, headerFont, Colors.DarkGray, RtfHorizontalAlignment.Center);
                        var paragraph = (RtfParagraph)doc.Current;
                        paragraph.TopBorderColor = Colors.DarkGray;
                        paragraph.TopBorderStyle = RtfBorderStyle.Single;
                        paragraph.TopBorderWidth = 1f;
                        doc.AddParagraph(string.Empty, bodyFont, Colors.Black);
                    }
                    else
                    {
                        // context
                        doc.AddParagraph(text, bodyFont, Colors.Black, RtfHorizontalAlignment.Justify);
                    }
                }
                doc.AddParagraph(string.Empty, bodyFont, Colors.Black);
            }
        }
Beispiel #10
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);
        }