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); }
private static BorderStyle ToCellBorderStyle(this Word.TableCellBorders borders) { if (borders == null) { return(null); } var top = borders.TopBorder.ToPen(); var right = borders.RightBorder.ToPen(); var bottom = borders.BottomBorder.ToPen(); var left = borders.LeftBorder.ToPen(); return(new BorderStyle(top, right, bottom, left)); }
/// <summary> /// The write table. /// </summary> /// <param name="t"> /// The t. /// </param> public void WriteTable(Table t) { this.body.Append(CreateParagraph(t.GetFullCaption(this.style), TableCaptionID)); var table = new DocumentFormat.OpenXml.Wordprocessing.Table(); var tableProperties1 = new TableProperties(); var tableStyle1 = new TableStyle { Val = "TableGrid" }; var tableWidth1 = new TableWidth { Width = "0", Type = TableWidthUnitValues.Auto }; var tableLook1 = new TableLook { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); tableProperties1.Append(tableLook1); var tableGrid1 = new TableGrid(); foreach (var tc in t.Columns) { // tc.Width var gridColumn1 = new GridColumn { Width = "3070" }; tableGrid1.Append(gridColumn1); } foreach (var row in t.Rows) { var tr = new TableRow(); if (row.IsHeader) { var trp = new TableRowProperties(); var tableHeader1 = new TableHeader(); trp.Append(tableHeader1); tr.Append(trp); } int j = 0; foreach (var c in row.Cells) { bool isHeader = row.IsHeader || t.Columns[j++].IsHeader; var cell = new TableCell(); var tcp = new TableCellProperties(); var borders = new TableCellBorders(); borders.Append( new BottomBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); borders.Append( new TopBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); borders.Append( new LeftBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); borders.Append( new RightBorder { Val = BorderValues.Single, Size = (UInt32Value)4U, Space = (UInt32Value)0U, Color = "auto" }); tcp.Append(borders); cell.Append(tcp); string styleID = isHeader ? "TableHeader" : "TableText"; cell.Append(CreateParagraph(c.Content, styleID)); tr.Append(cell); } table.Append(tr); } this.body.Append(table); }
/// <summary> /// The write table. /// </summary> /// <param name="t">The t.</param> public void WriteTable(Table t) { this.body.AppendChild(CreateParagraph(t.GetFullCaption(this.style), TableCaptionId)); var table = new DocumentFormat.OpenXml.Wordprocessing.Table(); var tableProperties1 = new TableProperties(); var tableStyle1 = new TableStyle { Val = "TableGrid" }; var tableWidth1 = new TableWidth { Width = "0", Type = TableWidthUnitValues.Auto }; var tableLook1 = new TableLook { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true }; tableProperties1.AppendChild(tableStyle1); tableProperties1.AppendChild(tableWidth1); tableProperties1.AppendChild(tableLook1); var tableGrid1 = new TableGrid(); // ReSharper disable once UnusedVariable foreach (var tc in t.Columns) { // TODO: use tc.Width to set the width of the column var gridColumn1 = new GridColumn { Width = "3070" }; tableGrid1.AppendChild(gridColumn1); } foreach (var row in t.Rows) { var tr = new TableRow(); if (row.IsHeader) { var trp = new TableRowProperties(); var tableHeader1 = new TableHeader(); trp.AppendChild(tableHeader1); tr.AppendChild(trp); } int j = 0; foreach (var c in row.Cells) { bool isHeader = row.IsHeader || t.Columns[j++].IsHeader; var cell = new TableCell(); var tcp = new TableCellProperties(); var borders = new TableCellBorders(); borders.AppendChild( new BottomBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); borders.AppendChild( new TopBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); borders.AppendChild( new LeftBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); borders.AppendChild( new RightBorder { Val = BorderValues.Single, Size = 4U, Space = 0U, Color = "auto" }); tcp.AppendChild(borders); cell.AppendChild(tcp); string styleId = isHeader ? "TableHeader" : "TableText"; cell.AppendChild(CreateParagraph(c.Content, styleId)); tr.AppendChild(cell); } table.AppendChild(tr); } this.body.AppendChild(table); }
/// <summary> /// /// </summary> /// <param name="cellbr"></param> /// <param name="firstColumn">Is current cell at the first column?</param> /// <param name="lastColumn">Is current cell at the last column?</param> /// <param name="firstRow">Is current cell at the first row?</param> /// <param name="lastRow">Is current cell at the first row?</param> /// <returns></returns> private Word.TableCellBorders applyCellBorders(Word.TableCellBorders cellbr, bool firstColumn, bool lastColumn, bool firstRow, bool lastRow) { Word.TableCellBorders ret = new Word.TableCellBorders() { TopBorder = new Word.TopBorder(), BottomBorder = new Word.BottomBorder(), LeftBorder = new Word.LeftBorder(), RightBorder = new Word.RightBorder(), InsideHorizontalBorder = new Word.InsideHorizontalBorder(), InsideVerticalBorder = new Word.InsideVerticalBorder(), TopLeftToBottomRightCellBorder = new Word.TopLeftToBottomRightCellBorder(), TopRightToBottomLeftCellBorder = new Word.TopRightToBottomLeftCellBorder(), }; // cell border first, if no cell border then conditional formating (table border + style) if (cellbr != null && cellbr.TopBorder != null) { StyleHelper.CopyAttributes(ret.TopBorder, cellbr.TopBorder); } else { if (this.condFmtbr != null && this.condFmtbr.TopBorder != null) { StyleHelper.CopyAttributes(ret.TopBorder, this.condFmtbr.TopBorder); } if (!firstRow && (this.condFmtbr != null && this.condFmtbr.InsideHorizontalBorder != null)) { StyleHelper.CopyAttributes(ret.TopBorder, this.condFmtbr.InsideHorizontalBorder); } } if (cellbr != null && cellbr.BottomBorder != null) { StyleHelper.CopyAttributes(ret.BottomBorder, cellbr.BottomBorder); } else { if (this.condFmtbr != null && this.condFmtbr.BottomBorder != null) { StyleHelper.CopyAttributes(ret.BottomBorder, this.condFmtbr.BottomBorder); } if (!lastRow && (this.condFmtbr != null && this.condFmtbr.InsideHorizontalBorder != null)) { StyleHelper.CopyAttributes(ret.BottomBorder, this.condFmtbr.InsideHorizontalBorder); } } if (cellbr != null && cellbr.LeftBorder != null) { StyleHelper.CopyAttributes(ret.LeftBorder, cellbr.LeftBorder); } else { if (this.condFmtbr != null && this.condFmtbr.LeftBorder != null) { StyleHelper.CopyAttributes(ret.LeftBorder, this.condFmtbr.LeftBorder); } if (!firstColumn && (this.condFmtbr != null && this.condFmtbr.InsideVerticalBorder != null)) { StyleHelper.CopyAttributes(ret.LeftBorder, this.condFmtbr.InsideVerticalBorder); } } if (cellbr != null && cellbr.RightBorder != null) { StyleHelper.CopyAttributes(ret.RightBorder, cellbr.RightBorder); } else { if (this.condFmtbr != null && this.condFmtbr.RightBorder != null) { StyleHelper.CopyAttributes(ret.RightBorder, this.condFmtbr.RightBorder); } if (!lastColumn && (this.condFmtbr != null && this.condFmtbr.InsideVerticalBorder != null)) { StyleHelper.CopyAttributes(ret.RightBorder, this.condFmtbr.InsideVerticalBorder); } } if (cellbr != null && cellbr.TopLeftToBottomRightCellBorder != null) { StyleHelper.CopyAttributes(ret.TopLeftToBottomRightCellBorder, cellbr.TopLeftToBottomRightCellBorder); } if (cellbr != null && cellbr.TopRightToBottomLeftCellBorder != null) { StyleHelper.CopyAttributes(ret.TopRightToBottomLeftCellBorder, cellbr.TopRightToBottomLeftCellBorder); } return(ret); }
/// <summary> /// Compare border and return who is win. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="dir"></param> /// <returns>Return ture means a win, false means b win.</returns> private bool compareBorder(Word.TableCellBorders a, Word.TableCellBorders b, compareDirection dir) { // compare line style int weight1 = 0, weight2 = 0; if (dir == compareDirection.Horizontal) { if (a.RightBorder != null && a.RightBorder.Val != null) { weight1 = (BorderNumber.ContainsKey(a.RightBorder.Val)) ? BorderNumber[a.RightBorder.Val] : 1; } else if (a.InsideVerticalBorder != null && a.InsideVerticalBorder.Val != null) { weight1 = (BorderNumber.ContainsKey(a.InsideVerticalBorder.Val)) ? BorderNumber[a.InsideVerticalBorder.Val] : 1; } if (b.LeftBorder != null && b.LeftBorder.Val != null) { weight2 = (BorderNumber.ContainsKey(b.LeftBorder.Val)) ? BorderNumber[b.LeftBorder.Val] : 1; } else if (b.InsideVerticalBorder != null && b.InsideVerticalBorder.Val != null) { weight2 = (BorderNumber.ContainsKey(b.InsideVerticalBorder.Val)) ? BorderNumber[b.InsideVerticalBorder.Val] : 1; } } else if (dir == compareDirection.Vertical) { if (a.BottomBorder != null && a.BottomBorder.Val != null) { weight1 = (BorderNumber.ContainsKey(a.BottomBorder.Val)) ? BorderNumber[a.BottomBorder.Val] : 1; } else if (a.InsideHorizontalBorder != null && a.InsideHorizontalBorder.Val != null) { weight1 = (BorderNumber.ContainsKey(a.InsideHorizontalBorder.Val)) ? BorderNumber[a.InsideHorizontalBorder.Val] : 1; } if (b.TopBorder != null && b.TopBorder.Val != null) { weight2 = (BorderNumber.ContainsKey(b.TopBorder.Val)) ? BorderNumber[b.TopBorder.Val] : 1; } else if (b.InsideHorizontalBorder != null && b.InsideHorizontalBorder.Val != null) { weight2 = (BorderNumber.ContainsKey(b.InsideHorizontalBorder.Val)) ? BorderNumber[b.InsideHorizontalBorder.Val] : 1; } } if (weight1 > weight2) { return(true); } else if (weight2 > weight1) { return(false); } // compare width float size1 = 0f, size2 = 0f; if (dir == compareDirection.Horizontal) { if (a.RightBorder.Size != null && a.RightBorder.Size.HasValue) { size1 = Tools.ConvertToPoint(a.RightBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } else if (a.InsideVerticalBorder.Size != null && a.InsideVerticalBorder.Size.HasValue) { size1 = Tools.ConvertToPoint(a.InsideVerticalBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } if (b.LeftBorder.Size != null && b.LeftBorder.Size.HasValue) { size2 = Tools.ConvertToPoint(b.LeftBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } else if (b.InsideVerticalBorder.Size != null && b.InsideVerticalBorder.Size.HasValue) { size2 = Tools.ConvertToPoint(b.InsideVerticalBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } } else if (dir == compareDirection.Vertical) { if (a.BottomBorder.Size != null && a.BottomBorder.Size.HasValue) { size1 = Tools.ConvertToPoint(a.BottomBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } else if (a.InsideHorizontalBorder.Size != null && a.InsideHorizontalBorder.Size.HasValue) { size1 = Tools.ConvertToPoint(a.InsideHorizontalBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } if (b.TopBorder.Size != null && b.TopBorder.Size.HasValue) { size2 = Tools.ConvertToPoint(b.TopBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } else if (b.InsideHorizontalBorder.Size != null && b.InsideHorizontalBorder.Size.HasValue) { size2 = Tools.ConvertToPoint(b.InsideHorizontalBorder.Size.Value, Tools.SizeEnum.LineBorder, -1f); } } if (size1 > size2) { return(true); } else if (size2 > size1) { return(false); } // compare brightness // TODO: current brightness implementation is based on Luminance // but ISO $17.4.66 defines the comparisons should be // 1. R+B+2G, 2. B+2G, 3. G float brightness1 = 0f, brightness2 = 0f; if (dir == compareDirection.Horizontal) { if (a.RightBorder.Color != null && a.RightBorder.Color.HasValue) { brightness1 = Tools.RgbBrightness(a.RightBorder.Color.Value); } else if (a.InsideVerticalBorder.Color != null && a.InsideVerticalBorder.Color.HasValue) { brightness1 = Tools.RgbBrightness(a.InsideVerticalBorder.Color.Value); } if (b.LeftBorder.Color != null && b.LeftBorder.Color.HasValue) { brightness2 = Tools.RgbBrightness(b.LeftBorder.Color.Value); } else if (b.InsideVerticalBorder.Color != null && b.InsideVerticalBorder.Color.HasValue) { brightness2 = Tools.RgbBrightness(b.InsideVerticalBorder.Color.Value); } } else if (dir == compareDirection.Vertical) { if (a.BottomBorder.Color != null && a.BottomBorder.Color.HasValue) { brightness1 = Tools.RgbBrightness(a.BottomBorder.Color.Value); } else if (a.InsideHorizontalBorder.Color != null && a.InsideHorizontalBorder.Color.HasValue) { brightness1 = Tools.RgbBrightness(a.InsideHorizontalBorder.Color.Value); } if (b.TopBorder.Color != null && b.TopBorder.Color.HasValue) { brightness2 = Tools.RgbBrightness(b.TopBorder.Color.Value); } else if (b.InsideHorizontalBorder.Color != null && b.InsideHorizontalBorder.Color.HasValue) { brightness2 = Tools.RgbBrightness(b.InsideHorizontalBorder.Color.Value); } } // smaller brightness wins if (brightness1 < brightness2) { return(true); } else if (brightness2 < brightness1) { return(false); } return(false); // special trick, especially for vertical comparison }