[]
GcWord allows you to add, modify, and delete a table from a Word document. A table can be created in a document using Add or Insert method of the TableCollection class. It also lets you format the table using the TableFormat class properties and apply styles to the table using Style class properties.
To create a table in Word document using GcWord:
Create a new Word document using an instance of the GcWordDocument class.
Get range of the first section in body of the document and add an empty table to the first section using the Add method.
Add rows and cells to the table using the Add method.
Create a new table style using the Style class and apply it on the table.
Save the document using the Save method.
GcWordDocument doc = new GcWordDocument();
//Access the first section of the document
Section section = doc.Body.Sections.First;
//Add a paragraph in the section
Paragraph header = section.GetRange().Paragraphs.Add(" Adding a table:");
// Add an empty table with:
int rows = 6;
int cols = 4;
Table t = section.GetRange().Tables.Add(0, 0);
// Add some rows and cells to it:
List cells = new List(cols);
for (int col = 0; col < cols; ++col)
cells.Add(string.Empty);
for (int row = 0; row < rows; ++row)
{
for (int col = 0; col < cols; ++col)
cells[col] = $"Row {row + 1}, col {col + 1}";
t.Rows.Add(cells.ToArray());
}
// Create a new table style:
Style ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
foreach (var border in ts1.Table.Borders)
{
border.LineStyle = LineStyle.BasicBlackDots;
border.LineWidth = 0.5f;
border.Color.RGB = Color.Purple;
}
//Apply the table style on the table
t.Style = ts1;
//Save the document
doc.Save("CreateTable.docx");
To modify the table formatting in a Word document:
Load the document created in Create Table section, using the Load method.
Access formatting properties of the table through Format property of the Table class.
Modify the table. For example, modify the alignment of the table using the Alignment property and set the AllowAutoFit property to allow the table cells to automatically resize according to their content.
//Load an existing document
doc.Load("CreateTable.docx");
//Access the table
Table t = doc.Body.Sections.First.GetRange().Tables[0];
//Modify the table
t.Format.AllowAutoFit = true;
t.Format.Alignment = TableAlignment.Center;
//Save the document
doc.Save("ModifiedDoc.docx");
To delete a table from a Word document:
Access the table from the table collection using Tables property of the RangeBase class.
Delete the table using the Delete method.
//Load an existing document
doc.Load("CreateTable.docx");
//Access the table
Table t = doc.Body.Sections.First.GetRange().Tables[0];
//Delete the table
t.Delete();
//Save the document
doc.Save("TableDeleted.docx");
To allow automatic resizing of cells in a table according to their content, use AllowAutoFit property of the TableFormatBase class.
//Load an existing document
doc.Load("CreateTable.docx");
//Access the table and allow automatic resizing of the cells
Table t = doc.Body.Sections.First.GetRange().Tables[0];
t.Format.AllowAutoFit = true;
//Save the document
doc.Save("AutoFitEnabled.docx");
To spit a table in a Word document:
Access a table and then access its row. For example, access the second row of the table.
Split the table from the specified row of an existing table.
Insert a paragraph between the two tables. This enables you to view two separate tables.
doc.Load("CreateTable.docx");
//Access the table and it's second row
Table t = doc.Body.Sections.First.GetRange().Tables[0];
Row row = t.Rows[1];
//This generates a new table from the specified row of an existing table.
Table splitTable1 = t.Split(row, InsertLocation.After);
//Insert a paragraph (without content) between the two tables
splitTable1.GetRange().Paragraphs.Insert(InsertLocation.Before);
//Save the document
doc.Save("SplitTable.docx");
To set table styles in a Word document:
Access the table from the table collection using Tables property of the RangeBase class.
Add a new table style to the style collection using Add method of StyleCollection class.
Apply the style on the table using Style property of Table class.
Set formatting properties of the table, such as Description, Alignment, and Title and save the document.
//Load the document
doc.Load("CreateTable.docx");
//Access the table
Table t = doc.Body.Sections.First.GetRange().Tables[0];
// Create a new table style:
Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table);
ts1.Table.ColumnStripe=3;
// Assign the style to the table:
t.Style = ts1;
//Set table formatting
t.Format.Alignment = TableAlignment.Center;
t.Format.Title = "Test Table";
t.Format.Description = "This is the table description";
//Save the document
doc.Save("SetTableInfo.docx");
To get table styles and formatting:
Access the table from the table collection using Tables property of the RangeBase class.
Fetch the existing table styles using the Style property and the table formatting using the Format property.
Display the fetched details on the console.
//Load the document
doc.Load("SetTableInfo.docx");
//Access the table
Table t = doc.Body.Sections.First.GetRange().Tables[0];
//Get table styles
Style st=t.Style;
uint cstripe = st.Table.ColumnStripe;
Console.WriteLine("Table stripe: " + cstripe.ToString());
//Get table formatting
TableFormat tformat = t.Format;
string title = tformat.Title;
//Write the fetched title of the table on the console
Console.WriteLine("Table Title: " + title);
To set the indentation of a table in Word document:
Add a new table style to the style collection using the Add method.
Set the indentation of the table, in points, using the Indent property
Apply the style on the table using the Style property.
//Load the table
doc.Load("CreateTable.docx");
//Access the table and allow automatic resizing of the cells
Table t = doc.Body.Sections.First.GetRange().Tables[0];
t.Format.AllowAutoFit = true;
// Create a new table style to set the Indentation
Style ts1 = doc.Styles.Add("Table Style 2", StyleType.Table);
//Set the indentation
ts1.Table.Indent = 3;
//Apply the style
t.Style = ts1;
//Save the document
doc.Save("SetIndent.docx");
To get the indentation of a table from a Word document:
Access the table from the table collection using Tables property of the RangeBase class.
Fetch indentation of the table using Indent property.
Display the fetched table indentation on the console.
//Load an existing document
doc.Load("SetIndent.docx");
//Access the table
Table t = doc.Body.Sections.First.GetRange().Tables[0];
//Get the style applied on the table
Style s = t.Style;
//Get the indentation
float indenting=s.Table.Indent;
//Write the fetched indentation(in points) on the console
Console.WriteLine("Get Indenting: " + indenting);
For more information about implementation of tables using GcWord, see GcWord sample browser.