Table width issue - document doesn't respect width set in code

I am using Aspose.Words for .Net.

I have the following issue. My code creates a table using the DocumentBuilder class. The table PreferredWidth is set:

table.PreferredWidth = PreferredWidth.FromPercent(80);

I tried all 100 as well. The problem that I have is that when a specific row is added to the table, the table width is not respected anymore and the table overflows to the right.

Here are some screenshots:

This screenshot shows you the issue:

This the same document excluding the last row :

I tried setting the widths of each column using .Width and also using .PreferredWidth. It doesn’t make any difference. The document is in landscape mode. All the cells in the document have WrapText = true.

What am I missing?

Thanks

@bogc

Cause

The issue you’re experiencing with the table width not being respected in your Aspose.Words document is likely due to the content of the specific row you’re adding. If the content exceeds the available width, it can cause the table to overflow, even if you’ve set the PreferredWidth.

Solution

To address this, you can try the following steps:

  1. Set Column Widths: Ensure that each column’s width is explicitly set. This can help maintain the overall table width. You can set the width for each column like this:

    foreach (Cell cell in table.FirstRow.Cells)
    {
        cell.CellFormat.Width = desiredWidth; // Set your desired width here
    }
    
  2. Check Content: Review the content of the row that causes the overflow. If it contains long text or images, consider using text wrapping or reducing the content size.

  3. Adjust Table Alignment: Ensure the table is properly aligned within the page margins. You can set the table alignment to center or left to see if that helps:

    table.Alignment = TableAlignment.Center; // or TableAlignment.Left
    
  4. Use AutoFit: If you want the table to adjust based on the content, you can use the AutoFit feature:

    table.AutoFit(AutoFitBehavior.AutoFitToWindow);
    

Code

Here’s a minimal example that incorporates these suggestions:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();

// Set preferred width
table.PreferredWidth = PreferredWidth.FromPercent(80);

// Add columns with specific widths
for (int i = 0; i < numberOfColumns; i++)
{
    builder.InsertCell();
    builder.CellFormat.Width = desiredWidth; // Set your desired width
    builder.Write("Column " + (i + 1));
}

builder.EndRow();

// Add the problematic row
builder.InsertCell();
builder.CellFormat.Width = desiredWidth; // Ensure width is set
builder.Write("This is a long text that might cause overflow if not handled properly.");
builder.EndRow();

builder.EndTable();
doc.Save("output.docx");

Next Steps

Implement the above suggestions and check if they resolve the overflow issue. If the problem persists, please share the specific content of the row causing the issue for further analysis. I look forward to your feedback!

None of these suggestions work. It seems that the Width of a cell or column is not enforced. Other ideas?

Setting a padding of 4 for all the cells seem to do the trick:
builder.CellFormat.TopPadding = builder.CellFormat.BottomPadding = builder.CellFormat.RightPadding = builder.CellFormat.LeftPadding = 4;

However, I don’t know why. Aspose support, thoughts?

I found the solution:

table.AllowAutoFit = false;

This setting corresponds to the Word Table Properties → Options → Automatically resize to fit contents checkbox. I set it to false and now the sizes that I was setting work as expected.

@bogc It is perfect that you managed to find the solution.