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)); }
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); } }