.NET Word Examples
- Create Word Document
- Perform Mail Merge
- Manage Bookmarks
- Find and Replace Text
- Format Tables
- Split Word Documents
- Merge Multiple Word Documents
- Fill and Manage Form Fields
- Generate and Update Table of Contents
- Compare Word Documents
- Encrypt and Decrypt Documents with Password
- Convert Word Documents to PDF
- Convert Word Documents to Images
- Word-HTML Conversions
- Word-Markdown Conversions
- Word-Text Conversions
Format a Table in a Word Document Using the Syncfusion .NET Word Library
Tables help arrange the Word document content in rows and columns. A row is a horizontal collection of cells and a column is a vertical collection of cells. Each cell can contain multiple paragraphs and additional tables. The Syncfusion® .NET Word library allows users to create and format tables with just a few lines of code in C# without relying on Microsoft Word or interop dependencies.
Format tables in Word documents using C#
Learn how to format tables in Word documents programmatically using C# with the Syncfusion .NET Word Library. This guide demonstrates applying table formatting properties like borders and alignment to customize table appearance.
Step 1: Create a new project
Start by creating a new C# Console Application project.
Step 2: Install the NuGet package
Add the Syncfusion.DocIO.Net.Core package to your project from NuGet.org.
Step 3: Add required namespaces to format table
Add the following namespaces to your Program.cs file:
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Drawing;
using System.IO;Step 4: Open the Word document and access table
Open an existing Word document and access the first table in the document.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Create an instance of WordDocument class (Empty Word Document)
WordDocument document = new WordDocument();
//Open an existing Word document into DocIO instance
document.Open(fileStreamPath, FormatType.Docx);
//Access the instance of the first section in the Word document
WSection section = document.Sections[0];
//Access the instance of the first table in the section
WTable table = section.Tables[0] as WTable;Step 5: Apply table formatting properties
Apply various formatting properties to customize the table appearance and styling.
// Set the title for the table
table.Title = "PriceDetails";
// Set the description of the table
table.Description = "This table shows the price details of various fruits";
// Set the left indent of the table
table.IndentFromLeft = 50;
// Set the background color of the table
table.TableFormat.BackColor = Color.FromArgb(192, 192, 192);
// Set the horizontal alignment of the table
table.TableFormat.HorizontalAlignment = RowAlignment.Left;
// Set the left, right, top and bottom padding of all the cells in the table
table.TableFormat.Paddings.All = 10;
// Set the auto resize of table to automatically resize all cell width based on its content
table.TableFormat.IsAutoResized = true;
// Set the table top, bottom, left and right border line width
table.TableFormat.Borders.LineWidth = 2f;
// Set the table horizontal border line width
table.TableFormat.Borders.Horizontal.LineWidth = 2f;
// Set the table vertical border line width
table.TableFormat.Borders.Vertical.LineWidth = 2f;
// Set the tables top, bottom, left and right border color
table.TableFormat.Borders.Color = Color.Red;
// Set the table Horizontal border color
table.TableFormat.Borders.Horizontal.Color = Color.Red;
// Set the table vertical border color
table.TableFormat.Borders.Vertical.Color = Color.Red;
// Set the table borders border type
table.TableFormat.Borders.BorderType = BorderStyle.Double;Step 6: Access and format the first row
Access the first row of the table and apply height formatting properties.
//Access the instance of the first row in the table
WTableRow row = table.Rows[0];
// Set the row height
row.Height = 20;
// Set the row height type
row.HeightType = TableRowHeightType.AtLeast;Step 7: Save the Word document
Save the formatted Word document to a file stream.
//Create file stream
FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite);
//Save the Word document to file stream
document.Save(outputFileStream, FormatType.Docx);
outputFileStream.Close();
document.Close();
fileStreamPath.Close();GitHub project
NuGet installation
Get started quickly by downloading the installer and checking license information on the Downloads page.
Table of contents
Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.
Learning
Technical Support