RenderingTest.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images;
  21. private RenderingTest()
  22. {
  23. }
  24. public static RenderingTest Create()
  25. {
  26. return new RenderingTest();
  27. }
  28. public RenderingTest FileName([CallerMemberName] string fileName = "test")
  29. {
  30. FileNamePrefix = fileName;
  31. return this;
  32. }
  33. public RenderingTest PageSize(Size size)
  34. {
  35. Size = size;
  36. return this;
  37. }
  38. public RenderingTest PageSize(int width, int height)
  39. {
  40. return PageSize(new Size(width, height));
  41. }
  42. public RenderingTest ProducePdf()
  43. {
  44. ResultType = RenderingTestResult.Pdf;
  45. return this;
  46. }
  47. public RenderingTest ProduceImages()
  48. {
  49. ResultType = RenderingTestResult.Images;
  50. return this;
  51. }
  52. public void Render(Action<IContainer> content)
  53. {
  54. var container = new Container();
  55. content(container);
  56. var document = new SimpleDocument(container, Size);
  57. if (ResultType == RenderingTestResult.Images)
  58. {
  59. Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
  60. document.GenerateImages(fileNameSchema);
  61. Process.Start("explorer", fileNameSchema(0));
  62. }
  63. if (ResultType == RenderingTestResult.Pdf)
  64. {
  65. var fileName = $"{FileNamePrefix}.pdf";
  66. document.GeneratePdf(fileName);
  67. Process.Start("explorer", fileName);
  68. }
  69. }
  70. }
  71. }