diff --git a/QuestPDF.Examples/PageBackgroundForeground.cs b/QuestPDF.Examples/PageBackgroundForeground.cs new file mode 100644 index 0000000..ac36a67 --- /dev/null +++ b/QuestPDF.Examples/PageBackgroundForeground.cs @@ -0,0 +1,57 @@ +using System; +using System.Linq; +using NUnit.Framework; +using QuestPDF.Examples.Engine; +using QuestPDF.Fluent; +using QuestPDF.Helpers; +using QuestPDF.Infrastructure; + +namespace QuestPDF.Examples +{ + public class PageBackgroundForeground + { + [Test] + public void Frame() + { + RenderingTest + .Create() + .PageSize(550, 400) + .ProducePdf() + .ShowResults() + .RenderDocument(document => + { + document.Page(page => + { + page.Size(PageSizes.A4); + page.Margin(1, Unit.Inch); + page.DefaultTextStyle(TextStyle.Default.FontSize(16)); + + page.Foreground() + .AlignMiddle() + .AlignCenter() + .Text("Watermark") + .FontSize(64) + .FontColor(Colors.Blue.Lighten4); + + page.Header().Text("Background and foreground").Bold().FontColor(Colors.Blue.Medium).FontSize(24); + + page.Content().PaddingVertical(25).Column(column => + { + column.Spacing(25); + + foreach (var i in Enumerable.Range(0, 100)) + column.Item().Background(Colors.Grey.Lighten2).Height(75); + }); + + page.Footer() + .AlignCenter() + .Text(x => + { + x.Span("Page "); + x.CurrentPageNumber(); + }); + }); + }); + } + } +} \ No newline at end of file diff --git a/QuestPDF/Elements/Page.cs b/QuestPDF/Elements/Page.cs index 7d06bbe..be8e678 100644 --- a/QuestPDF/Elements/Page.cs +++ b/QuestPDF/Elements/Page.cs @@ -20,6 +20,9 @@ namespace QuestPDF.Elements public string BackgroundColor { get; set; } = Colors.Transparent; + public Element Background { get; set; } = Empty.Instance; + public Element Foreground { get; set; } = Empty.Instance; + public Element Header { get; set; } = Empty.Instance; public Element Content { get; set; } = Empty.Instance; public Element Footer { get; set; } = Empty.Instance; @@ -27,39 +30,54 @@ namespace QuestPDF.Elements public void Compose(IContainer container) { container - .MinWidth(MinSize.Width) - .MinHeight(MinSize.Height) - - .MaxWidth(MaxSize.Width) - .MaxHeight(MaxSize.Height) - - .Background(BackgroundColor) - - .PaddingLeft(MarginLeft) - .PaddingRight(MarginRight) - .PaddingTop(MarginTop) - .PaddingBottom(MarginBottom) - - .DefaultTextStyle(DefaultTextStyle) - - .Decoration(decoration => + .Layers(layers => { - decoration - .Before() - .DebugPointer("Page header") - .Element(Header); + layers + .Layer() + .DebugPointer("Page background layer") + .Element(Background); - decoration - .Content() - .Element(x => IsClose(MinSize.Width, MaxSize.Width) ? x.ExtendHorizontal() : x) - .Element(x => IsClose(MinSize.Height, MaxSize.Height) ? x.ExtendVertical() : x) - .DebugPointer("Page content") - .Element(Content); + layers + .PrimaryLayer() + .MinWidth(MinSize.Width) + .MinHeight(MinSize.Height) + + .MaxWidth(MaxSize.Width) + .MaxHeight(MaxSize.Height) + + .Background(BackgroundColor) + + .PaddingLeft(MarginLeft) + .PaddingRight(MarginRight) + .PaddingTop(MarginTop) + .PaddingBottom(MarginBottom) + + .DefaultTextStyle(DefaultTextStyle) + + .Decoration(decoration => + { + decoration + .Before() + .DebugPointer("Page header") + .Element(Header); + + decoration + .Content() + .Element(x => IsClose(MinSize.Width, MaxSize.Width) ? x.ExtendHorizontal() : x) + .Element(x => IsClose(MinSize.Height, MaxSize.Height) ? x.ExtendVertical() : x) + .DebugPointer("Page content") + .Element(Content); + + decoration + .After() + .DebugPointer("Page footer") + .Element(Footer); + }); - decoration - .After() - .DebugPointer("Page footer") - .Element(Footer); + layers + .Layer() + .DebugPointer("Page foreground layer") + .Element(Foreground); }); bool IsClose(float x, float y) diff --git a/QuestPDF/Fluent/PageExtensions.cs b/QuestPDF/Fluent/PageExtensions.cs index ab968ee..c336160 100644 --- a/QuestPDF/Fluent/PageExtensions.cs +++ b/QuestPDF/Fluent/PageExtensions.cs @@ -88,6 +88,20 @@ namespace QuestPDF.Fluent Page.BackgroundColor = color; } + public IContainer Background() + { + var container = new Container(); + Page.Background = container; + return container; + } + + public IContainer Foreground() + { + var container = new Container(); + Page.Foreground = container; + return container; + } + public IContainer Header() { var container = new Container();