public Word.TableCell GenerateCell(string val, string w, int span = 0, bool bold = false, string sz = "12", string s = "style22", string f = "Courier New", Word.JustificationValues halign = Word.JustificationValues.Left) { Word.LeftMargin margin = new Word.LeftMargin { Type = new EnumValue <Word.TableWidthUnitValues>(Word.TableWidthUnitValues.Dxa), Width = new StringValue("10") }; Word.TableCellWidth width = new Word.TableCellWidth { Type = new EnumValue <Word.TableWidthUnitValues>(Word.TableWidthUnitValues.Dxa), Width = w }; Word.Shading shading = new Word.Shading { Fill = "auto", Val = new EnumValue <Word.ShadingPatternValues>(Word.ShadingPatternValues.Clear) }; Word.TableCellMargin cellmargin = new Word.TableCellMargin(margin); Word.VerticalTextAlignmentOnPage align = new Word.VerticalTextAlignmentOnPage { Val = new EnumValue <Word.VerticalJustificationValues>(Word.VerticalJustificationValues.Center) }; Word.GridSpan gspan = new Word.GridSpan { Val = span }; Word.TableCellProperties props = new Word.TableCellProperties(width, shading, cellmargin, align, gspan); Word.Paragraph paragraph = GenerateParagraph(val, bold, sz, s, f, halign); Word.TableCell cell = new Word.TableCell(props, paragraph); return(cell); }
public void TestAllRunProperties() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<p><span style='font-family:arial;font-weight:bold;text-decoration:underline;font-size:12px;font-style:italic;background-color:#ccc;color:#000'>test</span></p>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph paragraph = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(paragraph); Assert.AreEqual(1, paragraph.ChildElements.Count); Run run = paragraph.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); RunFonts fonts = properties.ChildElements[0] as RunFonts; Assert.IsNotNull(fonts); Assert.AreEqual("arial", fonts.Ascii.Value); Bold bold = properties.ChildElements[1] as Bold; Assert.IsNotNull(bold); Italic italic = properties.ChildElements[2] as Italic; Assert.IsNotNull(italic); Word.Color color = properties.ChildElements[3] as Word.Color; Assert.IsNotNull(color); Assert.AreEqual("000000", color.Val.Value); FontSize fontSize = properties.ChildElements[4] as FontSize; Assert.IsNotNull(fontSize); Assert.AreEqual("24", fontSize.Val.Value); Underline underline = properties.ChildElements[5] as Underline; Assert.IsNotNull(underline); Word.Shading shading = properties.ChildElements[6] as Word.Shading; Assert.IsNotNull(shading); Assert.AreEqual("cccccc", shading.Fill.Value); Assert.AreEqual(Word.ShadingPatternValues.Clear, shading.Val.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); errors.PrintValidationErrors(); Assert.AreEqual(0, errors.Count()); } }
public void TestRunBackgroundColor() { using MemoryStream mem = new MemoryStream(); WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<p><span style='background-color:#ccc;color:#000'>one</span><span>two</span></p>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Paragraph para = doc.Document.Body.ChildElements[0] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(2, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties properties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(properties); Word.Color color = properties.ChildElements[0] as Word.Color; Assert.IsNotNull(color); Assert.AreEqual("000000", color.Val.Value); Word.Shading shading = properties.ChildElements[1] as Word.Shading; Assert.IsNotNull(shading); Assert.AreEqual("cccccc", shading.Fill.Value); Assert.AreEqual(Word.ShadingPatternValues.Clear, shading.Val.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("one", text.InnerText); run = para.ChildElements[1] as Run; Assert.IsNotNull(run); Assert.AreEqual(1, run.ChildElements.Count); text = run.ChildElements[0] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual(0, text.ChildElements.Count); Assert.AreEqual("two", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); errors.PrintValidationErrors(); Assert.AreEqual(0, errors.Count()); }
private static TableCellProperties WordCellProperties(Campus.Report.Base.TableStyle style) { var leftBorder = new DocumentFormat.OpenXml.Wordprocessing.LeftBorder(); var rightBorder = new DocumentFormat.OpenXml.Wordprocessing.RightBorder(); var topBorder = new DocumentFormat.OpenXml.Wordprocessing.TopBorder(); var bottomBorder = new DocumentFormat.OpenXml.Wordprocessing.BottomBorder(); var insideHorizontalBorder = new DocumentFormat.OpenXml.Wordprocessing.InsideHorizontalBorder(); var insideVerticalBorder = new DocumentFormat.OpenXml.Wordprocessing.InsideVerticalBorder(); leftBorder.Val = WordLineType(style.BorderLine); rightBorder.Val = WordLineType(style.BorderLine); topBorder.Val = WordLineType(style.BorderLine); bottomBorder.Val = WordLineType(style.BorderLine); insideHorizontalBorder.Val = WordLineType(style.BorderLine); insideVerticalBorder.Val = WordLineType(style.BorderLine); leftBorder.Color = ColorToRgb(style.BorderColor); rightBorder.Color = ColorToRgb(style.BorderColor); topBorder.Color = ColorToRgb(style.BorderColor); bottomBorder.Color = ColorToRgb(style.BorderColor); insideHorizontalBorder.Color = ColorToRgb(style.BorderColor); insideVerticalBorder.Color = ColorToRgb(style.BorderColor); leftBorder.Size = WordBorderWaight(style.BorderLine); rightBorder.Size = WordBorderWaight(style.BorderLine); topBorder.Size = WordBorderWaight(style.BorderLine); bottomBorder.Size = WordBorderWaight(style.BorderLine); insideHorizontalBorder.Size = WordBorderWaight(style.BorderLine); insideVerticalBorder.Size = WordBorderWaight(style.BorderLine); var cellprop = new DocumentFormat.OpenXml.Wordprocessing.TableCellProperties(); var shading = new DocumentFormat.OpenXml.Wordprocessing.Shading(); shading.Val = DocumentFormat.OpenXml.Wordprocessing.ShadingPatternValues.Clear; shading.Fill = ColorToRgb(style.Foreground); var tableCellBorders = new DocumentFormat.OpenXml.Wordprocessing.TableCellBorders { LeftBorder = leftBorder, RightBorder = rightBorder, TopBorder = topBorder, BottomBorder = bottomBorder, InsideHorizontalBorder = insideHorizontalBorder, InsideVerticalBorder = insideVerticalBorder }; cellprop.Shading = shading; cellprop.TableCellBorders = tableCellBorders; return(cellprop); }
public void TableCellStyleInheritance() { using (MemoryStream mem = new MemoryStream()) { WordDocument doc = new WordDocument(mem); doc.Process(new HtmlParser("<table><tr><td style='border:1px solid #000;background-color:red'>test</td></tr></table>")); Assert.IsNotNull(doc.Document.Body); Assert.AreEqual(1, doc.Document.Body.ChildElements.Count); Word.Table table = doc.Document.Body.ChildElements[0] as Word.Table; Assert.IsNotNull(table); Assert.AreEqual(3, table.ChildElements.Count); TableRow row = table.ChildElements[2] as TableRow; Assert.IsNotNull(row); Assert.AreEqual(1, row.ChildElements.Count); TableCell cell = row.ChildElements[0] as TableCell; Assert.IsNotNull(cell); Assert.AreEqual(2, cell.ChildElements.Count); TableCellProperties cellProperties = cell.ChildElements[0] as TableCellProperties; Assert.IsNotNull(cellProperties); Assert.AreEqual(2, cellProperties.ChildElements.Count); TableCellBorders borders = cellProperties.ChildElements[0] as TableCellBorders; Assert.IsNotNull(borders); Assert.AreEqual(4, borders.ChildElements.Count); TopBorder topBorder = borders.ChildElements[0] as TopBorder; Assert.IsNotNull(topBorder); TestUtility.TestBorder <TopBorder>(topBorder, BorderValues.Single, "000000", 1U); LeftBorder leftBorder = borders.ChildElements[1] as LeftBorder; Assert.IsNotNull(leftBorder); TestUtility.TestBorder <LeftBorder>(leftBorder, BorderValues.Single, "000000", 1U); BottomBorder bottomBorder = borders.ChildElements[2] as BottomBorder; Assert.IsNotNull(bottomBorder); TestUtility.TestBorder <BottomBorder>(bottomBorder, BorderValues.Single, "000000", 1U); RightBorder rightBorder = borders.ChildElements[3] as RightBorder; Assert.IsNotNull(rightBorder); TestUtility.TestBorder <RightBorder>(rightBorder, BorderValues.Single, "000000", 1U); Word.Shading backgroundColor = cellProperties.ChildElements[1] as Word.Shading; Assert.IsNotNull(backgroundColor); Assert.AreEqual("FF0000", backgroundColor.Fill.Value); Paragraph para = cell.ChildElements[1] as Paragraph; Assert.IsNotNull(para); Assert.AreEqual(1, para.ChildElements.Count); Run run = para.ChildElements[0] as Run; Assert.IsNotNull(run); Assert.AreEqual(2, run.ChildElements.Count); RunProperties runProperties = run.ChildElements[0] as RunProperties; Assert.IsNotNull(runProperties); Word.Shading shading = runProperties.ChildElements[0] as Word.Shading; Assert.AreEqual("FF0000", shading.Fill.Value); Word.Text text = run.ChildElements[1] as Word.Text; Assert.IsNotNull(text); Assert.AreEqual("test", text.InnerText); OpenXmlValidator validator = new OpenXmlValidator(); var errors = validator.Validate(doc.WordprocessingDocument); Assert.AreEqual(0, errors.Count()); } }
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"); } }
private TableCell CreateTableCell(string contents, string cell_color = "ffffff", int width = 0) { // Create the TableCell object TableCell tc = new TableCell(); TableCellProperties tcp; if (width == 0) { // Create the TableCellProperties object tcp = new TableCellProperties( new TableCellWidth { Type = TableWidthUnitValues.Auto } ); } else { tcp = new TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = width.ToString() } ); } // Create the Shading object DocumentFormat.OpenXml.Wordprocessing.Shading shading = new DocumentFormat.OpenXml.Wordprocessing.Shading() { Color = "auto", Fill = cell_color, Val = ShadingPatternValues.Clear }; // Add the Shading object to the TableCellProperties object tcp.Append(shading); // Add the TableCellProperties object to the TableCell object tc.PrependChild<TableCellProperties>(tcp); // also need to ensure you include the text, otherwise it causes an error (it did for me!) Text text = new Text(contents); // Specify the table cell content. Run run = new Run(); RunProperties runProp = new RunProperties(); // Create run properties. RunFonts runFont = new RunFonts(); // Create font runFont.Ascii = "Arial"; // Specify font family FontSize size = new FontSize(); size.Val = new StringValue("22"); // 48 half-point font size runProp.Append(runFont); runProp.Append(size); run.PrependChild<RunProperties>(runProp); run.Append(text); ParagraphProperties pProperties = new ParagraphProperties(); ContextualSpacing cs = new ContextualSpacing(); pProperties.Append(cs); Paragraph paragraph = new Paragraph(run); paragraph.PrependChild<ParagraphProperties>(pProperties); tc.Append(paragraph); return tc; }
internal Shading(DocumentFormat.OpenXml.Wordprocessing.Shading shading) { this.shading = shading; }