This article describes how to autofit table in Word using C#. It has IDE settings, a list of steps, and a sample code to adjust table in Word using C#. It provides details to fit the table in different ways as per the requirements.
Steps to Autofit a Table in Word using C#
- Set the environment to use Aspose.Words for .NET for auto-fitting the tables
- Load the input Word file into the Document class with tables in it
- Get the desired child node from the loaded document
- Transform the node to a Table object
- Call the AutoFit() method with the required auto-fit behavior
- Save the output Word file after auto-fitting the table
These steps cover the details of how to make table fit in Word using C#. Load the Word document, select the target table by providing the index, and type-cast it to a Table object. Finally, call the AutoFit() method with the desired value from the AutoFitBehavior enumerator.
Code to Fit Table to Page in Word using C#
using Aspose.Words; | |
using Aspose.Words.Tables; | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
Document doc = new Document("Table.docx"); | |
Table table = (Table)doc.GetChild(NodeType.Table, 0, true); | |
table.AutoFit(AutoFitBehavior.AutoFitToWindow); | |
doc.Save("AdjustedTable.docx"); |
This code has demonstrated how to autofit a table in Word using C#. In the default setting, the table width is set according to the width of the page. Other options can be used to fit the table columns according to the width of the content or keep the column width fixed by increasing the row width.
This article has taught us the process of adjusting a table. To create a new table in a Word file, refer to the article on How to create table in Word document using C#.