Detect vertical-align in cell

Hello)
I have html table with cell like this:

<td style="transform: rotate(180deg); writing-mode: tb-rl; text-align: bottom; vertical-align: middle;">vertical</td>

How can detect vertical-align of the cell?

I`m using cellFormat.getVerticalAlignment() but always getting 0, despite vertical-align: middle, bottom or top

@cagecrew

To detect the vertical alignment of a cell in an HTML table when using Aspose.Words, you should utilize the CellFormat.VerticalAlignment property. However, it seems that you are encountering an issue where this property always returns 0, regardless of the vertical alignment specified in your HTML (e.g., vertical-align: middle, vertical-align: bottom, or vertical-align: top).

This behavior may occur because the vertical alignment set in HTML does not directly translate to the properties available in Aspose.Words. The CellFormat.VerticalAlignment property is primarily used for setting and getting the vertical alignment of text in a cell, but it may not reflect the styles applied through HTML.

To ensure that you can retrieve the correct vertical alignment, you might need to explicitly set the vertical alignment in your Aspose.Words code after loading the document. Here’s a simple example of how to set the vertical alignment for a cell:

Document doc = new Document("yourDocument.docx");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
Cell cell = table.getRows().get(0).getCells().get(0);

// Set vertical alignment
cell.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); // or TOP, BOTTOM

doc.save("outputDocument.docx");

If you need to check the vertical alignment after setting it, you can do so like this:

CellVerticalAlignment alignment = cell.getCellFormat().getVerticalAlignment();
System.out.println("Vertical Alignment: " + alignment);

If you continue to experience issues, consider checking if the HTML is being correctly interpreted by Aspose.Words, or if there are any additional styles that might be affecting the rendering of the table cell.

For more detailed information on formatting table cells, you can refer to the documentation on applying formatting to tables, rows, and cells in Aspose.Words here.

If you have further questions or need additional assistance, feel free to ask!

How to directly translate vertical alignment set in HTML style to the properties available in Aspose.Words? Because I need to export this HTML table with all styles to MS Word using Aspose

@cagecrew You can use CellFormat.VerticalAlignment property:

Document doc = new Document("C:\\Temp\\in.html");
Table table = doc.getFirstSection().getBody().getTables().get(0);
int verticalAlignment = table.getFirstRow().getFirstCell().getCellFormat().getVerticalAlignment();
System.out.println(CellVerticalAlignment.getName(verticalAlignment));
1 Like

Yes, I am already using .getCellFormat().getVerticalAlignment(); but I`m always getting “0” despite

<td style="writing-mode: tb-rl; vertical-align: top;">vertical</td>

or

<td style="writing-mode: tb-rl; vertical-align: middle;">vertical</td>

or

<td style="writing-mode: tb-rl; vertical-align: bottom;">vertical</td>

Does Aspose can detect this style tag?

@cagecrew I tested with the following HTML:

<html>
<body>
    <table>
        <tr>
            <td style="vertical-align: middle;">middle</td>
            <td style="vertical-align: top;">top</td>
            <td style="vertical-align: bottom;">bottom</td>
        </tr>
    </table>
</body>
</html>

and the following code:

Document doc = new Document("C:\\Temp\\in.html");
Table table = doc.getFirstSection().getBody().getTables().get(0);
for(Cell c : table.getFirstRow().getCells())
{
    int verticalAlignment = c.getCellFormat().getVerticalAlignment();
    System.out.println(CellVerticalAlignment.getName(verticalAlignment));
}

The output is correct:

CENTER
TOP
BOTTOM

Just figured it out) There was string in code table.setStyleIdentifier(StyleIdentifier.TABLE_GRID);

that makes VerticalAlignment always == TOP
I deleted it and everything is Ok now.

But what table.setStyleIdentifier(StyleIdentifier.TABLE_GRID) mean?

@cagecrew This line applies the TABLE_GRID style to the table and resets all explicitly set formatting.

1 Like