Skip to content

Commit 61a7c64

Browse files
committed
Better Landscape context knowledge #204
1 parent 237abc6 commit 61a7c64

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/Html2OpenXml/Expressions/BodyExpression.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,27 @@ namespace HtmlToOpenXml.Expressions;
2626
sealed class BodyExpression(IHtmlElement node, ParagraphStyleId? defaultStyle)
2727
: BlockElementExpression(node, defaultStyle)
2828
{
29+
private const uint PortraitPageWidth = 11906U;
30+
private const uint PortraitPageHeight = 16838U;
2931
private bool shouldRegisterTopBookmark;
32+
private ParsingContext? overridenContext;
3033

3134
public override IEnumerable<OpenXmlElement> Interpret(ParsingContext context)
3235
{
3336
MarkAllBookmarks();
3437

38+
var body = context.MainPart.Document.Body!;
39+
var sectionProperties = body.Descendants<SectionProperties>().LastOrDefault();
40+
if (sectionProperties != null)
41+
{
42+
context.IsLandscape = sectionProperties.GetFirstChild<PageSize>()?.Width?.Value >= PortraitPageHeight;
43+
}
44+
3545
var elements = base.Interpret(context);
3646

3747
if (shouldRegisterTopBookmark && elements.Any())
3848
{
3949
// Check whether it already exists
40-
var body = context.MainPart.Document.Body!;
4150
if (body.Descendants<BookmarkStart>().Where(b => b.Name?.Value == "_top").Any())
4251
{
4352
return elements;
@@ -70,6 +79,7 @@ protected override void ComposeStyles(ParsingContext context)
7079
var sectionProperties = mainPart.Document.Body!.GetFirstChild<SectionProperties>();
7180
if (sectionProperties == null || sectionProperties.GetFirstChild<PageSize>() == null)
7281
{
82+
context.IsLandscape = orientation == PageOrientationValues.Landscape;
7383
mainPart.Document.Body.Append(ChangePageOrientation(orientation));
7484
}
7585
else
@@ -80,6 +90,9 @@ protected override void ComposeStyles(ParsingContext context)
8090
SectionProperties validSectionProp = ChangePageOrientation(orientation);
8191
pageSize?.Remove();
8292
sectionProperties.PrependChild(validSectionProp.GetFirstChild<PageSize>()!.CloneNode(true));
93+
94+
overridenContext = context.CreateChild(this);
95+
overridenContext.IsLandscape = orientation == PageOrientationValues.Landscape;
8396
}
8497
}
8598
}
@@ -101,14 +114,14 @@ protected override void ComposeStyles(ParsingContext context)
101114
/// </summary>
102115
private static SectionProperties ChangePageOrientation(PageOrientationValues orientation)
103116
{
104-
PageSize pageSize = new() { Width = (UInt32Value) 16838U, Height = (UInt32Value) 11906U };
117+
PageSize pageSize = new() { Width = PortraitPageWidth, Height = PortraitPageHeight };
105118
if (orientation == PageOrientationValues.Portrait)
106119
{
107-
(pageSize.Height, pageSize.Width) = (pageSize.Width, pageSize.Height);
120+
pageSize.Orient = orientation;
108121
}
109122
else
110123
{
111-
pageSize.Orient = orientation;
124+
(pageSize.Height, pageSize.Width) = (pageSize.Width, pageSize.Height);
112125
}
113126

114127
return new SectionProperties (

src/Html2OpenXml/ParsingContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ sealed class ParsingContext(HtmlConverter converter, OpenXmlPartContainer hostin
4444
/// <summary>Whether the text content should collapse the whitespaces.</summary>
4545
public bool CollapseWhitespaces { get; set; } = true;
4646

47+
/// <summary>Whether the page orientation is portrait or landscape.</summary>
48+
public bool IsLandscape { get; set; }
49+
4750

4851

4952
public void CascadeStyles (OpenXmlElement element)
@@ -57,7 +60,8 @@ public ParsingContext CreateChild(HtmlElementExpression expression)
5760
var childContext = new ParsingContext(Converter, HostingPart, ImageLoader) {
5861
propertyBag = propertyBag,
5962
parentExpression = expression,
60-
parentContext = this
63+
parentContext = this,
64+
IsLandscape = IsLandscape
6165
};
6266
return childContext;
6367
}

0 commit comments

Comments
 (0)