StandardReport.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using QuestPDF.Drawing;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. namespace QuestPDF.ReportSample.Layouts
  6. {
  7. public class StandardReport : IDocument
  8. {
  9. private ReportModel Model { get; }
  10. public StandardReport(ReportModel model)
  11. {
  12. Model = model;
  13. }
  14. public DocumentMetadata GetMetadata()
  15. {
  16. return new DocumentMetadata()
  17. {
  18. Title = Model.Title
  19. };
  20. }
  21. public void Compose(IDocumentContainer container)
  22. {
  23. container
  24. .Page(page =>
  25. {
  26. page.DefaultTextStyle(Typography.Normal);
  27. page.MarginVertical(40);
  28. page.MarginHorizontal(50);
  29. page.Size(PageSizes.A4);
  30. page.Header().Element(ComposeHeader);
  31. page.Content().Element(ComposeContent);
  32. page.Footer().AlignCenter().Text(text =>
  33. {
  34. text.CurrentPageNumber().Format(x => x?.FormatAsRomanNumeral() ?? "-----");
  35. text.Span(" / ");
  36. text.TotalPages().Format(x => x?.FormatAsRomanNumeral() ?? "-----");
  37. });
  38. });
  39. }
  40. private void ComposeHeader(IContainer container)
  41. {
  42. container.Column(column =>
  43. {
  44. column.Item().Row(row =>
  45. {
  46. row.Spacing(50);
  47. row.RelativeItem().PaddingTop(-10).Text(Model.Title).Style(Typography.Title);
  48. row.ConstantItem(90).Hyperlink("https://www.questpdf.com").MaxHeight(30).Component<ImagePlaceholder>();
  49. });
  50. column.Item().ShowOnce().PaddingVertical(15).Border(1f).BorderColor(Colors.Grey.Lighten1).ExtendHorizontal();
  51. column.Item().ShowOnce().Grid(grid =>
  52. {
  53. grid.Columns(2);
  54. grid.Spacing(5);
  55. foreach (var field in Model.HeaderFields)
  56. {
  57. grid.Item().Text(text =>
  58. {
  59. text.Span($"{field.Label}: ").SemiBold();
  60. text.Span(field.Value);
  61. });
  62. }
  63. });
  64. });
  65. }
  66. void ComposeContent(IContainer container)
  67. {
  68. container.PaddingVertical(20).Column(column =>
  69. {
  70. column.Spacing(20);
  71. column.Item().Component(new TableOfContentsTemplate(Model.Sections));
  72. column.Item().PageBreak();
  73. foreach (var section in Model.Sections)
  74. column.Item().Section(section.Title).Component(new SectionTemplate(section));
  75. column.Item().PageBreak();
  76. column.Item().Section("Photos").Column(photos =>
  77. {
  78. foreach (var photo in Model.Photos)
  79. photos.Item().Component(new PhotoTemplate(photo));
  80. });
  81. });
  82. }
  83. }
  84. }