Browse Source

Added support for background and foreground in page API

MarcinZiabek 3 years ago
parent
commit
2c14f91ebe

+ 57 - 0
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();
+                            });
+                    });
+                });
+        }
+    }
+}

+ 45 - 27
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)
+                .Layers(layers =>
+                {
+                    layers
+                        .Layer()
+                        .DebugPointer("Page background layer")
+                        .Element(Background);
+                    
+                    layers
+                        .PrimaryLayer()
+                        .MinWidth(MinSize.Width)
+                        .MinHeight(MinSize.Height)
                 
-                .MaxWidth(MaxSize.Width)
-                .MaxHeight(MaxSize.Height)
+                        .MaxWidth(MaxSize.Width)
+                        .MaxHeight(MaxSize.Height)
                 
-                .Background(BackgroundColor)
+                        .Background(BackgroundColor)
      
-                .PaddingLeft(MarginLeft)
-                .PaddingRight(MarginRight)
-                .PaddingTop(MarginTop)
-                .PaddingBottom(MarginBottom)
+                        .PaddingLeft(MarginLeft)
+                        .PaddingRight(MarginRight)
+                        .PaddingTop(MarginTop)
+                        .PaddingBottom(MarginBottom)
                 
-                .DefaultTextStyle(DefaultTextStyle)
+                        .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(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)

+ 14 - 0
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();