//creates the document
WordDocument document = new WordDocument();
document.EnsureMinimal();
//Adds the table to the document
WTable table = document.LastSection.AddTable() as WTable;
table.ResetCells(4, 4);
//Gets the owner section of table using GetOwnerSection method
WSection section = GetOwnerSection(table);
//Retrieve the page width by using pagesize property
float pagewidth = section.PageSetup.PageSize.Width;
//Gets the owner section of the table:
Private WSection GetOwnerSection(WTable table)
{
//Gets the owner of the table
Entity entity = table.Owner;
while (!(entity is WSection))
{
if (entity.Owner != null)
entity = entity.Owner;
else
break;
}
return entity as WSection
} |
//Retrieve the page client area width by using page setup property
float pageClientWidth = section.PageSetup.ClientWidth;
//Iterating through each cells and setting up width
//in order to fit the table
foreach (WTableRow row in table.Rows)
{
foreach (WTableCell cell in row.Cells)
{
//Updates the cell width based on the page client area width
cell.Width = pageClientWidth / row.Cells.Count;
}
} |