Files

95 lines
2.5 KiB
C#
Raw Permalink Normal View History

2021-06-09 23:34:25 +02:00
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
2021-06-09 23:34:25 +02:00
using QuestPDF.Elements;
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
namespace QuestPDF.Examples.Engine
{
2021-08-09 22:35:39 +02:00
public enum RenderingTestResult
{
Pdf,
Images
}
2021-06-09 23:34:25 +02:00
public class RenderingTest
{
private string FileNamePrefix = "test";
private Size Size { get; set; }
2021-08-26 22:25:33 +02:00
private bool ShowResult { get; set; }
2021-08-09 22:35:39 +02:00
private RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images;
2021-06-09 23:34:25 +02:00
private RenderingTest()
{
}
public static RenderingTest Create()
{
return new RenderingTest();
}
public RenderingTest FileName([CallerMemberName] string fileName = "test")
2021-06-09 23:34:25 +02:00
{
FileNamePrefix = fileName;
return this;
}
2021-08-25 03:40:16 +02:00
public RenderingTest PageSize(Size size)
{
Size = size;
return this;
}
2021-06-09 23:34:25 +02:00
public RenderingTest PageSize(int width, int height)
{
2021-08-25 03:40:16 +02:00
return PageSize(new Size(width, height));
2021-06-09 23:34:25 +02:00
}
2021-08-09 22:35:39 +02:00
public RenderingTest ProducePdf()
{
ResultType = RenderingTestResult.Pdf;
return this;
}
public RenderingTest ProduceImages()
{
ResultType = RenderingTestResult.Images;
return this;
}
2021-08-26 22:25:33 +02:00
public RenderingTest ShowResults()
{
ShowResult = true;
return this;
}
2021-06-09 23:34:25 +02:00
public void Render(Action<IContainer> content)
{
var container = new Container();
content(container);
2021-08-26 22:25:33 +02:00
var maxPages = ResultType == RenderingTestResult.Pdf ? 1000 : 10;
var document = new SimpleDocument(container, Size, maxPages);
2021-06-09 23:34:25 +02:00
2021-08-09 22:35:39 +02:00
if (ResultType == RenderingTestResult.Images)
{
Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
document.GenerateImages(fileNameSchema);
2021-08-26 22:25:33 +02:00
if (ShowResult)
Process.Start("explorer", fileNameSchema(0));
2021-08-09 22:35:39 +02:00
}
if (ResultType == RenderingTestResult.Pdf)
{
var fileName = $"{FileNamePrefix}.pdf";
document.GeneratePdf(fileName);
2021-08-26 22:25:33 +02:00
if (ShowResult)
Process.Start("explorer", fileName);
2021-08-09 22:35:39 +02:00
}
2021-06-09 23:34:25 +02:00
}
}
}