RenderingTest.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using QuestPDF.Drawing;
  5. using QuestPDF.Elements;
  6. using QuestPDF.Fluent;
  7. using QuestPDF.Helpers;
  8. using QuestPDF.Infrastructure;
  9. namespace QuestPDF.Examples.Engine
  10. {
  11. public enum RenderingTestResult
  12. {
  13. Pdf,
  14. Images
  15. }
  16. public class RenderingTest
  17. {
  18. private string FileNamePrefix = "test";
  19. private Size Size { get; set; }
  20. private bool ShowResult { get; set; }
  21. private RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images;
  22. private RenderingTest()
  23. {
  24. }
  25. public static RenderingTest Create()
  26. {
  27. return new RenderingTest();
  28. }
  29. public RenderingTest FileName([CallerMemberName] string fileName = "test")
  30. {
  31. FileNamePrefix = fileName;
  32. return this;
  33. }
  34. public RenderingTest PageSize(Size size)
  35. {
  36. Size = size;
  37. return this;
  38. }
  39. public RenderingTest PageSize(int width, int height)
  40. {
  41. return PageSize(new Size(width, height));
  42. }
  43. public RenderingTest ProducePdf()
  44. {
  45. ResultType = RenderingTestResult.Pdf;
  46. return this;
  47. }
  48. public RenderingTest ProduceImages()
  49. {
  50. ResultType = RenderingTestResult.Images;
  51. return this;
  52. }
  53. public RenderingTest ShowResults()
  54. {
  55. ShowResult = true;
  56. return this;
  57. }
  58. public void Render(Action<IContainer> content)
  59. {
  60. var container = new Container();
  61. content(container);
  62. var maxPages = ResultType == RenderingTestResult.Pdf ? 1000 : 10;
  63. var document = new SimpleDocument(container, Size, maxPages);
  64. if (ResultType == RenderingTestResult.Images)
  65. {
  66. Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
  67. document.GenerateImages(fileNameSchema);
  68. if (ShowResult)
  69. Process.Start("explorer", fileNameSchema(0));
  70. }
  71. if (ResultType == RenderingTestResult.Pdf)
  72. {
  73. var fileName = $"{FileNamePrefix}.pdf";
  74. document.GeneratePdf(fileName);
  75. if (ShowResult)
  76. Process.Start("explorer", fileName);
  77. }
  78. }
  79. }
  80. }