| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- using System;
- using System.Linq;
- using NUnit.Framework;
- using QuestPDF.Drawing.Exceptions;
- using QuestPDF.Elements;
- using QuestPDF.Examples.Engine;
- using QuestPDF.Fluent;
- using QuestPDF.Helpers;
- using QuestPDF.Infrastructure;
- namespace QuestPDF.Examples
- {
- public class ContentDirectionExamples
- {
- private static Action<IContainer> ContentDirectionTemplate(Action<IContainer> content)
- {
- return container =>
- {
- container
- .MinimalBox()
- .ExtendHorizontal()
- .Table(table =>
- {
- table.ColumnsDefinition(columns =>
- {
- columns.RelativeColumn();
- columns.ConstantColumn(1);
- columns.RelativeColumn();
- });
-
- table.Header(header =>
- {
- header.Cell().ContentFromLeftToRight().Element(HeaderCell("Left-to-right"));
- header.Cell().LineVertical(1).LineColor(Colors.Grey.Medium);
- header.Cell().ContentFromRightToLeft().Element(HeaderCell("Right-to-left"));
- static Action<IContainer> HeaderCell(string label)
- {
- return container => container
- .BorderColor(Colors.Grey.Medium)
- .Background(Colors.Grey.Lighten2)
- .PaddingHorizontal(15)
- .PaddingVertical(5)
- .Text(label)
- .FontSize(18)
- .SemiBold();
- }
- });
- table.Cell().Element(TestCell).ContentFromLeftToRight().Element(content);
- table.Cell().LineVertical(1).LineColor(Colors.Grey.Medium);
- table.Cell().Element(TestCell).ContentFromRightToLeft().Element(content);
-
- static IContainer TestCell(IContainer container)
- {
- return container.Padding(15);
- }
- });
- };
- }
-
- [Test]
- public void Page()
- {
- RenderingTest
- .Create()
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .RenderDocument(document =>
- {
- document.Page(page =>
- {
- page.Size(PageSizes.A5);
- page.Margin(20);
- page.PageColor(Colors.White);
-
- page.DefaultTextStyle(x => x.FontFamily("Calibri").FontSize(20));
- page.ContentFromRightToLeft();
-
- page.Content().Column(column =>
- {
- column.Spacing(20);
- column.Item()
- .Text("مثال على الفاتورة") // example invoice
- .FontSize(32).FontColor(Colors.Blue.Darken2).SemiBold();
-
- column.Item().Table(table =>
- {
- table.ColumnsDefinition(columns =>
- {
- columns.RelativeColumn();
- columns.ConstantColumn(75);
- columns.ConstantColumn(100);
- });
- table.Cell().Element(HeaderStyle).Text("وصف السلعة"); // item description
- table.Cell().Element(HeaderStyle).Text("كمية"); // quantity
- table.Cell().Element(HeaderStyle).Text("سعر"); // price
- var items = new[]
- {
- "دورة البرمجة", // programming course
- "دورة تصميم الرسومات", // graphics design course
- "تحليل وتصميم الخوارزميات", // analysis and design of algorithms
- };
-
- foreach (var item in items)
- {
- var price = Placeholders.Random.NextDouble() * 100;
-
- table.Cell().Text(item);
- table.Cell().Text(Placeholders.Random.Next(1, 10));
- table.Cell().Text($"USD${price:F2}");
- }
- static IContainer HeaderStyle(IContainer x) => x.BorderBottom(1).PaddingVertical(5);
- });
- });
- });
- });
- }
- [Test]
- public void Column()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Column(column =>
- {
- column.Spacing(5);
- column.Item().Height(50).Width(50).Background(Colors.Red.Lighten1);
- column.Item().Height(50).Width(100).Background(Colors.Green.Lighten1);
- column.Item().Height(50).Width(150).Background(Colors.Blue.Lighten1);
- });
- }
- }
-
- [Test]
- public void Row()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Row(row =>
- {
- row.Spacing(5);
-
- row.AutoItem().Height(50).Width(50).Background(Colors.Red.Lighten1);
- row.AutoItem().Height(50).Width(50).Background(Colors.Green.Lighten1);
- row.AutoItem().Height(50).Width(75).Background(Colors.Blue.Lighten1);
- });
- }
- }
-
- [Test]
- public void Table()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Table(table =>
- {
- table.ColumnsDefinition(columns =>
- {
- columns.RelativeColumn();
- columns.RelativeColumn();
- columns.RelativeColumn();
- });
-
- table.Cell().Height(50).Background(Colors.Red.Lighten1);
- table.Cell().Height(50).Background(Colors.Green.Lighten1);
- table.Cell().Height(50).Background(Colors.Blue.Lighten1);
- table.Cell().ColumnSpan(2).Height(50).Background(Colors.Orange.Lighten1);
- });
- }
- }
-
- [Test]
- public void Constrained()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Width(50).Height(50).Background(Colors.Red.Lighten1);
- }
- }
-
- [Test]
- public void Unconstrained()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container
- .Width(100)
- .Height(100)
- .Background(Colors.Grey.Lighten3)
- .AlignCenter()
- .AlignMiddle()
-
- .Unconstrained()
-
- .Width(50)
- .Height(50)
- .Background(Colors.Red.Lighten1);
- }
- }
-
- [Test]
- public void Inlined()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Column(column =>
- {
- column.Spacing(10);
- column.Item().Text("Default alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => { }));
-
- column.Item().Text("Left alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignLeft()));
-
- column.Item().Text("Center alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignCenter()));
-
- column.Item().Text("Right alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignRight()));
-
- column.Item().Text("Justify alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignJustify()));
-
- column.Item().Text("Space around alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignSpaceAround()));
- });
-
- static Action<IContainer> ContentWithAlignment(Action<InlinedDescriptor> configure)
- {
- return container =>
- {
- container.Inlined(inlined =>
- {
- inlined.Spacing(5);
- configure(inlined);
-
- inlined.Item().Height(40).Width(50).Background(Colors.Red.Lighten1);
- inlined.Item().Height(40).Width(75).Background(Colors.Green.Lighten1);
- inlined.Item().Height(40).Width(100).Background(Colors.Blue.Lighten1);
- inlined.Item().Height(40).Width(125).Background(Colors.Orange.Lighten1);
- });
- };
- }
- }
- }
-
- [Test]
- public void Text()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Column(column =>
- {
- column.Spacing(10);
- column.Item().Text("Default alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => { }));
-
- column.Item().Text("Left alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignLeft()));
-
- column.Item().Text("Center alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignCenter()));
-
- column.Item().Text("Right alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignRight()));
-
- column.Item().Text("Justify alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.Justify()));
-
- column.Item().Text("Start alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignStart()));
-
- column.Item().Text("End alignment").FontSize(14).SemiBold();
- column.Item().Element(ContentWithAlignment(x => x.AlignEnd()));
- });
- static Action<IContainer> ContentWithAlignment(Action<TextDescriptor> configure)
- {
- return container =>
- {
- container.Text(text =>
- {
- configure(text);
- text.Span("Dotnet").Bold().FontColor(Colors.Red.Medium);
- text.Span(" is an open-source platform for building desktop, web, and mobile applications that can run natively on any operating system.");
- });
- };
- }
- }
- }
-
- [Test]
- public void Decoration()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Decoration(decoration =>
- {
- decoration.Before().Background(Colors.Green.Lighten1).Padding(5).Text("Before").FontSize(16);
- decoration.Content().Background(Colors.Green.Lighten2).Padding(5).Text("Content").FontSize(16);
- decoration.After().Background(Colors.Green.Lighten3).Padding(5).Text("After").FontSize(16);
- });
- }
- }
-
- [Test]
- public void Dynamic()
- {
- RenderingTest
- .Create()
- .PageSize(PageSizes.A4)
- .ProduceImages()
- .ShowResults()
- .EnableDebugging()
- .Render(ContentDirectionTemplate(Content));
- void Content(IContainer container)
- {
- container.Dynamic(new SimpleDynamic());
- }
- }
-
- class SimpleDynamic : IDynamicComponent
- {
- public DynamicComponentComposeResult Compose(DynamicContext context)
- {
- var content = context.CreateElement(container =>
- {
- container.Row(row =>
- {
- row.ConstantItem(50).Background(Colors.Red.Lighten2).Height(50);
- row.ConstantItem(75).Background(Colors.Green.Lighten2).Height(50);
- row.ConstantItem(100).Background(Colors.Blue.Lighten2).Height(50);
- });
- });
-
- return new DynamicComponentComposeResult
- {
- Content = content,
- HasMoreContent = false
- };
- }
- }
- }
- }
|