Click or drag to resize

TableFormat Class

Represents a set of table-wide properties. These properties affect the appearance of all rows and cells within the parent table, but may be overridden by individual row and cell level formatting.
Inheritance Hierarchy
SystemObject
  SautinSoft.DocumentFormat
    SautinSoft.Document.TablesTableFormat

Namespace: SautinSoft.Document.Tables
Assembly: SautinSoft.Document (in SautinSoft.Document.dll) Version: 2024.3.11
Syntax
public sealed class TableFormat : Format

The TableFormat type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleTableFormat Initializes a new instance of TableFormat class.
Top
Properties
 NameDescription
Public propertyCode exampleAlignment Gets or sets the alignment of the table with respect to the text margins in the section.
Public propertyAutomaticallyResizeToFitContents Gets or sets a value indicating whether the table shall automatically resize to fit contents.
Public propertyBackgroundColor Gets or sets the table background color.
Public propertyCode exampleBorders Gets the table borders.
Public propertyColumnBandSize Gets or sets the number of columns in column band for this table style.
Public propertyDefaultCellPadding Gets or sets the default table cell padding.
Public propertyDefaultCellSpacing Gets or sets the default table cell spacing (the spacing between adjacent cells and the edges of the table) for all cells in the parent table.
Public propertyIndentFromLeft Gets or sets the value that represents the left indent of the table.
Public propertyCode examplePreferredWidth Gets or sets the table preferred width.
Public propertyRowBandSize Gets or sets the number of rows in row band for this table style.
Public propertyStyle Gets or sets the table style.
Public propertyStyleOptions Gets or sets the table style options that specify which of the referenced TableStyle conditional formats will be applied on the parent table.
Top
Methods
 NameDescription
Public methodClearFormatting Clears the formatting.
(Overrides FormatClearFormatting)
Public methodClone Clones this TableFormat instance.
Public methodEquals Determines whether the specified object is equal to this TableFormat instance.
(Overrides ObjectEquals(Object))
Top
Example

See Developer Guide: How to create a table and apply formatting

How to create a table and apply formatting in C#
using System;
using SautinSoft.Document;
using SautinSoft.Document.Tables;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            TableFormat();
        }

        /// <summary>
        /// How to create a table and apply formatting in a document. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/table-format.php
        /// </remarks>        
        public static void TableFormat()
        {
            string documentPath = @"TableFormat.docx";

            // Let's create a new document.
            DocumentCore dc = new DocumentCore();

            // Add a new section.
            Section s = new Section(dc);
            dc.Sections.Add(s);

            Table table = new Table(dc);
            table.TableFormat.AutomaticallyResizeToFitContents = false;
            table.TableFormat.Alignment = HorizontalAlignment.Center;
            table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.DarkBlue, 2.0);

            table.Columns.Add(new TableColumn() { PreferredWidth = 50 });
            table.Columns.Add(new TableColumn() { PreferredWidth = 80 });
            table.Columns.Add(new TableColumn() { PreferredWidth = 100 });
            table.Columns.Add(new TableColumn() { PreferredWidth = 120 });
            table.Columns.Add(new TableColumn() { PreferredWidth = 80 });

            TableRow row = new TableRow(dc);
            row.RowFormat.Height = new TableRowHeight(100, HeightRule.AtLeast);
            table.Rows.Add(row);

            TableCell cell1 = new TableCell(dc, new Paragraph(dc, "PDF is Portable Document Format"));
            cell1.CellFormat.TextDirection = TextDirection.TopToBottom;
            cell1.CellFormat.BackgroundColor = Color.Yellow;
            cell1.CellFormat.Padding = new Padding(5);
            cell1.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.Yellow, 1.0);
            row.Cells.Add(cell1);

            TableCell cell2 = new TableCell(dc, new Paragraph(dc, "DOCX is Office Open XML"));
            cell2.CellFormat.VerticalAlignment = VerticalAlignment.Center;
            cell2.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.Black, 1.0);
            row.Cells.Add(cell2);

            row.Cells.Add(new TableCell(dc, new Paragraph(dc, "HTML is Hypertext Markup Language"))
            {
                CellFormat = new TableCellFormat()
                {
                    BackgroundColor = Color.Pink                   
                }
            });

            row.Cells.Add(new TableCell(dc, new Paragraph(dc, "Images: jpeg, png, bmp, tiff")
            {
                ParagraphFormat = new ParagraphFormat()
                {
                    Alignment = HorizontalAlignment.Center
                }
            })
            {
                CellFormat = new TableCellFormat()
                {
                    VerticalAlignment = VerticalAlignment.Center,
                    BackgroundColor = Color.Purple
                }
            });

            TableCell cell5 = new TableCell(dc, new Paragraph(dc, "RTF is Rich Text Format"));
            cell5.CellFormat.TextDirection = TextDirection.BottomToTop;
            cell5.CellFormat.Padding = new Padding(5);
            cell5.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dashed, Color.Red, 1.0);
            row.Cells.Add(cell5);

            // Add the table into the section.
            s.Blocks.Add(table);

            // Save our document DOCX.
            dc.Save(documentPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
        }
    }
}
How to create a table and apply formatting in VB.Net
Imports System
Imports SautinSoft.Document
Imports SautinSoft.Document.Tables

Module Sample
    Sub Main()
        TableFormat()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How to create a table and apply formatting in a document. 
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/table-format.php
    ''' </remarks>
    Sub TableFormat()
        Dim documentPath As String = "TableFormat.docx"

        ' Let's create a new document.
        Dim dc As New DocumentCore()

        ' Add a new section.
        Dim s As New Section(dc)
        dc.Sections.Add(s)


        Dim table As New Table(dc)
        table.TableFormat.AutomaticallyResizeToFitContents = False
        table.TableFormat.Alignment = HorizontalAlignment.Center
        table.TableFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.DarkBlue, 2.0)

        table.Columns.Add(New TableColumn() With {.PreferredWidth = 50})
        table.Columns.Add(New TableColumn() With {.PreferredWidth = 80})
        table.Columns.Add(New TableColumn() With {.PreferredWidth = 100})
        table.Columns.Add(New TableColumn() With {.PreferredWidth = 120})
        table.Columns.Add(New TableColumn() With {.PreferredWidth = 80})

        Dim row As New TableRow(dc)
        row.RowFormat.Height = New TableRowHeight(100, HeightRule.AtLeast)
        table.Rows.Add(row)

        Dim cell1 As New TableCell(dc, New Paragraph(dc, "PDF is Portable Document Format"))
        cell1.CellFormat.TextDirection = TextDirection.TopToBottom
        cell1.CellFormat.BackgroundColor = Color.Yellow
        cell1.CellFormat.Padding = New Padding(5)
        cell1.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.Yellow, 1.0)
        row.Cells.Add(cell1)

        Dim cell2 As New TableCell(dc, New Paragraph(dc, "DOCX is Office Open XML"))
        cell2.CellFormat.VerticalAlignment = VerticalAlignment.Center
        cell2.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.Black, 1.0)
        row.Cells.Add(cell2)

        row.Cells.Add(New TableCell(dc, New Paragraph(dc, "HTML is Hypertext Markup Language")) With {
            .CellFormat = New TableCellFormat() With {.BackgroundColor = Color.Pink}
        })

        row.Cells.Add(New TableCell(dc, New Paragraph(dc, "Images: jpeg, png, bmp, tiff") With
        {
            .ParagraphFormat = New ParagraphFormat() With
            {.Alignment = HorizontalAlignment.Center}
        }) With {
            .CellFormat = New TableCellFormat() With {
                .VerticalAlignment = VerticalAlignment.Center,
                .BackgroundColor = Color.Purple
            }
        })

        Dim cell5 As New TableCell(dc, New Paragraph(dc, "RTF is Rich Text Format"))
        cell5.CellFormat.TextDirection = TextDirection.BottomToTop
        cell5.CellFormat.Padding = New Padding(5)
        cell5.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dashed, Color.Red, 1.0)
        row.Cells.Add(cell5)

        ' Add the table into the section.
        s.Blocks.Add(table)

        ' Save our document into DOCX format.
        dc.Save(documentPath)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(documentPath) With {.UseShellExecute = True})
    End Sub

End Module
See Also