static Rect RenderTable(C1WordDocument word, Rect rc, Rect rcPage, DataTable table, string[] fields) { // select fonts Font hdrFont = new Font("Tahoma", 10, RtfFontStyle.Bold); Font txtFont = new Font("Tahoma", 8); // build table //word.AddBookmark(table.TableName, 0, rc.Y); rc = WordUtils.RenderParagraph(word, "NorthWind " + table.TableName, hdrFont, rcPage, rc, false); // build table rc = RenderTableHeader(word, hdrFont, rc, fields); foreach (DataRow dr in table.Rows) { rc = RenderTableRow(word, txtFont, hdrFont, rcPage, rc, fields, dr); } // done return(rc); }
//--------------------------------------------------------------------------------- #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; } } }
//--------------------------------------------------------------------------------- #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" }); }
//--------------------------------------------------------------------------------- #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); }