Partager via


TableCellProperties Class

Table Cell Properties.When the object is serialized out as xml, its qualified name is w:tcPr.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlCompositeElement
      DocumentFormat.OpenXml.Wordprocessing.TableCellProperties

Namespace:  DocumentFormat.OpenXml.Wordprocessing
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)

Syntax

'Declaration
<ChildElementInfoAttribute(GetType(TableCellWidth))> _
<ChildElementInfoAttribute(GetType(TableCellMargin))> _
<ChildElementInfoAttribute(GetType(TableCellPropertiesChange))> _
<ChildElementInfoAttribute(GetType(GridSpan))> _
<ChildElementInfoAttribute(GetType(HorizontalMerge))> _
<ChildElementInfoAttribute(GetType(VerticalMerge))> _
<ChildElementInfoAttribute(GetType(TableCellBorders))> _
<ChildElementInfoAttribute(GetType(Shading))> _
<ChildElementInfoAttribute(GetType(NoWrap))> _
<ChildElementInfoAttribute(GetType(ConditionalFormatStyle))> _
<ChildElementInfoAttribute(GetType(TextDirection))> _
<ChildElementInfoAttribute(GetType(TableCellFitText))> _
<ChildElementInfoAttribute(GetType(TableCellVerticalAlignment))> _
<ChildElementInfoAttribute(GetType(HideMark))> _
<ChildElementInfoAttribute(GetType(CellInsertion))> _
<ChildElementInfoAttribute(GetType(CellDeletion))> _
<ChildElementInfoAttribute(GetType(CellMerge))> _
Public Class TableCellProperties _
    Inherits OpenXmlCompositeElement
'Usage
Dim instance As TableCellProperties
[ChildElementInfoAttribute(typeof(TableCellWidth))]
[ChildElementInfoAttribute(typeof(TableCellMargin))]
[ChildElementInfoAttribute(typeof(TableCellPropertiesChange))]
[ChildElementInfoAttribute(typeof(GridSpan))]
[ChildElementInfoAttribute(typeof(HorizontalMerge))]
[ChildElementInfoAttribute(typeof(VerticalMerge))]
[ChildElementInfoAttribute(typeof(TableCellBorders))]
[ChildElementInfoAttribute(typeof(Shading))]
[ChildElementInfoAttribute(typeof(NoWrap))]
[ChildElementInfoAttribute(typeof(ConditionalFormatStyle))]
[ChildElementInfoAttribute(typeof(TextDirection))]
[ChildElementInfoAttribute(typeof(TableCellFitText))]
[ChildElementInfoAttribute(typeof(TableCellVerticalAlignment))]
[ChildElementInfoAttribute(typeof(HideMark))]
[ChildElementInfoAttribute(typeof(CellInsertion))]
[ChildElementInfoAttribute(typeof(CellDeletion))]
[ChildElementInfoAttribute(typeof(CellMerge))]
public class TableCellProperties : OpenXmlCompositeElement

Remarks

The following table lists the possible child types:

  • ConditionalFormatStyle <w:cnfStyle>

  • TableCellWidth <w:tcW>

  • GridSpan <w:gridSpan>

  • HorizontalMerge <w:hMerge>

  • VerticalMerge <w:vMerge>

  • TableCellBorders <w:tcBorders>

  • Shading <w:shd>

  • NoWrap <w:noWrap>

  • TableCellMargin <w:tcMar>

  • TextDirection <w:textDirection>

  • TableCellFitText <w:tcFitText>

  • TableCellVerticalAlignment <w:vAlign>

  • HideMark <w:hideMark>

  • CellInsertion <w:cellIns>

  • CellDeletion <w:cellDel>

  • CellMerge <w:cellMerge>

  • TableCellPropertiesChange <w:tcPrChange>

[ISO/IEC 29500-1 1st Edition]

17.4.70 tcPr (Table Cell Properties)

This element specifies the set of properties which shall be applied a specific table cell. Each unique property is specified by a child element of this element. In any instance where there is a conflict between the table level, table-level exception, or row level properties with a corresponding table cell property, these properties shall overwrite the table or row wide properties.

[Example: Consider a table where the cell width overwrites the table width represented in the following WordprocessingML:

<w:tbl>
<w:tblPr>
<w:tblCellMar>
<w:left w:w="0" w:type="dxa"/>
</w:tblCellMar>
</w:tblPr>
…
<w:tr>
<w:tc>
<w:tcPr>
<w:tcMar>
<w:left w:w="720" w:type="dxa"/>
</w:tcMar>
</w:tcPr>
…
</w:tc>
</w:tr>
</w:tbl>

This table cell has a left cell margin of 720 twentieths of a point (one half inch) as specified in the tcMar element, which overwrites the table level setting of 0 left table cell margin. end example]

Parent Elements

tc (§17.4.66)

Child Elements

Subclause

cellDel (Table Cell Deletion)

§17.13.5.1

cellIns (Table Cell Insertion)

§17.13.5.2

cellMerge (Vertically Merged/Split Table Cells)

§17.13.5.3

cnfStyle (Table Cell Conditional Formatting)

§17.4.8

gridSpan (Grid Columns Spanned by Current Table Cell)

§17.4.17

headers (Header Cells Associated With Table Cell)

§17.4.19

hideMark (Ignore End Of Cell Marker In Row Height Calculation)

§17.4.21

hMerge (Horizontally Merged Cell)

§17.4.22

noWrap (Don't Wrap Cell Content)

§17.4.30

shd (Table Cell Shading)

§17.4.33

tcBorders (Table Cell Borders)

§17.4.67

tcFitText (Fit Text Within Cell)

§17.4.68

tcMar (Single Table Cell Margins)

§17.4.69

tcPrChange (Revision Information for Table Cell Properties)

§17.13.5.36

tcW (Preferred Table Cell Width)

§17.4.72

textDirection (Table Cell Text Flow Direction)

§17.4.73

vAlign (Table Cell Vertical Alignment)

§17.4.84

vMerge (Vertically Merged Cell)

§17.4.85

[Note: The W3C XML Schema definition of this element’s content model (CT_TcPr) is located in §A.1. end note]

[ISO/IEC 29500-1 1st Edition]

17.7.4.8 tcPr (Table Style Conditional Formatting Table Cell Properties)

This element specifies the set of table cell properties which shall be applied to all regions within a table which match the conditional formatting type specified on the parent tblStylePr element. These properties are applied in the order specified via the style hierarchy.

[Example: Consider a table style which contains conditional formatting for its firstRow, defined as follows:

<w:style w:type="table" w:styleId="exampleTableStyle">
…
<w:tblStylePr w:type="firstRow">
<w:tcPr>
<w:tcBorders>
<w:top w:val="nil" />
<w:left w:val="nil" />
<w:bottom w:val="nil" />
<w:right w:val="nil" />
<w:insideH w:val="nil" />
<w:insideV w:val="nil" />
</w:tcBorders>
</w:tcPr>
…
</w:tblStylePr>
</w:style>

The tcPr element specified within the tblStylePr element specifies the set of table cell properties which must be applied to all parts of the table which meet the criteria specified by the type value of firstRow - all of the header rows of the table. In this example, the single table cell property applied is a set of table cell borders via the tcBorders element (§17.4.67). In this case, these cell borders simply reset any previous cell borders to nil. end example]

Parent Elements

tblStylePr (§17.7.6.6)

Child Elements

Subclause

cellDel (Table Cell Deletion)

§17.13.5.1

cellIns (Table Cell Insertion)

§17.13.5.2

cellMerge (Vertically Merged/Split Table Cells)

§17.13.5.3

cnfStyle (Table Cell Conditional Formatting)

§17.4.8

gridSpan (Grid Columns Spanned by Current Table Cell)

§17.4.17

headers (Header Cells Associated With Table Cell)

§17.4.19

hideMark (Ignore End Of Cell Marker In Row Height Calculation)

§17.4.21

hMerge (Horizontally Merged Cell)

§17.4.22

noWrap (Don't Wrap Cell Content)

§17.4.30

shd (Table Cell Shading)

§17.4.33

tcBorders (Table Cell Borders)

§17.4.67

tcFitText (Fit Text Within Cell)

§17.4.68

tcMar (Single Table Cell Margins)

§17.4.69

tcPrChange (Revision Information for Table Cell Properties)

§17.13.5.36

tcW (Preferred Table Cell Width)

§17.4.72

textDirection (Table Cell Text Flow Direction)

§17.4.73

vAlign (Table Cell Vertical Alignment)

§17.4.84

vMerge (Vertically Merged Cell)

§17.4.85

[Note: The W3C XML Schema definition of this element’s content model (CT_TcPr) is located in §A.1. end note]

[ISO/IEC 29500-1 1st Edition]

17.7.4.9 tcPr (Style Table Cell Properties)

This element specifies the set of table cell properties which shall be applied to the table. These properties are not conditional and shall always be applied (although they are applied before all conditional formatting properties).

[Example: Consider a table style defined as follows:

<w:style w:type="table" w:styleId="exampleTableStyle">
<w:tcPr>
<w:tcFitText/>
</w:tcPr>
</w:style>

The tcPr element specified within the style element specifies the set of table cell properties which must be applied to all parts of the table. In this example, the single table cell property applied is the fit text setting via the tcFitText element (§17.4.68). end example]

Parent Elements

style (§17.7.4.17)

Child Elements

Subclause

cellDel (Table Cell Deletion)

§17.13.5.1

cellIns (Table Cell Insertion)

§17.13.5.2

cellMerge (Vertically Merged/Split Table Cells)

§17.13.5.3

cnfStyle (Table Cell Conditional Formatting)

§17.4.8

gridSpan (Grid Columns Spanned by Current Table Cell)

§17.4.17

headers (Header Cells Associated With Table Cell)

§17.4.19

hideMark (Ignore End Of Cell Marker In Row Height Calculation)

§17.4.21

hMerge (Horizontally Merged Cell)

§17.4.22

noWrap (Don't Wrap Cell Content)

§17.4.30

shd (Table Cell Shading)

§17.4.33

tcBorders (Table Cell Borders)

§17.4.67

tcFitText (Fit Text Within Cell)

§17.4.68

tcMar (Single Table Cell Margins)

§17.4.69

tcPrChange (Revision Information for Table Cell Properties)

§17.13.5.36

tcW (Preferred Table Cell Width)

§17.4.72

textDirection (Table Cell Text Flow Direction)

§17.4.73

vAlign (Table Cell Vertical Alignment)

§17.4.84

vMerge (Vertically Merged Cell)

§17.4.85

[Note: The W3C XML Schema definition of this element’s content model (CT_TcPr) is located in §A.1. end note]

© ISO/IEC29500: 2008.

Examples

The following example creates a table into a word-processing document, and uses the TableCellProperties class to specify the width of the table cell.

using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace TableCellPropertiesEx
{
    class Program
    {
         // Insert a table into an existing word processing document.
        static void Main(string[] args)
        {
            string fileName = @"C:\users\public\documents\TableCellPropertiesEx.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 tblProp = new TableProperties(
                    new TableBorders(new TopBorder()
                        { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
                        new BottomBorder()
                        { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
                        new LeftBorder()
                        { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
                        new RightBorder()
                        { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
                        new InsideHorizontalBorder()
                        { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 },
                        new InsideVerticalBorder()
                        { Val = new EnumValue<BorderValues>(BorderValues.Sawtooth), Size = 24 } )
                );

                // Append the TableProperties object to the empty table.
                table.AppendChild<TableProperties>(tblProp);

                // 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);
            }
        }
    }
}
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\TableCellPropertiesEx.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 tblProp As New TableProperties(New TableBorders(New TopBorder() With { _
             .Val = New EnumValue(Of BorderValues)(BorderValues.Sawtooth), _
             .Size = 24 _
            }, New BottomBorder() With { _
             .Val = New EnumValue(Of BorderValues)(BorderValues.Sawtooth), _
             .Size = 24 _
            }, New LeftBorder() With { _
             .Val = New EnumValue(Of BorderValues)(BorderValues.Sawtooth), _
             .Size = 24 _
            }, New RightBorder() With { _
             .Val = New EnumValue(Of BorderValues)(BorderValues.Sawtooth), _
             .Size = 24 _
            }, New InsideHorizontalBorder() With { _
             .Val = New EnumValue(Of BorderValues)(BorderValues.Sawtooth), _
             .Size = 24 _
            }, New InsideVerticalBorder() With { _
             .Val = New EnumValue(Of BorderValues)(BorderValues.Sawtooth), _
             .Size = 24 _
            }))

            ' Append the TableProperties object to the empty table.
            table.AppendChild(Of TableProperties)(tblProp)

            ' 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)
        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.

See Also

Reference

TableCellProperties Members

DocumentFormat.OpenXml.Wordprocessing Namespace