Table layout is incorrect after inserting HTML into document with InsertHtml using Java

I’m trying to build a document from HTML snippets alternating with tables (containing some meta info about the HTML snippets). When an HTML snippet contains a table, the table formatting of the builder is changed. How can I reset the builder to it’s prior settings?

Example code follows, resulting document attached.

public void testTable() throws Exception {
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    insertTable(builder);
    builder.insertHtml("<p>Html Paragraph</p>");
    insertTable(builder);
    builder.insertHtml("<table><tr><td>Html Table</td></tr></table>");
    insertTable(builder);
        
    doc.save("c:/tmp/aspose/TestTable.doc");
}

private void insertTable(DocumentBuilder builder) {
    builder.insertParagraph();
    builder.startTable();
    builder.insertCell();
    builder.write("Inserted table by builder.");
    builder.endTable();
    builder.insertParagraph();
}

Hi Jens,

Thanks for your query. I have modified your insertTable function code. Please use following insertTable function and read this documentation link for your kind reference.

public static void insertTable(DocumentBuilder builder)
{
    builder.insertParagraph();
    Table table = builder.startTable();
    builder.insertCell();
    table.setPreferredWidth(PreferredWidth.fromPercent(100));
    table.setBorders(LineStyle.SINGLE, 1, Color.BLACK);
    builder.write("Inserted table by builder.");
    builder.endTable();
    builder.insertParagraph();
}

Thanks, at first this seemed to help.
Unfortunately it doesn’t work when exporting the document to pdf. (Aspose.Words 11.0.0)

doc.save("c:/tmp/aspose/TestTable.pdf");

Hi Jen,

Please use following line of code before saving the pdf.

doc.updateTableLayout();

Thank you Tahir,

I meanwhile found that hint in many forum entries, but unfortunately it doesn’t really help: the tables inserted by insertTable are OK afterwards, but the table inserted by insertHtml is unnecessary made smaller (see attached). In more complicted layouts many tables get corrupt afterwards or are unchanged when they should be changed. It seems that updateTableLayout() does some heuristic only (, but most probably this is all that can be done).

Is it preferable to use the DocumentBuilder only to insert the HTML parts and construct the other tables directly into the DOM? Is it recommended to merge DocumentBuilder and DOM actions in general, and what must be considered then?

Hi Jen,

In your shared code you did not mention the table’s width and border. Please use the following line of code to insert table by using insertHtml method. Please find the generated output pdf file in attachment.

builder.insertHtml("<table width='100%' border='1'><tr><td>Html Table</td></tr></table>");

Hi Tahir,

the monster has arisen again when I tried to combine mergecells and html. The table is totally messed up in the word document even after inserting a simple html paragraph. The pdf is more friendly, but doesn’t manage the merge after inserting an html table. Is there any documentation about how to reset the builder after inserting html?

public void testTable() throws Exception {
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    insertMergeCellTable(builder);
    builder.insertHtml("<p>Html Paragraph</p>");
    insertMergeCellTable(builder);
    builder.insertHtml("<table><tr><td>Html Table</td></tr></table>");
    insertMergeCellTable(builder);
    doc.updateTableLayout();
    doc.save("c:/tmp/aspose/TestTable.doc");
    doc.save("c:/tmp/aspose/TestTable.pdf");
}
    
private void insertMergeCellTable(DocumentBuilder builder) {
    builder.startTable();
    builder.insertCell();
    builder.write("Text in one cell.");

    builder.insertCell();
    builder.write("Text in another cell.");
    builder.endRow();

    builder.insertCell();
    builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
    builder.write("Text in merged cells.");

    builder.insertCell();
    // This cell is merged to the previous and should be empty.
    builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
    builder.endRow();
    Table table = builder.endTable();
    table.setPreferredWidth(PreferredWidth.fromPercent(100));
    table.setBorders(LineStyle.SINGLE, 1, Color.BLACK);
}

Regards
Jens

Hi Jen,

I have tested the scenario and have managed to reproduce the same issue at my end. I have logged this issue in our issue tracking system and you will be updated via this forum thread once this issue is resolved.

We apology for your inconvenience.

Hi Jen,
Thanks for your inquiry. 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. That is why it is normally don recommended to call updateTableLayout when you save your output document to MS Word formats. Sometimes it is necessary to call this method when you render document.
Best regards,