Call to UpdatePageLayout corrupts table layout

Last post 02-13-2012, 3:19 PM by alexey.noskov. 10 replies.
Sort Posts: Previous Next
  •  02-05-2012, 3:40 PM 359337

    Call to UpdatePageLayout corrupts table layout .NET

    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);



     
  •  02-05-2012, 9:43 PM 359341 in reply to 359337

    Re: Call to UpdatePageLayout corrupts table layout

    Attachment: Present (inaccessible)
    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.



    Best Regards,
    Imran Rafique
    Support Developer
    Aspose Sialkot Team
    http://www.aspose.com/
    Aspose - Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-06-2012, 1:42 AM 359374 in reply to 359341

    Re: Call to UpdatePageLayout corrupts table layout

    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:

    http://docs.aspose.com/display/wordsnet/Document+Members

     

    So, just remove this line of code.

     

    Best regards,


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  02-06-2012, 3:48 AM 359410 in reply to 359341

    Re: Call to UpdatePageLayout corrupts table layout

    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.
     
  •  02-06-2012, 4:52 AM 359436 in reply to 359410

    Re: Call to UpdatePageLayout corrupts table layout

    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.


    Best Regards,
    Imran Rafique
    Support Developer
    Aspose Sialkot Team
    http://www.aspose.com/
    Aspose - Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-06-2012, 4:59 AM 359439 in reply to 359436

    Re: Call to UpdatePageLayout corrupts table layout .NET

    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
     
  •  02-06-2012, 5:31 AM 359449 in reply to 359439

    Re: Call to UpdatePageLayout corrupts table layout

    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:


    Best Regards,
    Imran Rafique
    Support Developer
    Aspose Sialkot Team
    http://www.aspose.com/
    Aspose - Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-06-2012, 5:38 AM 359451 in reply to 359449

    Re: Call to UpdatePageLayout corrupts table layout .NET

    Attachment: Present (inaccessible)
    Hello Imran,

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

    Regards, Paul.
     
  •  02-07-2012, 3:33 AM 359677 in reply to 359451

    Re: Call to UpdatePageLayout corrupts table layout

    Attachment: Present (inaccessible)
    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.



    Best Regards,
    Imran Rafique
    Support Developer
    Aspose Sialkot Team
    http://www.aspose.com/
    Aspose - Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-10-2012, 3:38 PM 360829 in reply to 359677

    Re: Call to UpdatePageLayout corrupts table layout .NET

    Attachment: Present (inaccessible)
    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
     
  •  02-13-2012, 3:19 PM 361220 in reply to 360829

    Re: Call to UpdatePageLayout corrupts table layout

    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,


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
View as RSS news feed in XML