We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Call to UpdatePageLayout corrupts table layout

Hello Aspose,

See the following code, the first row of the table isn’t drawn correctly (the cell isn’t merged correctly) if UpdatePageLayout is called.

Regards, Paul

DocumentBuilder db = new DocumentBuilder();
db.StartTable();
// First Row (Two cells)
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10.0);
db.InsertCell();
db.CellFormat.HorizontalMerge = CellMerge.First;
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90.0);
db.InsertCell();
db.CellFormat.HorizontalMerge = CellMerge.Previous;
db.EndRow();
// Second Row (Three Cells);
db.CellFormat.ClearFormatting();
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10.0);
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(45.0);
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(45.0);
db.EndRow();
db.EndTable();
db.Document.UpdatePageLayout(); // Comment this out and table is OK
db.Document.Save(@"c:\temp\merged.docx", SaveFormat.Docx);

Hi Paul,

Thank you for inquiry. Please follow up the code snippet:

DocumentBuilder db = new DocumentBuilder();
db.StartTable();
// First Row (Two cells)
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10.0);
db.InsertCell();
db.CellFormat.HorizontalMerge = CellMerge.First;
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90.0);
db.InsertCell();
db.CellFormat.HorizontalMerge = CellMerge.Previous;
db.EndRow();
// Second Row (Three Cells);
db.CellFormat.ClearFormatting();
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10.0);
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(45.0);
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(45.0);
db.EndRow();
db.EndTable();
db.Document.UpdateTableLayout();
db.Document.Save(@"temp359337\test.docx", SaveFormat.Docx);

In case of any ambiguity, please let me know.

Hi
Thank you for additional information. It is not recommended to call UpdateTableLayout when you save the document in Word formats. Sometimes it is necessary when you save the document in PDF. But normally this method should not be called:
https://reference.aspose.com/words/net/aspose.words/document/
So, just remove this line of code.
Best regards,

Hello Imran,

I’m not calling UpdateTableLayout() I’m calling UpdatePageLayout().

The following code produces the correct layout:

db.Document.UpdateTableLayout();
db.Document.UpdatePageLayout();

The following code alone produces a corrupted table:

db.Document.UpdatePageLayout();

In my actual production code I call UpdatePageLayout() because there is a page number field that needs updating.

Why do I have to call UpdateTableLayout() for such a simple table, the documentation states that it’s a method to be used before exporting to fixed-page format and only in rare cases.

Regards, Paul.

Hi Paul,

Thank you for inquiry.

db.Document.UpdateTableLayout();

It updates widths of cells and tables in the document according to their preferred widths and content. You do not need to call this method if the tables appear correct in the output document.

Hello Imran,

“You do not need to call this method if the tables appear correct in the output document.”

That’s the point I’m trying to make. When I call UpdatePageLayout() it corrupts the table. Why does calling UpdatePageLayout() corrupt such a simple table ?

Regards, Paul

Hi Paul,

Thank you for inquiry. It would be great, if you share example input / output documents to reproduce this problem on my end.

Note:
Moreover, please note that DocumentExplorer is a very useful tool which easily enables us to see the entire document structure. You can find DocumentExplorer in the folder where you installed Aspose.Words e.g. C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Demos\CSharp\DocumentExplorer\bin\DocumentExplorer.exe. Below is the DOM structure of your document as viewed with DocumentExplorer:

Hello Imran,

See attached program.cs.txt and merged.docx.

Regards, Paul.

Hi Paul,

Thank you for inquiry.

>>In my actual production code I call UpdatePageLayout() because there is a page number field that needs >>updating.

>>Why do I have to call UpdateTableLayout() for such a simple table, the documentation states that it’s a method to be >>used before exporting to fixed-page format and only in rare cases.

Ms Word automatically updates these fields in headers/footers. We do not need to use UpdatePageLayout here in this code.

I have attached input / output Word documents. Example code:

Document document = new Document("c:\\temp\\TemplateInput.docx");
DocumentBuilder db = new DocumentBuilder(document);
db.StartTable();
// First Row (Two cells)
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10.0);
db.InsertCell();
db.CellFormat.HorizontalMerge = CellMerge.First;
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(90.0);
db.InsertCell();
db.CellFormat.HorizontalMerge = CellMerge.Previous;
db.EndRow();
// Second Row (Three Cells);
db.CellFormat.ClearFormatting();
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10.0);
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(45.0);
db.InsertCell();
db.CellFormat.PreferredWidth = PreferredWidth.FromPercent(45.0);
db.EndRow();
db.EndTable();
db.Document.Save(@"temp359337\TemplateOut.docx", SaveFormat.Docx);

Hope this will help you. If you have any ambiguity, please share original Word document here so that i can provide you more information.

Hello Imran,

I removed the UpdatePageLayout() and added a table of contents field and a call to UpdateFields() before saving the document. The table is still corrupted.

See attached Program.cs.txt and merged.docx

Regards, Paul

Hi Paul,
Thanks for your reqeust. I would like to clarify a bit regarding horizontally merged cells. By Microsoft Word design, rows in a table in a Microsoft Word document are completely independent. It means each row can have any number of cells of any width. So if you imagine first row with one wide cell and second row with two narrow cells, then looking at this document the cell in the first row will appear horizontally merged. But it is not a merged cell; it is just a single wide cell. Another perfectly valid scenario is when the first row has two cells. First cell has CellMerge.First and second cell has CellMerge.Previous, in this case it is a merged cell. In both cases, the visual appearance in MS Word is exactly the same. Both cases are valid.
Here is simple code, which demonstrates the described things.

// Create empty document and DocumentBuilder object.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Configure DocumentBuilder
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setColor(Color.BLACK);
// Build table, with simply wide cells.
// First row will contains simply wide cell and in MS Word it will look like merged.
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.write("This is simply wide cell");
builder.endRow();
// Insert the second row
builder.insertCell();
builder.getCellFormat().setWidth(100);
builder.insertCell();
builder.getCellFormat().setWidth(100);
builder.endRow();
builder.endTable();
// Insert few paragraphs between table.
builder.writeln();
builder.writeln();
// Build table, with merged cells.
// First row will contains merged cells.
builder.insertCell();
builder.getCellFormat().setWidth(100);
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write("This is merged cells");
builder.insertCell();
builder.getCellFormat().setWidth(100);
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
builder.endRow();
// Insert the second row
builder.insertCell();
builder.getCellFormat().setWidth(100);
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.insertCell();
builder.getCellFormat().setWidth(100);
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.endRow();
builder.endTable();
// Save output document
doc.save("C:\\Temp\\out.doc");

However, in your case you call updateTableLayout. UpdateTableLayout usually gives good result. But sometimes with complex tables it can give incorrect result, i.e. table layout can be broken. This is because algorithm of UpdateTableLayout is not the same as MS Word uses to layout tables. So in your case, I would recommend you to use simply wide cells instead of merged cells.
Best regards,