Browse Source

Added new example to dynamic component: footer with alternating position

MarcinZiabek 3 years ago
parent
commit
b7bc640e31
1 changed files with 69 additions and 0 deletions
  1. 69 0
      QuestPDF.Examples/DynamicPageNumberLeftRight.cs

+ 69 - 0
QuestPDF.Examples/DynamicPageNumberLeftRight.cs

@@ -0,0 +1,69 @@
+using System.Collections.Generic;
+using System.Linq;
+using NUnit.Framework;
+using QuestPDF.Elements;
+using QuestPDF.Examples.Engine;
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Examples
+{
+    public class FooterWithAlternatingAlignment : IDynamicComponent<int>
+    {
+        public int State { get; set; }
+        
+        public DynamicComponentComposeResult Compose(DynamicContext context)
+        {
+            var content = context.CreateElement(element =>
+            {
+                element
+                    .Element(x => context.PageNumber % 2 == 0 ? x.AlignLeft() : x.AlignRight())
+                    .Text(x =>
+                    {
+                        x.CurrentPageNumber();
+                        x.Span(" / ");
+                        x.TotalPages();
+                    });
+            });
+            
+            return new DynamicComponentComposeResult()
+            {
+                Content = content,
+                HasMoreContent = false
+            };
+        }
+    }
+    
+    public static class DynamicPageNumberLeftRightExamples
+    {
+        [Test]
+        public static void Dynamic()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(PageSizes.A5)
+                .MaxPages(100)
+                .ShowResults()
+                .ProduceImages()
+                .RenderDocument(container =>
+                {
+                    container.Page(page =>
+                    {
+                        page.Size(PageSizes.A6);
+                        page.PageColor(Colors.White);
+                        page.Margin(1, Unit.Centimetre);
+                        page.DefaultTextStyle(x => x.FontSize(18));
+
+                        page.Content().Column(column =>
+                        {
+                            foreach (var i in Enumerable.Range(0, 50))
+                                column.Item().PaddingTop(25).Background(Colors.Grey.Lighten2).Height(50);
+                        });
+                        
+                        page.Footer().Dynamic(new FooterWithAlternatingAlignment());
+                    });
+                });
+        }
+    }
+}