TableProperties Class
Table Properties.When the object is serialized out as xml, its qualified name is w:tblPr.
Inheritance Hierarchy
System.Object
DocumentFormat.OpenXml.OpenXmlElement
DocumentFormat.OpenXml.OpenXmlCompositeElement
DocumentFormat.OpenXml.Wordprocessing.TableProperties
Namespace: DocumentFormat.OpenXml.Wordprocessing
Assembly: DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)
Syntax
'Declaration
<ChildElementInfoAttribute(GetType(TableJustification))> _
<ChildElementInfoAttribute(GetType(TableBorders))> _
<ChildElementInfoAttribute(GetType(TableStyle))> _
<ChildElementInfoAttribute(GetType(TablePositionProperties))> _
<ChildElementInfoAttribute(GetType(TableOverlap))> _
<ChildElementInfoAttribute(GetType(BiDiVisual))> _
<ChildElementInfoAttribute(GetType(TableWidth))> _
<ChildElementInfoAttribute(GetType(TableCellSpacing))> _
<ChildElementInfoAttribute(GetType(TableIndentation))> _
<ChildElementInfoAttribute(GetType(Shading))> _
<ChildElementInfoAttribute(GetType(TableLayout))> _
<ChildElementInfoAttribute(GetType(TableCellMarginDefault))> _
<ChildElementInfoAttribute(GetType(TableLook))> _
<ChildElementInfoAttribute(GetType(TableCaption), FileFormatVersions.Office2010)> _
<ChildElementInfoAttribute(GetType(TableDescription), FileFormatVersions.Office2010)> _
<ChildElementInfoAttribute(GetType(TablePropertiesChange))> _
Public Class TableProperties _
Inherits OpenXmlCompositeElement
'Usage
Dim instance As TableProperties
[ChildElementInfoAttribute(typeof(TableJustification))]
[ChildElementInfoAttribute(typeof(TableBorders))]
[ChildElementInfoAttribute(typeof(TableStyle))]
[ChildElementInfoAttribute(typeof(TablePositionProperties))]
[ChildElementInfoAttribute(typeof(TableOverlap))]
[ChildElementInfoAttribute(typeof(BiDiVisual))]
[ChildElementInfoAttribute(typeof(TableWidth))]
[ChildElementInfoAttribute(typeof(TableCellSpacing))]
[ChildElementInfoAttribute(typeof(TableIndentation))]
[ChildElementInfoAttribute(typeof(Shading))]
[ChildElementInfoAttribute(typeof(TableLayout))]
[ChildElementInfoAttribute(typeof(TableCellMarginDefault))]
[ChildElementInfoAttribute(typeof(TableLook))]
[ChildElementInfoAttribute(typeof(TableCaption), FileFormatVersions.Office2010)]
[ChildElementInfoAttribute(typeof(TableDescription), FileFormatVersions.Office2010)]
[ChildElementInfoAttribute(typeof(TablePropertiesChange))]
public class TableProperties : OpenXmlCompositeElement
Remarks
The following table lists the possible child types:
TableStyle <w:tblStyle>
TablePositionProperties <w:tblpPr>
TableOverlap <w:tblOverlap>
BiDiVisual <w:bidiVisual>
TableWidth <w:tblW>
TableJustification <w:jc>
TableCellSpacing <w:tblCellSpacing>
TableIndentation <w:tblInd>
TableBorders <w:tblBorders>
Shading <w:shd>
TableLayout <w:tblLayout>
TableCellMarginDefault <w:tblCellMar>
TableLook <w:tblLook>
TableCaption <w:tblCaption>
TableDescription <w:tblDescription>
TablePropertiesChange <w:tblPrChange>
[ISO/IEC 29500-1 1st Edition]
17.4.60 tblPr (Table Properties)
This element specifies the set of table-wide properties applied to the current table. These properties affect the appearance of all rows and cells within the parent table, but can be overridden by individual table-level exception, row, and cell level properties as defined by each property.
[Example: Consider the following simple WordprocessingML table:
This table defines a one point single border for all border types and is set to 100% of page width - both table-wide properties. The resulting table is represented by the following WordprocessingML:
<w:tbl>
<w:tblPr>
<w:tblW w:w="0" w:type="auto"/>
<w:tblBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/>
</w:tblBorders>
</w:tblPr>
…
</w:tbl>
In this example, the tblW element (§17.4.64) defines the total width of the table, which, in this case, is set to a type of auto, which specifies that the table should be automatically sized to fit its contents. The tblBorders element (§17.4.39) specifies each of the table's borders, and specifies a one point border on the top, left, bottom, right and inside horizontal and vertical border. end example]
Parent Elements |
---|
tbl (§17.4.38) |
Child Elements |
Subclause |
---|---|
bidiVisual (Visually Right to Left Table) |
§17.4.1 |
jc (Table Alignment) |
§17.4.29 |
shd (Table Shading) |
§17.4.32 |
tblBorders (Table Borders) |
§17.4.39 |
tblCaption (Table Caption) |
§17.4.41 |
tblCellMar (Table Cell Margin Defaults) |
§17.4.43 |
tblCellSpacing (Table Cell Spacing Default) |
§17.4.46 |
tblDescription (Table Description) |
§17.4.47 |
tblInd (Table Indent from Leading Margin) |
§17.4.51 |
tblLayout (Table Layout) |
§17.4.53 |
tblLook (Table Style Conditional Formatting Settings) |
§17.4.56 |
tblOverlap (Floating Table Allows Other Tables to Overlap) |
§17.4.57 |
tblpPr (Floating Table Positioning) |
§17.4.58 |
tblPrChange (Revision Information for Table Properties) |
§17.13.5.34 |
tblStyle (Referenced Table Style) |
§17.4.63 |
tblStyleColBandSize (Number of Columns in Column Band) |
§17.7.6.5 |
tblStyleRowBandSize (Number of Rows in Row Band) |
§17.7.6.7 |
tblW (Preferred Table Width) |
§17.4.64 |
[Note: The W3C XML Schema definition of this element’s content model (CT_TblPr) is located in §A.1. end note]
© ISO/IEC29500: 2008.
Examples
The following code example uses the TableProperties class to create a Table and specify its border information.
using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace TablePropertiesEx
{
class Program
{
// Insert a table into an existing word processing document.
static void Main(string[] args)
{
string fileName = @"C:\users\public\documents\TablePropertiesEx.docx";
using (WordprocessingDocument document
= WordprocessingDocument.Open(fileName, true))
{
// Create an empty table.
Table table = new Table();
// Create a TableProperties object and specify its border information.
TableProperties tableProperties = new TableProperties(
new TableBorders(new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 24 },
new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 24 },
new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 24 },
new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 24 },
new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 24 },
new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Birds), Size = 24 })
);
// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tableProperties);
// Create a row and a cell.
TableRow tableRow = new TableRow();
TableCell tableCell1 = new TableCell();
// Specify the width property of the table cell.
tableCell1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
// Write some text in the cell.
tableCell1.Append(new Paragraph(new Run(new Text("Some cell text."))));
// Append the cell to the row.
tableRow.Append(tableCell1);
// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tableCell2 = new TableCell(tableCell1.OuterXml);
// Append the cell to the row.
tableRow.Append(tableCell2);
// Append the table row to the table.
table.Append(tableRow);
// Append the table to the document.
document.MainDocumentPart.Document.Body.Append(table);
Console.WriteLine("The table has been created.\nPress a key.");
Console.ReadKey();
}
}
}
}
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Module Module1
' Insert a table into an existing word processing document.
Sub Main(ByVal args As String())
Dim fileName As String = "C:\users\public\documents\TablePropertiesEx.docx"
Using document As WordprocessingDocument = WordprocessingDocument.Open(fileName, True)
' Create an empty table.
Dim table As New Table()
' Create a TableProperties object and specify its border information.
Dim tableProperties As New TableProperties(New TableBorders(New TopBorder() With { _
.Val = New EnumValue(Of BorderValues)(BorderValues.Birds), _
.Size = 24 _
}, New BottomBorder() With { _
.Val = New EnumValue(Of BorderValues)(BorderValues.Birds), _
.Size = 24 _
}, New LeftBorder() With { _
.Val = New EnumValue(Of BorderValues)(BorderValues.Birds), _
.Size = 24 _
}, New RightBorder() With { _
.Val = New EnumValue(Of BorderValues)(BorderValues.Birds), _
.Size = 24 _
}, New InsideHorizontalBorder() With { _
.Val = New EnumValue(Of BorderValues)(BorderValues.Birds), _
.Size = 24 _
}, New InsideVerticalBorder() With { _
.Val = New EnumValue(Of BorderValues)(BorderValues.Birds), _
.Size = 24 _
}))
' Append the TableProperties object to the empty table.
table.AppendChild(Of TableProperties)(tableProperties)
' Create a row and a cell.
Dim tableRow As New TableRow()
Dim tableCell1 As New TableCell()
' Specify the width property of the table cell.
tableCell1.Append(New TableCellProperties(New TableCellWidth() With { _
.Type = TableWidthUnitValues.Dxa, _
.Width = "2400" _
}))
' Write some text in the cell.
tableCell1.Append(New Paragraph(New Run(New Text("Some cell text."))))
' Append the cell to the row.
tableRow.Append(tableCell1)
' Create a second table cell by copying the OuterXml value of the first table cell.
Dim tableCell2 As New TableCell(tableCell1.OuterXml)
' Append the cell to the row.
tableRow.Append(tableCell2)
' Append the table row to the table.
table.Append(tableRow)
' Append the table to the document.
document.MainDocumentPart.Document.Body.Append(table)
Console.WriteLine("The table has been created." & vbLf & "Press a key.")
Console.ReadKey()
End Using
End Sub
End Module
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.