Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Apr 26, 2024 5:55 pm

I have a word document which contains a table, and with code I add a table in the last row of it.
I go through the created table to change the width of the cells, but it does not respect the width.
I do it in the following way:

Code: Select all
            Document strDocument = new Document();
            strDocument.LoadFromFile(strFileFinal);

            Table strPrincipalTable = strDocument.Sections[0].Tables[0] as Spire.Doc.Table;

            TableRow strDataRowPrincipal = strPrincipalTable.Rows[2];
            strDataRowPrincipal.Cells[0].Paragraphs.Clear();

 for (int i = 0; i < (table.Rows.Count); i++)
                {
                    table.Rows[i].Cells[0].SetCellWidth(10f, CellWidthType.Auto);
                    table.Rows[i].Cells[1].SetCellWidth(30f, CellWidthType.Auto);
                    table.Rows[i].Cells[2].SetCellWidth(30f, CellWidthType.Auto);
                    table.Rows[i].Cells[3].SetCellWidth(10f, CellWidthType.Auto);
                    table.Rows[i].Cells[4].SetCellWidth(10f, CellWidthType.Auto);
                    table.Rows[i].Cells[5].SetCellWidth(30f, CellWidthType.Auto);
                }


attached url image example of what the table looks like at the bottom
https://postimg.cc/XZgtskKF

thank you for your answer

PolloKulos
 
Posts: 11
Joined: Sun Mar 28, 2021 3:23 am

Sat Apr 27, 2024 1:48 am

I have tried the following line of code but it does not work for me:

Code: Select all
table.AutoFit(AutoFitBehaviorType.AutoFitToContents);


but when I open the word document directly and add the autofit contents property to the table it works

PolloKulos
 
Posts: 11
Joined: Sun Mar 28, 2021 3:23 am

Sun Apr 28, 2024 2:13 am

Hi,

Thanks for your inquiry.
The code you provided isn’t complete, so I created a simple input word file and test demo to test according to your messages.
There are two solutions for you, I put them below and attach my input test word file.
1) Set table AutoFit to AutoFitBehaviorType.AutoFitToWindow
Code: Select all
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);

I put complete code below for this solution:
Code: Select all
// Create a new Document object
            Document strDocument = new Document();
            // Load the document from the specified file path
            strDocument.LoadFromFile(@"../../data/testInsertTable.docx");

            // Get the first table in the first section of the document
            Table strPrincipalTable = strDocument.Sections[0].Tables[0] as Spire.Doc.Table;

            // Get the second row of the principal table
            TableRow strDataRowPrincipal = strPrincipalTable.Rows[1];
            // Clear the paragraphs in the first cell of the second row
            strDataRowPrincipal.Cells[0].Paragraphs.Clear();

            // Create a new table with the same document and set its layout to true
            Table table = new Table(strDocument, true);
            // Reset the cells of the table to have 2 rows and 6 columns
            table.ResetCells(2, 6);
            // Iterate through each row of the table
            for (int i = 0; i < (table.Rows.Count); i++)
            {
                // Set the width of each cell in the row
                table.Rows[i].Cells[0].SetCellWidth(10f, CellWidthType.Auto);
                table.Rows[i].Cells[1].SetCellWidth(30f, CellWidthType.Auto);
                table.Rows[i].Cells[2].SetCellWidth(30f, CellWidthType.Auto);
                table.Rows[i].Cells[3].SetCellWidth(10f, CellWidthType.Auto);
                table.Rows[i].Cells[4].SetCellWidth(10f, CellWidthType.Auto);
                table.Rows[i].Cells[5].SetCellWidth(30f, CellWidthType.Auto);
            }

            // Add the newly created table to the first cell of the second row of the principal table
            strDataRowPrincipal.Cells[0].ChildObjects.Add(table);

            // Auto-fit the table to the window size
            table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
            // Save the modified document to the specified file path
            strDocument.SaveToFile(@"../../output/resultInsertTable_Percentage.docx", FileFormat.Docx);


2) Set tableCell width to CellWidthType.Percentage, I put complete code below for this solution.
Code: Select all
// Create a new Document object
            Document strDocument = new Document();
            // Load the document from the specified file path
            strDocument.LoadFromFile(@"../../data/testInsertTable.docx");

            // Get the first table in the first section of the document
            Table strPrincipalTable = strDocument.Sections[0].Tables[0] as Spire.Doc.Table;

            // Get the second row of the principal table
            TableRow strDataRowPrincipal = strPrincipalTable.Rows[1];
            // Clear the paragraphs in the first cell of the second row
            strDataRowPrincipal.Cells[0].Paragraphs.Clear();

            // Create a new table with the same document and set its layout to true
            Table table = new Table(strDocument, true);
            // Reset the cells of the table to have 2 rows and 6 columns
            table.ResetCells(2, 6);
            // Set the preferred width of the table to be 100% of the available width
            table.PreferredWidth = new PreferredWidth(WidthType.Percentage, 100);

            // Iterate through each row of the table
            for (int i = 0; i < (table.Rows.Count); i++)
            {
                // Set the width of each cell in the row
                table.Rows[i].Cells[0].SetCellWidth(10, CellWidthType.Percentage);
                table.Rows[i].Cells[1].SetCellWidth(10, CellWidthType.Percentage);
                table.Rows[i].Cells[2].SetCellWidth(30, CellWidthType.Percentage);
                table.Rows[i].Cells[3].SetCellWidth(10, CellWidthType.Percentage);
                table.Rows[i].Cells[4].SetCellWidth(10, CellWidthType.Percentage);
                table.Rows[i].Cells[5].SetCellWidth(30, CellWidthType.Percentage);
            }

            // Add the newly created table to the first cell of the second row of the principal table
            strDataRowPrincipal.Cells[0].ChildObjects.Add(table);
            // Save the modified document to the specified file path
            strDocument.SaveToFile(@"../../output/resultInsertTable_Percentage.docx", FileFormat.Docx);


If the two solutions above don’t help you, please offer your complete code that can reproduce your issue and input word file to help us do further investigation. You can attach here or send it via email([email protected]).

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc