Rich-text with table tag adds uneven space in table cells

Hi, We observed that when the -html flag is used to render rich text containing a table, extra margin or padding gets added to the cells, which causes alignment styles such as top, bottom, and center to not render properly. I have attached the data, template, output, and a screenshot showing the extra space for your reference. Could you please check? Thanks.

Note: We have tried using <<html [expression] -sourceStyles>>, which resolves the alignment issue but introduces a font issue, since it does not match the template style. However, even when using the template style, the extra space still does not align with the default style of a Word table.

richtext-query.zip (38.4 KB)

@pgcentric

It appears you are encountering issues with extra margins or padding in table cells when rendering rich text using the -html flag. This can indeed disrupt the alignment of content within the cells, making it challenging to achieve the desired layout.

You mentioned that using <<html [expression] -sourceStyles>> resolves the alignment issue but introduces font mismatches with your template style. This is a common challenge when dealing with HTML rendering, as default styles may not always align with your specific requirements.

Here are several suggestions to help address the issue:

  1. CSS Adjustments: Consider applying custom CSS styles to your HTML output to control the padding and margins of the table cells. This can help you achieve a more consistent appearance that aligns with your template. For example:

    table {
        border-collapse: collapse;
    }
    td {
        padding: 0; /* Adjust as necessary */
        margin: 0; /* Adjust as necessary */
        vertical-align: top; /* or middle, bottom as needed */
    }
    
  2. HTML Wrapper: If the issue persists, consider wrapping your rich text in a more controlled HTML structure that allows for better styling. This can help mitigate the default styles applied during rendering.

  3. Reviewing Output: If possible, compare the rendered output with the expected output in your template. This can help identify specific discrepancies that can be addressed through CSS or adjustments in the rendering process.

  4. Update Library: Ensure you are using the latest version of the Aspose library, as updates often include bug fixes and improvements that may resolve rendering issues.

If these suggestions do not resolve the problem, please provide additional details or examples of your HTML and CSS for further assistance.

Feel free to reach out if you have more questions or need further clarification!

@pgcentric Behavior is expected. The problem occurs because the paragraph where HTML is inserted has first line indent set:

LINQ Reporting Engine uses DocumentBuilder.insertHtml method for inserting content and by default uses HtmlInsertOptions.USE_BUILDER_FORMATTING. When this option is used, Aspose.Words uses font and paragraph formatting specified in DocumentBuilder as base formatting for text inserted from HTML. In your case except of font paragraph first line indent is also applied. You can reproduce the same behavior using the following simple code:

String cusTable = "<table style=\"width: 100%; margin-left: calc(0%); border-collapse: collapse;\">\n <tbody>\n  <tr>\n   <td style=\"width: 33.3333%; height:200px; vertical-align: top; border: 1px solid #dadada;\">\n    <div style=\"text-align: center;\">\n     Top align\n    </div></td>\n   <td style=\"width: 28.4509%; border: 1px solid #dadada;\"><br></td>\n   <td style=\"width: 38.1266%; vertical-align: middle; border: 1px solid #dadada;\">\n    <div style=\"text-align: center;\">\n    center align\n    </div></td>\n  </tr>\n  <tr>\n   <td style=\"width: 33.3333%; border: 1px solid #dadada;\"><br></td>\n   <td style=\"width: 28.4509%; border: 1px solid #dadada;\"><br></td>\n   <td style=\"width: 38.1266%; border: 1px solid #dadada;\"><br></td>\n  </tr>\n  <tr>\n   <td style=\"width: 33.3333%; height:200px; vertical-align: bottom; border: 1px solid #dadada;\">\n    <div style=\"text-align: center;\">\n     Bottom aligned&nbsp;\n    </div></td>\n   <td style=\"width: 28.4509%; border: 1px solid #dadada;\"><br></td>\n   <td style=\"width: 38.1266%; border: 1px solid #dadada;\">4</td>\n  </tr>\n </tbody>\n</table>";
        
Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(cusTable, HtmlInsertOptions.USE_BUILDER_FORMATTING);
doc.save("C:\\Temp\\out.docx");

in.docx (15.7 KB)
out.docx (11.6 KB)

So to resolve the problem you should simply reset first line indent of the paragraph in your template.

Hi, Thanks. When the tag is placed in a normal paragraph, updating the paragraph settings resolves the issue. However, when it is placed inside a table cell, the output is the same as before. In the paragraph settings, the margin values are already set to 0, so I’m not sure where this extra space is coming from. Could you please check? Thanks!
rich-text-table.zip (25.8 KB)

@pgcentric Paragraph formatting attributes have a hierarchical structure. The SpaceAfter value can be set directly in the paragraph definition in the style of that paragraph or in the default settings for the entire document. The problem is that the default SpaceAfter values differ for different versions of MS Word: for all versions of Word 2007–Word 2021, this value is 8pt, and for Word 2003, it is 10pt.

There is no directly specified value inside the table, so to solve this problem, you need to use doc.getStyles().getDefaultParagraphFormat().setSpaceAfter(0);.