Inner tables render incorrect

Hi,

Tables placed inside a table cell do not render correctly. 2 different issues:

  • CellFormat.TopPadding is applied to all cells in a row, not only to the individual cell.last call seems to “win”

  • inner table is always rendered with a distance inside the outer cell, the outer borders are not merged, although the padding is set to 0. Word is very ugly, PDF a little better but also 1 pixel inside outer cell.

This occurs in the latest version 13.6.0 and also in older versions.
Please find the examples attached (doc, pdf, java code).

Could you provide me a way to work around this issue and a fix date?
Thanks in advance,
BR,
Torsten

Hi Torsten,

Thanks for your inquiry. I have worked with your shared code and documents and have found that this is not a bug. I have made some modifications in your code, please following highlighted code. Please read following documentation links for your kind reference. Hope this helps you.

https://docs.aspose.com/words/java/applying-formatting/
https://docs.aspose.com/words/java/specifying-table-and-cell-widths/
https://docs.aspose.com/words/java/working-with-tables/

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

public void createDocument_test() throws Exception
{
    Document doc = null;
    try
    {
        doc = new Document();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    // Once the builder is created, its cursor is positioned at the beginning of the document.
    DocumentBuilder m_DocumentBuilder = new DocumentBuilder(doc);
    try
    {
        Table table = m_DocumentBuilder.startTable();
        Cell cell = m_DocumentBuilder.insertCell();
        table.setPreferredWidth(PreferredWidth.fromPercent(100));
        table.getFirstRow().getRowFormat().setHeight(40); //**
        CellFormat cellFormat = cell.getCellFormat();
        cellFormat.setPreferredWidth(PreferredWidth.fromPercent(20)); //**
        cellFormat.setTopPadding(ConvertUtil.millimeterToPoint(2.4));
        cellFormat.setLeftPadding(ConvertUtil.millimeterToPoint(1.9));
        cellFormat.setRightPadding(ConvertUtil.millimeterToPoint(1.9));
        m_DocumentBuilder.write("abc");
        cell = m_DocumentBuilder.insertCell();
        cellFormat = cell.getCellFormat();
        cellFormat.setPreferredWidth(PreferredWidth.fromPercent(60)); //**
        cellFormat.setTopPadding(ConvertUtil.millimeterToPoint(0.0));
        cellFormat.setLeftPadding(ConvertUtil.millimeterToPoint(0.0));
        cellFormat.setRightPadding(ConvertUtil.millimeterToPoint(0.0));
        cellFormat.setBottomPadding(ConvertUtil.millimeterToPoint(0.0));
        cellFormat.getBorders().setDistanceFromText(ConvertUtil.millimeterToPoint(0.0));
        cellFormat.setVerticalAlignment(CellVerticalAlignment.CENTER); //**
        Table table2 = m_DocumentBuilder.startTable();
        Cell cell2 = m_DocumentBuilder.insertCell();
        table2.setPreferredWidth(PreferredWidth.fromPercent(80)); //**
        table2.setAlignment(TableAlignment.CENTER); //**
        table2.getFirstRow().getRowFormat().setHeight(8); //**
        table2.getFirstRow().getRowFormat().setHeightRule(HeightRule.EXACTLY); //**
        cell2 = m_DocumentBuilder.insertCell();
        Row lastRow2 = m_DocumentBuilder.endRow(); //the row that was just finished
        cell2 = m_DocumentBuilder.insertCell();
        cell2 = m_DocumentBuilder.insertCell();
        lastRow2 = m_DocumentBuilder.endRow(); //the row that was just finished
        m_DocumentBuilder.endTable();
        cell = m_DocumentBuilder.insertCell();
        m_DocumentBuilder.write("def");
        cellFormat = cell.getCellFormat();
        cellFormat.setVerticalAlignment(CellVerticalAlignment.TOP);
        cellFormat.setPreferredWidth(PreferredWidth.fromPercent(20));
        cellFormat.setTopPadding(ConvertUtil.millimeterToPoint(1.4));
        cellFormat.setLeftPadding(ConvertUtil.millimeterToPoint(1.9));
        cellFormat.setRightPadding(ConvertUtil.millimeterToPoint(1.9));
        Row lastRow = m_DocumentBuilder.endRow(); //the row that was just finished
    } catch (Throwable e)
    {
        throw new RuntimeException(e);
    }
    try
    {
        PdfSaveOptions saveoptions = new PdfSaveOptions();
        doc.save(MyDir + "AsposeInnerTableTest.pdf", saveoptions);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    try
    {
        DocSaveOptions saveoptions = new DocSaveOptions();
        doc.save(MyDir + "AsposeInnerTableTest.doc", saveoptions);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Hi Tahir,

thanks for your quick and helpful reply.
Setting the preferred width to 100% helped a little, setting the top and bottom padding of the cell to 0px (which is - also in MS Word - always applied to the entire row ) avoids having the ugly space on top / bottom of the inner table, so it now looks similar to the desired (see attached docx). But still there are double borders on top/bottom which maybe cannot be worked around in MS word, and maybe also not in Aspose.words. PDF now also looks quite ok.

Maybe I will have to rework this and use horizontally+vertically splitted cells, to simulate “seamless” inner tables.

Many thanks again,
Torsten

Hi Torsten,

Thanks for your feedback.

THaase:
But still there are double borders on top/bottom which maybe cannot be worked around in MS word, and maybe also not in Aspose.words. PDF now also looks quite ok.

Please note that the Aspose.Words tries to mimic the same behavior as MS Word does. If you create the same document by using MS Word, you will get the same output. In your case, I suggest you please set LineStyle of top/bottom/left/right borders of inner table to LineStyle.NONE. Please check the following code snippet. Hope this helps you.

table.setBorder(BorderType.LEFT, LineStyle.NONE, 0.0, Color.WHITE, true);
table.setBorder(BorderType.RIGHT, LineStyle.NONE, 0.0, Color.WHITE, true);
table.setBorder(BorderType.TOP, LineStyle.NONE, 0.0, Color.WHITE, true);
table.setBorder(BorderType.BOTTOM, LineStyle.NONE, 0.0, Color.WHITE, true);