/// <summary> /// Render a cell in a word document /// </summary> /// <param name="cell"></param> /// <param name="document"></param> /// <param name="parent"></param> /// <param name="context"></param> /// <param name="documentPart"></param> /// <param name="isInAlternateRow"></param> /// <param name="formatProvider"></param> /// <returns></returns> public static TableCell Render(this Cell cell, Models.Document document, OpenXmlElement parent, ContextModel context, OpenXmlPart documentPart, bool isInAlternateRow, IFormatProvider formatProvider) { context.ReplaceItem(cell, formatProvider); TableCell wordCell = new TableCell(); TableCellProperties cellProp = new TableCellProperties(); wordCell.AppendChild(cellProp); if (isInAlternateRow) { cell.ReplaceAlternateConfiguration(); } cellProp.AddBorders(cell); cellProp.AddShading(cell); cellProp.AddMargin(cell); cellProp.AddJustifications(cell); if (cell.CellWidth != null) { cellProp.AppendChild(new TableCellWidth() { Width = cell.CellWidth.Width, Type = cell.CellWidth.Type.ToOOxml() }); } // manage cell column and row span if (cell.ColSpan > 1) { cellProp.AppendChild(new GridSpan() { Val = cell.ColSpan }); } if (cell.Fusion) { if (cell.FusionChild) { cellProp.AppendChild(new VerticalMerge() { Val = MergedCellValues.Continue }); } else { cellProp.AppendChild(new VerticalMerge() { Val = MergedCellValues.Restart }); } } if (!cell.Show) { return(wordCell); } if (cell.ChildElements.Any(x => x is Models.TemplateModel)) { // Need to replace elements : for (int i = 0; i < cell.ChildElements.Count; i++) { var e = cell.ChildElements[i]; if (e is TemplateModel) { var elements = (e as TemplateModel).ExtractTemplateItems(document); if (i == cell.ChildElements.Count - 1) { cell.ChildElements.AddRange(elements); } else { cell.ChildElements.InsertRange(i + 1, elements); } } } } if (cell.ChildElements.Any(x => x is Models.Paragraph) || cell.ChildElements.Any(x => x is ForEach)) { foreach (var element in cell.ChildElements) { element.InheritFromParent(cell); if (element is Models.Paragraph || element is ForEach) { element.Render(document, wordCell, context, documentPart, formatProvider); } else { var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); if (cell.Justification.HasValue) { var ppr = new ParagraphProperties(); ppr.AppendChild(new Justification() { Val = cell.Justification.Value.ToOOxml() }); paragraph.AppendChild(ppr); } wordCell.AppendChild(paragraph); var r = new Run(); paragraph.AppendChild(r); element.Render(document, r, context, documentPart, formatProvider); } } } else { // fill cell content (need at least an empty paragraph) var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(); if (cell.Justification.HasValue) { var ppr = new ParagraphProperties(); ppr.AppendChild(new Justification() { Val = cell.Justification.Value.ToOOxml() }); paragraph.AppendChild(ppr); } wordCell.AppendChild(paragraph); var r = new Run(); paragraph.AppendChild(r); foreach (var element in cell.ChildElements) { element.InheritFromParent(cell); element.Render(document, r, context, documentPart, formatProvider); } } return(wordCell); }