We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Using entire width of a word documentent without margins, to create a table

How to use the entire width of a word document without margins, to create a table(which uses the entire space) ?

Not like this:
 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;
                }
            }
I want to use the entire width, like an Excel sheet, which is used in Excel, but using IWTable table .

1 Reply

MN Meikanda Nayanar Syncfusion Team April 16, 2019 11:36 AM UTC

Hi Kevin, 

Thank you for contacting Syncfsuion support.  

To meet your requirement, we suggest you to set 0 margins for the Word document and set width of the table cells to fit in entire page width. Please refer the below code example and let us know if it helps you:  

//Creates a Word document.  
WordDocument document = new WordDocument();  
//Adds one section.  
IWSection section = document.AddSection();  
//Initializes a new instance of the WTable class with the specified WordDocument instance.   
IWTable table = section.AddTable();  
//Resets the table with the specified number of rows and columns.   
table.ResetCells(2, 3);  
  
//Append text in each cells in table.  
table[0, 0].AddParagraph().AppendText("First cell First row");  
table[0, 1].AddParagraph().AppendText("Second cell First row");  
table[0, 2].AddParagraph().AppendText("Third cell First row");  
table[1, 0].AddParagraph().AppendText("First cell Second row");  
table[1, 1].AddParagraph().AppendText("Second cell Second row");  
table[1, 2].AddParagraph().AppendText("Third cell Second row");  
//Sets table border line width.  
table.TableFormat.Borders.LineWidth = 1;  
  
//Reset the margins.  
section.PageSetup.Margins.All = 0;  
//Calculate the page client width and exclude the border line width to preserve inside page.  
float pageClientWidth = section.PageSetup.ClientWidth - (table.TableFormat.Borders.Left.LineWidth + table.TableFormat.Borders.Right.LineWidth);  
//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;  
    }  
}  
  
//Saves and closes the document   
document.Save("Sample.docx");  
document.Close();  


Please let us know whether this suits your requirement.  

Thanks. 
Meikandan 


Loader.
Live Chat Icon For mobile
Up arrow icon