When my HTML table has a merged column and the next column has n rows of even height, converting the content to DOCX loses the even spacing. Even spacing is something that can be set in Word directly as well.
A table that looks like this:
Becomes like this:
@bmentzer Could you please attach your sample input HTML and output document here for testing? We will check the issue and provide you more information.
Unfortunately, it is impossible to analyze the issue using screenshots.
Attached is my input and output examples:
InputOutput.zip (7.9 KB)
@bmentzer Aspose.Words behave the same way as MS Word does in this case. So the behavior is correct. To get the desired output, in your case, you can explicitly specify row height:
<style>
table, th, td {
border: 1px solid black;
}
tr {
height: 80pt;
}
</style>
In this case the output looks like this: out.docx (8.3 KB)
Here is the modified input: in.zip (797 Bytes)
@alexey.noskov
My example happens to be this size, but my content in the merged cell can vary. Until the html is converted to the docx I don’t know what size to set the rows to. As such, I can’t hard code the rows to specific sizes. Word has a right click option for distributing rows evenly. Is there an option during the conversion, after I load the html into the aspose doc to evenly distribute the rows?
@bmentzer Distribute Rows Evenly
option in MS Word actually sets row height explicitly. To achieve this it is required to calculate actual height of the table and recalculate height of the rows. You can achieve this using LayoutCollector and LayoutEnumerator. The following code demonstrates a basic technique:
Document doc = new Document(@"C:\Temp\in.html");
// Use LayoutCollector and LayoutEnumerator to calculate actual height of the table.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Get the table.
Table table = doc.FirstSection.Body.Tables[0];
// Calculate bounding box of the first and the last row of the table.
enumerator.Current = collector.GetEntity(table.FirstRow.FirstCell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Row)
enumerator.MoveParent();
RectangleF firstRowRect = enumerator.Rectangle;
enumerator.Current = collector.GetEntity(table.LastRow.FirstCell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Row)
enumerator.MoveParent();
RectangleF lastRowRect = enumerator.Rectangle;
// Calculate building box of the whole table.
// NOTE: the code demonstrates the basic technique and works only when whole table is on one page.
RectangleF tableRect = RectangleF.Union(firstRowRect, lastRowRect);
// Calculate height of each row.
double rowHeight = tableRect.Height / table.Rows.Count;
// Set height of the rows.
foreach (Row r in table.Rows)
r.RowFormat.Height = rowHeight;
doc.Save(@"C:\Temp\out.docx");
Note, the table can span several pages, the provided code demonstrates only the basic technique with a table on a single page.