@@ -26,18 +26,27 @@ namespace HtmlToOpenXml.Expressions;
2626sealed 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 (
0 commit comments