public void ParentTypeConstraintTest() { Word.TableProperties tblPr = new Word.TableProperties(); ValidationContext context = new ValidationContext() { Element = tblPr }; ParentTypeConstraint constraint = new ParentTypeConstraint() { ParentType = typeof(Word.Table) }; Assert.Null(constraint.Validate(context)); Word.Table tbl = new Word.Table(); tbl.Append(tblPr); Assert.Null(constraint.Validate(context)); tbl.RemoveAllChildren(); Word.Paragraph p = new Word.Paragraph(); p.Append(tblPr); Assert.NotNull(constraint.Validate(context)); }
private static void UpdateWordBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options) { if (null != content && block is OXW.Table) { OXW.Table table = ((OXW.Table)block).CloneNode(true) as OXW.Table; OXW.TableRow headerRowTemplate = table.Descendants <OXW.TableRow>().First().CloneNode(true) as OXW.TableRow; OXW.TableRow contentRowTemplate = table.Descendants <OXW.TableRow>().Skip(1).First().CloneNode(true) as OXW.TableRow; #region Column number management OXW.TableGrid tablegrid = table.Descendants <OXW.TableGrid>().FirstOrDefault(); if (null != tablegrid) { List <OXW.GridColumn> columns = tablegrid.Descendants <OXW.GridColumn>().ToList(); if (null != columns && content.NbColumns != columns.Count) { if (content.NbColumns < columns.Count) { for (int i = columns.Count - 1, lim = content.NbColumns - 1; i > lim; i--) { tablegrid.RemoveChild <OXW.GridColumn>(columns[i]); } } else { for (int i = 0, lim = content.NbColumns - columns.Count; i < lim; i++) { tablegrid.AppendChild <OXW.GridColumn>(new OXW.GridColumn() { Width = "1000" }); } } } } #endregion Column number management ModifyWordRowTextContent(headerRowTemplate, string.Empty); ModifyWordRowTextContent(contentRowTemplate, string.Empty); int idx = 0; int nbrow = 0; List <OXW.TableCell> headerCells = headerRowTemplate.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); List <OXW.TableCell> contentCells = contentRowTemplate.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); headerRowTemplate.RemoveAllChildren <OXW.TableCell>(); OXW.TableRow row = headerRowTemplate; int headerCellsCount = headerCells.Count; int contentCellsCount = headerCells.Count; table.RemoveAllChildren <OXW.TableRow>(); foreach (var item in content.Data) { if (null != item) { OXW.TableCell cell = null; if (content.HasColumnHeaders && 0 == nbrow) { cell = headerCells[idx % headerCellsCount].CloneNode(true) as OXW.TableCell; } else { cell = contentCells[idx % contentCellsCount].CloneNode(true) as OXW.TableCell; } ModifyWordCellTextContent(cell, item); row.Append(cell); } idx = ++idx % content.NbColumns; if (0 == idx) { if (null != row) { table.Append(row); nbrow++; } row = contentRowTemplate.CloneNode(true) as OXW.TableRow; row.RemoveAllChildren <OXW.TableCell>(); } } var blockSdt = block.Ancestors <OXW.SdtBlock>().First(); blockSdt.Parent.ReplaceChild(table, blockSdt); } else { LogHelper.Instance.LogErrorFormat("Impossible to load data in Table block with a block source of type \"{0}\"", null != block ? block.GetType().ToString() : "null"); } }
public void ParentTypeConstraintTest() { Word.TableProperties tblPr = new Word.TableProperties(); ValidationContext context = new ValidationContext() { Element = tblPr }; ParentTypeConstraint constraint = new ParentTypeConstraint() { ParentType = typeof(Word.Table) }; Assert.Null(constraint.Validate(context)); Word.Table tbl = new Word.Table(); tbl.Append(tblPr); Assert.Null(constraint.Validate(context)); tbl.RemoveAllChildren(); Word.Paragraph p = new Word.Paragraph(); p.Append(tblPr); Assert.NotNull(constraint.Validate(context)); }
private static void UpdateWordBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options) { if (null != content && block is OXW.Table) { OXW.Table table = ((OXW.Table)block).CloneNode(true) as OXW.Table; OXW.TableRow headerRowTemplate = table?.Descendants <OXW.TableRow>().First().CloneNode(true) as OXW.TableRow; OXW.TableRow contentRowTemplate = table?.Descendants <OXW.TableRow>().Skip(1).First().CloneNode(true) as OXW.TableRow; #region Column number management OXW.TableGrid tablegrid = table?.Descendants <OXW.TableGrid>().FirstOrDefault(); List <OXW.GridColumn> columns = tablegrid?.Descendants <OXW.GridColumn>().ToList(); if (columns != null && content.NbColumns != columns.Count) { if (content.NbColumns < columns.Count) { for (int i = columns.Count - 1, lim = content.NbColumns - 1; i > lim; i--) { tablegrid.RemoveChild(columns[i]); } } else { for (int i = 0, lim = content.NbColumns - columns.Count; i < lim; i++) { tablegrid.AppendChild(new OXW.GridColumn() { Width = "200" }); } } } #endregion Column number management ModifyWordRowTextContent(headerRowTemplate, string.Empty, string.Empty, string.Empty); ModifyWordRowTextContent(contentRowTemplate, string.Empty, string.Empty, string.Empty); int idx = 0; int nbrow = 0; List <OXW.TableCell> headerCells = headerRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); List <OXW.TableCell> contentCells = contentRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList(); headerRowTemplate?.RemoveAllChildren <OXW.TableCell>(); OXW.TableRow row = headerRowTemplate; if (headerCells != null) { int headerCellsCount = headerCells.Count; int contentCellsCount = headerCells.Count; table.RemoveAllChildren <OXW.TableRow>(); for (int i = 0; i < content.Data.Count(); i++) { var item = content.Data.ToArray()[i]; if (null != item) { OXW.TableCell cell; if (content.HasColumnHeaders && 0 == nbrow) { cell = headerCells[idx % headerCellsCount].CloneNode(true) as OXW.TableCell; } else { cell = contentCells?[idx % contentCellsCount].CloneNode(true) as OXW.TableCell; } string txtColor = string.Empty; string effect = string.Empty; if (content.HasCellsAttributes()) { CellAttributes attributes = content.CellsAttributes.FirstOrDefault(a => a.Index == i); if (attributes != null) { OXW.TableCellProperties tcp = new OXW.TableCellProperties( new OXW.TableCellWidth { Type = OXW.TableWidthUnitValues.Auto, } ); // Create the Shading object OXW.Shading shading = new OXW.Shading() { Color = "auto", Fill = ColorTranslator.ToHtml(attributes.BackgroundColor), Val = OXW.ShadingPatternValues.Clear }; // Add the Shading object to the TableCellProperties object tcp.Append(shading); // Add the TableCellProperties object to the TableCell object cell?.Append(tcp); txtColor = $"{attributes.FontColor.R:X2}{attributes.FontColor.G:X2}{attributes.FontColor.B:X2}"; effect = attributes.Effect; } } ModifyWordCellTextContent(cell, item, txtColor, effect); row?.Append(cell); } idx = ++idx % content.NbColumns; if (0 != idx) { continue; } if (null != row) { table.Append(row); nbrow++; } row = contentRowTemplate?.CloneNode(true) as OXW.TableRow; row?.RemoveAllChildren <OXW.TableCell>(); } } var blockSdtAncestor = block.Ancestors <OXW.SdtBlock>(); if (0 != blockSdtAncestor.ToList().Count) { // case table is in a content control var blockStd = block.Ancestors <OXW.SdtBlock>().First(); blockStd.Parent.ReplaceChild(table, blockStd); } else { // case table is directly in the document var blockStd = block; blockStd.Parent.ReplaceChild(table, blockStd); } } else { LogHelper.Instance.LogErrorFormat("Impossible to load data in Table block with a block source of type \"{0}\"", block?.GetType().ToString() ?? "null"); } }