13th October 2014

Open XML Usage – Part Two

Open XML Usage – Part Two

Part Two

Add styles and tables.

I found it much easier to create re-usable code when creating Word documents.

The CreateAndAddCharacterStyle method takes a number of parameters i.e. font, font size, colour, bold setting and italics setting.

Once the style is created it can then be applied to a run when creating text.

The CreateTable method creates a table with a header and data rows.

The table borders can be coloured and styled.

The table cells can have colours, margins, justification and alignment applied.

Please refer to Part One of this series to see the Word document output.

public static void CreateAndAddCharacterStyle(StyleDefinitionsPart styleDefinitionsPart, string styleid, string stylename, string aliases, string setFont, string setFontSize, string colourValue, bool isBold, bool isItalic)

//// Get access to the root element of the styles part. Styles styles = styleDefinitionsPart.Styles; //// Create a new character style and specify some of the attributes. Style style = new Style()

Type = StyleValues.Character, StyleId = styleid, CustomStyle = true

; //// Create and add the child elements (properties of the style). Aliases aliases1 = new Aliases()

Val = aliases

; StyleName styleName1 = new StyleName()

Val = stylename

; if (aliases != string.Empty)

style.Append(aliases1);

style.Append(styleName1); //// Create the StyleRunProperties object and specify some of the run properties. StyleRunProperties styleRunProperties1 = new StyleRunProperties(); if (colourValue == string.Empty)

Color color1 = new Color()

ThemeColor = ThemeColorValues.None

; styleRunProperties1.Append(color1);

else

Color color1 = new Color()

Val = colourValue

; styleRunProperties1.Append(color1);

RunFonts font1 = new RunFonts()

Ascii = setFont

; styleRunProperties1.Append(font1); //// Font size value is double the actual size of the font FontSize fontSize1 = new FontSize()

Val = setFontSize

; styleRunProperties1.Append(fontSize1); if (isBold)

Bold bold1 = new Bold(); styleRunProperties1.Append(bold1);

if (isItalic)

Italic italic1 = new Italic(); styleRunProperties1.Append(italic1);

//// Add the run properties to the style. style.Append(styleRunProperties1); //// Add the style to the styles part. styles.Append(style);

private static void CreateTable(Body body)

string cellHeaderColour = "#E4E2F2"; Paragraph p = new Paragraph(); Table tbl = new Table(); //// Create the table properties TableProperties tblProperties = new TableProperties(); //// Create Table Borders TableBorders tblBorders = WordDocument.CreateTableBorders(true, true, true, true, true, true, "#808080", BorderValues.Thick); //// Add the table borders to the properties tblProperties.AppendChild(tblBorders); //// Add the table properties to the table tbl.AppendChild(tblProperties); //// Header Row TableRow trHeader = new TableRow(); TableCell cellColumnHeader1 = WordDocument.CreateTableCell("TableCellHeaderStyle", "Column Heading 1", "1400", cellHeaderColour, JustificationValues.Left, TableVerticalAlignmentValues.Top); TableCell cellColumnHeader2 = WordDocument.CreateTableCell("TableCellHeaderStyle", "Column Heading 2", "6000", cellHeaderColour, JustificationValues.Left, TableVerticalAlignmentValues.Top); TableCell cellColumnHeader3 = WordDocument.CreateTableCell("TableCellHeaderStyle", "Column Heading 3", "1400", cellHeaderColour, JustificationValues.Center, TableVerticalAlignmentValues.Top); TableCell cellColumnHeader4 = WordDocument.CreateTableCell("TableCellHeaderStyle", "Column Heading 4", "1400", cellHeaderColour, JustificationValues.Center, TableVerticalAlignmentValues.Top); //// Add the cells to the row trHeader.Append(cellColumnHeader1, cellColumnHeader2, cellColumnHeader3, cellColumnHeader4); //// Add the rows to the table tbl.Append(trHeader); TableRow trData = new TableRow(); //// Add a data row TableCell cellDataColumn1 = WordDocument.CreateTableCell("TableCellStyle", "Column Data 1", "1400", string.Empty, JustificationValues.Left, TableVerticalAlignmentValues.Top); TableCell cellDataColumn2 = WordDocument.CreateTableCell("TableCellStyle", "Column Data 2", "6000", string.Empty, JustificationValues.Left, TableVerticalAlignmentValues.Top); TableCell cellDataColumn3 = WordDocument.CreateTableCell("TableCellStyle", "Column Data 3", "1400", string.Empty, JustificationValues.Center, TableVerticalAlignmentValues.Top); TableCell cellDataColumn4 = WordDocument.CreateTableCell("TableCellStyle", "Column Data 4", "1400", string.Empty, JustificationValues.Center, TableVerticalAlignmentValues.Top); //// Add the cells to the row trData.Append(cellDataColumn1, cellDataColumn2, cellDataColumn3, cellDataColumn4); //// Add the rows to the table tbl.Append(trData); //// Add the table to the body body.Append(tbl);

public static TableBorders CreateTableBorders(bool addTop, bool addBottom, bool addRight, bool addLeft, bool addInsideHorizontal, bool addInsideVertical, string colour, BorderValues borderValue)

TableBorders tblBorders = new TableBorders(); if (addTop)

TopBorder topBorder = new TopBorder(); topBorder.Val = new EnumValue<BorderValues>(borderValue); topBorder.Color = colour; tblBorders.AppendChild(topBorder);

if (addBottom)

BottomBorder bottomBorder = new BottomBorder(); bottomBorder.Val = new EnumValue<BorderValues>(borderValue); bottomBorder.Color = colour; tblBorders.AppendChild(bottomBorder);

if (addRight)

RightBorder rightBorder = new RightBorder(); rightBorder.Val = new EnumValue<BorderValues>(borderValue); rightBorder.Color = colour; tblBorders.AppendChild(rightBorder);

if (addLeft)

LeftBorder leftBorder = new LeftBorder(); leftBorder.Val = new EnumValue<BorderValues>(borderValue); leftBorder.Color = colour; tblBorders.AppendChild(leftBorder);

if (addInsideHorizontal)

InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder(); insideHBorder.Val = new EnumValue<BorderValues>(borderValue); insideHBorder.Color = colour; tblBorders.AppendChild(insideHBorder);

if (addInsideVertical)

InsideVerticalBorder insideVBorder = new InsideVerticalBorder(); insideVBorder.Val = new EnumValue<BorderValues>(borderValue); insideVBorder.Color = colour; tblBorders.AppendChild(insideVBorder);

return tblBorders;

public static TableCell CreateTableCellFromParagraph(Paragraph p, string cellWidth, string colourValue, JustificationValues justifyCell, TableVerticalAlignmentValues cellVerticalAlign)

ParagraphProperties paragraphProperties = new ParagraphProperties(); Justification objJustification = new Justification()

Val = justifyCell

; paragraphProperties.Append(objJustification); p.Append(paragraphProperties); TableCell tableCell = new TableCell(p); tableCell.Append(new TableCellProperties(new TableCellWidth()

Type = TableWidthUnitValues.Dxa, Width = cellWidth

)); tableCell.Append(new TableCellProperties(CreateTableCellMargin("100", "100", "100", "100"))); tableCell.Append(new TableCellProperties(new TableCellVerticalAlignment()

Val = cellVerticalAlign

)); if (colourValue != string.Empty)

tableCell.Append(new TableCellProperties(new Shading()

Val = ShadingPatternValues.Clear, Color = "auto", Fill = colourValue

));

return tableCell;

public static TableCellMargin CreateTableCellMargin(string topWidth, string leftWidth, string bottomWidth, string rightWidth)

TableCellMargin tblCellMargin = new TableCellMargin( new DocumentFormat.OpenXml.Wordprocessing.TopMargin

Width = topWidth, Type = DocumentFormat.OpenXml.Wordprocessing.TableWidthUnitValues.Dxa

, new DocumentFormat.OpenXml.Wordprocessing.LeftMargin

Width = leftWidth, Type = DocumentFormat.OpenXml.Wordprocessing.TableWidthUnitValues.Dxa

, new DocumentFormat.OpenXml.Wordprocessing.BottomMargin

Width = bottomWidth, Type = DocumentFormat.OpenXml.Wordprocessing.TableWidthUnitValues.Dxa

, new DocumentFormat.OpenXml.Wordprocessing.RightMargin

Width = rightWidth, Type = DocumentFormat.OpenXml.Wordprocessing.TableWidthUnitValues.Dxa

); return tblCellMargin;

public static TableCell CreateTableCell(string styleValue, string cellText, string cellWidth, string colourValue, JustificationValues justifyCell, TableVerticalAlignmentValues cellVerticalAlign)

ParagraphProperties paragraphProperties = new ParagraphProperties(); Justification objJustification = new Justification()

Val = justifyCell

; paragraphProperties.Append(objJustification); Run runTableCell = new Run(); runTableCell.RunProperties = new RunProperties()

RunStyle = new RunStyle()

Val = styleValue

; runTableCell.Append(new Text(cellText)); TableCell tableCell = new TableCell(new Paragraph(runTableCell)

ParagraphProperties = paragraphProperties

); tableCell.Append(new TableCellProperties(new TableCellWidth()

Type = TableWidthUnitValues.Dxa, Width = cellWidth

)); tableCell.Append(new TableCellProperties(CreateTableCellMargin("100", "100", "100", "100"))); tableCell.Append(new TableCellProperties(new TableCellVerticalAlignment()

Val = cellVerticalAlign

)); if (colourValue != string.Empty)

tableCell.Append(new TableCellProperties(new Shading()

Val = ShadingPatternValues.Clear, Color = "auto", Fill = colourValue

));

return tableCell;

Fill in this quick form and discover your digital future
Choose your interests:

Where to find us

We'd love to welcome you into our office! We're only 20 miles north of Peterborough, conveniently just off the A16.

Carver House
Apex Court, Elsoms Way
Pinchbeck
Lincolnshire
PE11 3UL