diff --git a/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedDocument.cs b/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedDocument.cs index 3c9c8b24..be4391b6 100644 --- a/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedDocument.cs +++ b/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedDocument.cs @@ -193,10 +193,16 @@ Rectangle CalcContentRect(int page) XUnitPt height = pageSetup.PageHeight.Point; - height -= pageSetup.TopMargin.Point; - height -= pageSetup.BottomMargin.Point; + double topMargin = pageSetup.TopMargin.Point; + if (_formattedHeaders.TryGetValue(new HeaderFooterPosition(_sectionNumber, CurrentPagePosition), out FormattedHeaderFooter? header)) + topMargin = Math.Max(topMargin, header.ContentRect.Y.Point + header.ContentRect.Height.Point); + height -= topMargin; + double bottomMargin = pageSetup.BottomMargin.Point; + if (_formattedFooters.TryGetValue(new HeaderFooterPosition(_sectionNumber, CurrentPagePosition), out FormattedHeaderFooter? footer)) + bottomMargin = Math.Max(bottomMargin, pageSetup.PageHeight.Point - footer.ContentRect.Y.Point); + height -= bottomMargin; XUnitPt x; - XUnitPt y = pageSetup.TopMargin.Point; + XUnitPt y = topMargin; if (pageSetup.MirrorMargins) x = page % 2 == 0 ? pageSetup.RightMargin.Point : pageSetup.LeftMargin.Point; else @@ -380,8 +386,9 @@ Area IAreaProvider.GetNextArea() ++_sectionPages; InitFieldInfos(); FormatHeadersFooters(); + Rectangle rect = CalcContentRect(_currentPage); _isNewSection = false; - return CalcContentRect(_currentPage); + return rect; } int _currentPage; diff --git a/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedHeaderFooter.cs b/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedHeaderFooter.cs index 64940be9..bd249f37 100644 --- a/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedHeaderFooter.cs +++ b/src/foundation/src/MigraDoc/src/MigraDoc.Rendering/Rendering/FormattedHeaderFooter.cs @@ -25,6 +25,13 @@ internal void Format(XGraphics gfx) _formatter = new TopDownFormatter(this, _documentRenderer, _headerFooter.Elements); _formatter.FormatOnAreas(gfx, false); _contentHeight = RenderInfo.GetTotalHeight(GetRenderInfos()); + if (_headerFooter.IsHeader) + ContentRect.Height = _contentHeight; + if (_headerFooter.IsFooter) + { + ContentRect.Y = ContentRect.Y + ContentRect.Height - _contentHeight; + ContentRect.Height = _contentHeight; + } } Area? IAreaProvider.GetNextArea()