Skip to content

Commit 1d7fec6

Browse files
committed
Fix horizontally merged cells in RTF to DOCX converter
1 parent 2c887cd commit 1d7fec6

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

documentation/Supported_features.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ However, some charts may already be OLE objects (Excel/MS Graph object; usually
100100
- ❌ Paragraph group formatting
101101
- ❌ Styles (however the formatting is usually written inline in RTF for compatibility reasons, even if there is an associated style too)
102102
- 🟡 Lists (note: there are still issues that need to be resolved)
103-
- Tables
103+
- 🟡 Tables
104104
- 🟡 Section properties
105105
- 🟡 Default section properties / document-level formatting and settings
106106
- 🟡 Fields (supported but additional work is needed for some complex fields and form fields)

src/DocSharp.Docx/RtfToDocx/RtfToDocxConverter.Table.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ private void EndTableRow()
9090
return;
9191
}
9292
var table = container.LastChild as Table ?? container.AppendChild(new Table());
93+
94+
// We have to add GridSpan to cells based on their width and the number of cells,
95+
// otherwise horizontally merged cells do not behave as expected, even if they width is increased.
96+
// The following approach is not ideal, but I can't think of a better way at this time.
97+
int minWidth = Math.Max(pendingTableRow.Elements<TableCell>().Min(c => (c.TableCellProperties?.TableCellWidth?.Width?.Value).ToIntInvariant(0)), 1);
98+
foreach (var cell in pendingTableRow.Elements<TableCell>())
99+
{
100+
int cellWidth = Math.Max((cell.TableCellProperties?.TableCellWidth?.Width?.Value).ToIntInvariant(0), 1);
101+
int gridSpan = (int)Math.Round((double)cellWidth / minWidth, MidpointRounding.AwayFromZero);
102+
// Grid span is always at least 1.
103+
if (gridSpan > 1)
104+
cell.TableCellProperties!.GridSpan = new GridSpan() { Val = gridSpan };
105+
}
106+
93107
table.AppendChild(pendingTableRow);
94108
pendingTableRow = null;
95109
// Do not reset row properties and exceptions here, as they can be inherited by subsequent rows until a new trowd control word is encountered
@@ -139,6 +153,7 @@ private bool ProcessTableControlWord(RtfControlWord cw)
139153
currentTableRowExceptions = new();
140154
inTableRowDefinition = true;
141155
cellIndex = 0;
156+
cellx = 0;
142157
return true;
143158
case "row":
144159
case "nestrow":

0 commit comments

Comments
 (0)