Ver código fonte

Merge pull request #28 from jcl86/main

SkipOnce element for different headers and footers
Marcin Ziąbek 4 anos atrás
pai
commit
bb00dfd4d4

+ 85 - 0
QuestPDF.ReportSample/Layouts/DifferentHeadersTemplate.cs

@@ -0,0 +1,85 @@
+using QuestPDF.Drawing;
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.ReportSample.Layouts
+{
+    public class DifferentHeadersTemplate : IDocument
+    {
+        public DocumentMetadata GetMetadata() => DocumentMetadata.Default;
+
+        public void Compose(IDocumentContainer container)
+        {
+            container
+                .Page(page =>
+                {
+                    page.Margin(40);
+
+                    page.Size(PageSizes.A4);
+
+                    page.Header().Element(ComposeHeader);
+                    page.Content().Element(ComposeContent);
+                    page.Footer().Element(ComposeFooter);
+                });
+        }
+
+        private void ComposeHeader(IContainer container)
+        {
+            container.Background(Colors.Grey.Lighten3).Border(1).Stack(stack =>
+            {
+                stack.Item().ShowOnce().Padding(5).AlignMiddle().Row(row =>
+                {
+                    row.RelativeColumn(2).AlignMiddle().Text("PRIMARY HEADER", TextStyle.Default.Color(Colors.Grey.Darken3).Size(30).Bold());
+                    row.RelativeColumn(1).AlignRight().Box().AlignMiddle().Background(Colors.Blue.Darken2).Padding(30);
+                });
+                stack.Item().SkipOnce().Padding(5).Row(row =>
+                {
+                    row.RelativeColumn(2).Text("SECONDARY HEADER", TextStyle.Default.Color(Colors.Grey.Darken3).Size(30).Bold());
+                    row.RelativeColumn(1).AlignRight().Box().Background(Colors.Blue.Lighten4).Padding(15);
+                });
+            });
+        }
+
+        private void ComposeContent(IContainer container)
+        {
+            container.Stack(stack =>
+            {
+                stack.Item().PaddingVertical(80).Text("First");
+                stack.Item().PageBreak();
+                stack.Item().PaddingVertical(80).Text("Second");
+                stack.Item().PageBreak();
+                stack.Item().PaddingVertical(80).Text("Third");
+                stack.Item().PageBreak();
+            });
+        }
+
+        private void ComposeFooter(IContainer container)
+        {
+            container.Background(Colors.Grey.Lighten3).Stack(stack =>
+            {
+                stack.Item().ShowOnce().Background(Colors.Grey.Lighten3).Row(row =>
+                {
+                    row.RelativeColumn().Text(x =>
+                    {
+                        x.CurrentPageNumber();
+                        x.Span(" / ");
+                        x.TotalPages();
+                    });
+                    row.RelativeColumn().AlignRight().Text("Footer for header");
+                });
+
+                stack.Item().SkipOnce().Background(Colors.Grey.Lighten3).Row(row =>
+                {
+                    row.RelativeColumn().Text(x =>
+                    {
+                        x.CurrentPageNumber();
+                        x.Span(" / ");
+                        x.TotalPages();
+                    });
+                    row.RelativeColumn().AlignRight().Text("Footer for every page except header");
+                });
+            });
+        }
+    }
+}

+ 34 - 0
QuestPDF/Elements/SkipOnce.cs

@@ -0,0 +1,34 @@
+using QuestPDF.Drawing.SpacePlan;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Elements
+{
+    internal class SkipOnce : ContainerElement, IStateResettable
+    {
+        private bool firstPageWasSkiped;
+
+        public void ResetState()
+        {
+            firstPageWasSkiped = false;
+        }
+
+        internal override ISpacePlan Measure(Size availableSpace)
+        {
+            if (Child == null || !firstPageWasSkiped)
+                return new FullRender(Size.Zero);
+
+            return Child.Measure(availableSpace);
+        }
+
+        internal override void Draw(Size availableSpace)
+        {
+            if (Child == null)
+                return;
+
+            if (firstPageWasSkiped)
+                Child.Draw(availableSpace);
+
+            firstPageWasSkiped = true;
+        }
+    }
+}

+ 6 - 1
QuestPDF/Fluent/ElementExtensions.cs

@@ -70,7 +70,12 @@ namespace QuestPDF.Fluent
         {
             return element.Element(new ShowOnce());
         }
-        
+
+        public static IContainer SkipOnce(this IContainer element)
+        {
+            return element.Element(new SkipOnce());
+        }
+
         public static IContainer ShowEntire(this IContainer element)
         {
             return element.Element(new ShowEntire());

+ 3 - 0
readme.md

@@ -37,8 +37,11 @@ The library is available as a nuget package. You can install it as any other nug
 ## Documentation
 
 **[Release notes and roadmap](https://www.questpdf.com/documentation/releases.html)** - everything that is planned for future library iterations, description of new features and information about potential breaking changes.
+
 **[Getting started tutorial](https://www.questpdf.com/documentation/getting-started.html)** - a short and easy to follow tutorial showing how to design an invoice document under 200 lines of code.
+
 **[API Reference](https://www.questpdf.com/documentation/api-reference.html)** - a detailed description of behavior of all available components and how to use them with C# Fluent API.
+
 **[Patterns and practices](https://www.questpdf.com/documentation/patterns-and-practices.html#document-metadata)** - everything that may help you designing great reports and reusable code that is easy to maintain.
 
 ## Example invoice