RenderingTest.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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(int width, int height)
  34. {
  35. Size = new Size(width, height);
  36. return this;
  37. }
  38. public RenderingTest ProducePdf()
  39. {
  40. ResultType = RenderingTestResult.Pdf;
  41. return this;
  42. }
  43. public RenderingTest ProduceImages()
  44. {
  45. ResultType = RenderingTestResult.Images;
  46. return this;
  47. }
  48. public void Render(Action<IContainer> content)
  49. {
  50. var container = new Container();
  51. content(container);
  52. var document = new SimpleDocument(container, Size);
  53. if (ResultType == RenderingTestResult.Images)
  54. {
  55. Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
  56. document.GenerateImages(fileNameSchema);
  57. Process.Start("explorer", fileNameSchema(0));
  58. }
  59. if (ResultType == RenderingTestResult.Pdf)
  60. {
  61. var fileName = $"{FileNamePrefix}.pdf";
  62. document.GeneratePdf(fileName);
  63. Process.Start("explorer", fileName);
  64. }
  65. }
  66. }
  67. }