Skip to content

Commit 90bf035

Browse files
committed
Constraint table within a numbering list to not exceed page margin #202
1 parent e809010 commit 90bf035

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Html2OpenXml/Expressions/Numbering/ListExpression.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,20 @@ public override IEnumerable<OpenXmlElement> Interpret(ParsingContext context)
9393

9494
// table must be aligned to the list item
9595
var tables = childElements.OfType<Table>();
96+
var tableIndentation = level * Indentation * 2;
9697
foreach (var table in tables)
9798
{
9899
var tableProperties = table.GetFirstChild<TableProperties>();
99100
if (tableProperties == null)
100101
table.PrependChild(tableProperties = new());
101102

102-
tableProperties.TableIndentation ??= new() { Width = level * Indentation * 2 };
103+
tableProperties.TableIndentation ??= new() { Width = tableIndentation };
104+
// ensure to restrain the table width to the list item
105+
if (tableProperties.TableWidth?.Type?.Value == TableWidthUnitValues.Pct
106+
&& tableProperties.TableWidth?.Width?.Value == "5000")
107+
{
108+
tableProperties.TableWidth.Width = (5000 - tableIndentation).ToString();
109+
}
103110
}
104111

105112
// ensure to filter out any non-paragraph like any nested table

test/HtmlToOpenXml.Tests/NumberingTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ public void NestedTable_ReturnsAlignedTable()
636636
Assert.That(tableProperties, Is.Not.Null);
637637
Assert.That(tableProperties.TableIndentation, Is.Not.Null);
638638
Assert.That(tableProperties.TableIndentation.Width?.Value, Is.EqualTo(720 * (i+1)));
639+
Assert.That(tableProperties.TableWidth, Is.Not.Null);
640+
Assert.That(tableProperties.TableWidth.Type?.Value, Is.EqualTo(TableWidthUnitValues.Pct));
641+
Assert.That(tableProperties.TableWidth.Width?.HasValue, Is.True);
642+
Assert.That(Convert.ToInt32(tableProperties.TableWidth.Width.Value), Is.GreaterThan(0).And.LessThan(5000));
639643
}
640644
}
641645
}

0 commit comments

Comments
 (0)