Adding more elements to a Document – Part III

In this third tutorial of this series, we will look at how to add Tables to your documents.

Learn more about Xceed Words for .NET

This week, we will look at how to add Tables to your documents.

Tables

A Table is simply a grid view, with rows and columns, to allow you to display information in a clear and organized way.

Adding Tables

Adding a Table to a document is done by calling AddTable on the Document. There are 2 options:

By providing the number or rows and columns:

// Create a new table (initial size of 3 rows and 2 columns).
var table = document.AddTable( 3, 2 );
table.Design = TableDesign.TableGrid;

// Insert the new table to the document
document.InsertTable( table );

By providing an existing table:

// Retrieve an existing table (ex: from another document)
var table = document2.Tables[ tableIndex ];

// Insert the table to the document
document.InsertTable( table );

Note: The Design property on Table is used to indicate which design to apply to the table. The possible values are found on the TableDesign enumeration. There are 50 something different values, their names and a small thumbnail for reference are available in the la documentation.

Removing Tables

Removing an existing Table from a document is done by calling Retirer on the Table.

// Remove an existing table from the document
var table = document.Tables[ tableIndex ];
table.Remove;

Adding Rows

Inserting rows is done by calling InsertRow on the Table.

// Insert a new Row at the end of a table
var table = document.Tables[ tableIndex ];
table.InsertRow();

Note: The InsertRow method supports 3 other overloads:

  • (Int32): Inserts a Row at a specific location in this Table.
  • (Row, Boolean): Inserts a copy of the provided Row at the end of this Table, and optionally keep the same formatting as the original Row.
  • (Row, Int32, Boolean): Inserts a copy of the provided Row at a specific location in this Table, and optionally keep the same formatting as the original Row.

Removing Rows

Removing rows is done by calling RemoveRow on the Table. Calling the method with no parameter will remove the last row from the table, otherwise an index can be passed to indicate which row to remove.

// Remove the last row from the table
var table = document.Tables[ tableIndex ];
table.RemoveRow();

// Remove a specific row from the table
table.RemoveRow( rowIndex );

Adding Columns

Inserting columns is done by calling InsertColumn on the Table.

// Insert a new Column at the end of a table
var table = document.Tables[ tableIndex ];
table.InsertColumn();

Note: The InsertColumn method supports 1 other overload:

  • (Int32, Boolean): Inserts a Column at a specific location in this Table.

Removing Columns

Removing columns is done by calling RemoveColumn on the Table. Calling the method with no parameter will remove the last column from the table, otherwise an index can be passed to indicate which column to remove.

// Remove the last column from the table
var table = document.Tables[ tableIndex ];
table.RemoveColumn();

// Remove a specific column from the table
table.RemoveColumn( colIndex );

Merging Cells

The Table supports the ability to merge multiple cells together. There are 2 scenarios currently supported:

Merging multiple cells in the same row by calling MergeCells on the Row:

// Merge the first 3 cells of the first row
var table = document.Tables[ tableIndex ];
table.Rows[ 0 ].MergeCells( 0, 3 );

// Merge the last 2 cells in the third row
var columnCount = table.Rows [ 1 ].ColumnCount;
table.Rows[ 2 ].MergeCells( columnCount - 2, columnCount - 1 );

Merging cells in a specific column by calling MergeCellsInColumn on the Table:

// Merge the first cell of rows 2 and 3
var table = document.Tables[ tableIndex ];
table.MergedCellsInColumn( 0, 1, 2 );

Inserting Other Elements

There are several other methods available on Table to insert specific elements before/after the Table. For example, by using InsertListBeforerSelf or InsertListAfterSelf to insert a list. There are similar methods to insert a Page, Paragraph, or another Table.

Example

The following example demonstrates how to create a table in a Document, insert rows in it, and fill the table with data.

// Create a document.
using( var document = DocX.Create( "InsertRowAndDataTable.docx" ) )
{
	// Add a Table of 5 rows and 2 columns into the document and sets its values.
	var t = document.AddTable( 5, 2 );
	t.Design = TableDesign.ColorfulListAccent1;
	t.Alignment = Alignment.center;
	t.Rows[ 0 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Mike" );
	t.Rows[ 0 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "65" );
	t.Rows[ 1 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Kevin" );
	t.Rows[ 1 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "62" );
	t.Rows[ 2 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Carl" );
	t.Rows[ 2 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "60" );
	t.Rows[ 3 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Michael" );
	t.Rows[ 3 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "59" );
	t.Rows[ 4 ].Cells[ 0 ].Paragraphs[ 0 ].Append( "Shawn" );
	t.Rows[ 4 ].Cells[ 1 ].Paragraphs[ 0 ].Append( "57" );

	// Add a row at the end of the table and sets its values.
	var r = t.InsertRow();
	r.Cells[ 0 ].Paragraphs[ 0 ].Append( "Mario" );
	r.Cells[ 1 ].Paragraphs[ 0 ].Append( "54" );

	// Insert a new Paragraph into the document.
	var p = document.InsertParagraph( "Xceed Top Players Points:" );
	p.SpacingAfter( 40d );

	// Insert the Table after the Paragraph.
	p.InsertTableAfterSelf( t );

	// Save the document
	document.Save();
}

For more information, please refer to the la documentation.