diff --git a/QuestPDF.Examples/ComplexLayoutBenchmark.cs b/QuestPDF.Examples/ComplexLayoutBenchmark.cs new file mode 100644 index 0000000..8f7df56 --- /dev/null +++ b/QuestPDF.Examples/ComplexLayoutBenchmark.cs @@ -0,0 +1,57 @@ +using NUnit.Framework; +using QuestPDF.Examples.Engine; +using QuestPDF.Fluent; +using QuestPDF.Helpers; +using QuestPDF.Infrastructure; + +namespace QuestPDF.Examples +{ + public class ComplexLayoutBenchmark + { + [Test] + public void ComplexLayout() + { + RenderingTest + .Create() + .PageSize(PageSizes.A4) + .ProducePdf() + .ShowResults() + .Render(x => x.Image(new byte[] { 1, 2, 3 })); + //.Render(x => GenerateStructure(x, 16)); + } + + private void GenerateStructure(IContainer container, int level) + { + if (level <= 0) + { + container.Background(Placeholders.BackgroundColor()).Height(10); + return; + } + + level--; + + if (level % 3 == 0) + { + container + .Border(level / 10f) + .BorderColor(Colors.Black) + .Row(row => + { + row.RelativeColumn().Element(x => GenerateStructure(x, level)); + row.RelativeColumn().Element(x => GenerateStructure(x, level)); + }); + } + else + { + container + .Border(level / 10f) + .BorderColor(Colors.Black) + .Stack(stack => + { + stack.Item().Element(x => GenerateStructure(x, level)); + stack.Item().Element(x => GenerateStructure(x, level)); + }); + } + } + } +} \ No newline at end of file diff --git a/QuestPDF.Examples/Engine/RenderingTest.cs b/QuestPDF.Examples/Engine/RenderingTest.cs index 6279a99..3309c4a 100644 --- a/QuestPDF.Examples/Engine/RenderingTest.cs +++ b/QuestPDF.Examples/Engine/RenderingTest.cs @@ -18,6 +18,7 @@ namespace QuestPDF.Examples.Engine { private string FileNamePrefix = "test"; private Size Size { get; set; } + private int? MaxPagesThreshold { get; set; } private bool ShowResult { get; set; } private RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images; @@ -77,11 +78,16 @@ namespace QuestPDF.Examples.Engine }); }); } + + public void MaxPages(int value) + { + MaxPagesThreshold = value; + } public void RenderDocument(Action content) { - var maxPages = ResultType == RenderingTestResult.Pdf ? 1000 : 10; - var document = new SimpleDocument(content, maxPages); + MaxPagesThreshold ??= ResultType == RenderingTestResult.Pdf ? 1000 : 10; + var document = new SimpleDocument(content, MaxPagesThreshold.Value); Render(document); } diff --git a/QuestPDF.Examples/ImageExamples.cs b/QuestPDF.Examples/ImageExamples.cs new file mode 100644 index 0000000..89801bf --- /dev/null +++ b/QuestPDF.Examples/ImageExamples.cs @@ -0,0 +1,41 @@ +using System.IO; +using NUnit.Framework; +using QuestPDF.Examples.Engine; +using QuestPDF.Fluent; +using QuestPDF.Helpers; + +namespace QuestPDF.Examples +{ + public class ImageExamples + { + [Test] + public void LoadingImage() + { + RenderingTest + .Create() + .PageSize(PageSizes.A5) + .ProducePdf() + .ShowResults() + .Render(page => + { + page.Padding(25).Stack(stack => + { + stack.Spacing(25); + stack.Item().Image(File.ReadAllBytes("logo.png")); + stack.Item().Image("logo.png"); + }); + }); + } + + [Test] + public void Exception() + { + RenderingTest + .Create() + .PageSize(PageSizes.A5) + .ProducePdf() + .ShowResults() + .Render(page => page.Image("non_existent.png")); + } + } +} \ No newline at end of file diff --git a/QuestPDF.Examples/QuestPDF.Examples.csproj b/QuestPDF.Examples/QuestPDF.Examples.csproj index 5dc14b7..a7a0014 100644 --- a/QuestPDF.Examples/QuestPDF.Examples.csproj +++ b/QuestPDF.Examples/QuestPDF.Examples.csproj @@ -23,6 +23,9 @@ PreserveNewest + + PreserveNewest + diff --git a/QuestPDF.Examples/logo.png b/QuestPDF.Examples/logo.png new file mode 100644 index 0000000..a0d780f Binary files /dev/null and b/QuestPDF.Examples/logo.png differ diff --git a/QuestPDF/Fluent/ImageExtensions.cs b/QuestPDF/Fluent/ImageExtensions.cs index 7a3fe4e..bc29883 100644 --- a/QuestPDF/Fluent/ImageExtensions.cs +++ b/QuestPDF/Fluent/ImageExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using QuestPDF.Drawing.Exceptions; using QuestPDF.Elements; using QuestPDF.Infrastructure; using SkiaSharp; @@ -7,12 +9,29 @@ namespace QuestPDF.Fluent { public static class ImageExtensions { - public static void Image(this IContainer parent, byte[] data, ImageScaling scaling = ImageScaling.FitWidth) + public static void Image(this IContainer parent, byte[] imageData, ImageScaling scaling = ImageScaling.FitWidth) { - if (data == null) - return; + var image = SKImage.FromEncodedData(imageData); + parent.Image(image, scaling); + } + + public static void Image(this IContainer parent, string filePath, ImageScaling scaling = ImageScaling.FitWidth) + { + var image = SKImage.FromEncodedData(filePath); + parent.Image(image, scaling); + } + + public static void Image(this IContainer parent, Stream fileStream, ImageScaling scaling = ImageScaling.FitWidth) + { + var image = SKImage.FromEncodedData(fileStream); + parent.Image(image, scaling); + } + + private static void Image(this IContainer parent, SKImage image, ImageScaling scaling = ImageScaling.FitWidth) + { + if (image == null) + throw new DocumentComposeException("Cannot load or decode provided image."); - var image = SKImage.FromEncodedData(data); var aspectRatio = image.Width / (float)image.Height; var imageElement = new Image diff --git a/QuestPDF/QuestPDF.csproj b/QuestPDF/QuestPDF.csproj index bd435f7..e74e2fb 100644 --- a/QuestPDF/QuestPDF.csproj +++ b/QuestPDF/QuestPDF.csproj @@ -4,7 +4,7 @@ MarcinZiabek CodeFlint QuestPDF - 2021.11.0-beta + 2021.11.0-beta2 QuestPDF is an open-source, modern and battle-tested library that can help you with generating PDF documents by offering friendly, discoverable and predictable C# fluent API. Implemented new elements: SkipOnce and Inlined. Added possibility to define global, page-wide test style. Improved exception handling experience. 8