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 DocumentSettings GetSettings() => DocumentSettings.Default;
  22. public void Compose(IDocumentContainer container)
  23. {
  24. container
  25. .Page(page =>
  26. {
  27. page.DefaultTextStyle(Typography.Normal);
  28. page.MarginVertical(40);
  29. page.MarginHorizontal(50);
  30. page.Size(PageSizes.A4);
  31. page.Header().Element(ComposeHeader);
  32. page.Content().Element(ComposeContent);
  33. page.Footer().AlignCenter().Text(text =>
  34. {
  35. text.CurrentPageNumber().Format(x => x?.FormatAsRomanNumeral() ?? "-----");
  36. text.Span(" / ");
  37. text.TotalPages().Format(x => x?.FormatAsRomanNumeral() ?? "-----");
  38. });
  39. });
  40. }
  41. private void ComposeHeader(IContainer container)
  42. {
  43. container.Column(column =>
  44. {
  45. column.Item().Row(row =>
  46. {
  47. row.Spacing(50);
  48. row.RelativeItem().PaddingTop(-10).Text(Model.Title).Style(Typography.Title);
  49. row.ConstantItem(90).Hyperlink("https://www.questpdf.com").MaxHeight(30).Component<ImagePlaceholder>();
  50. });
  51. column.Item().ShowOnce().PaddingVertical(15).Border(1f).BorderColor(Colors.Grey.Lighten1).ExtendHorizontal();
  52. column.Item().ShowOnce().Grid(grid =>
  53. {
  54. grid.Columns(2);
  55. grid.Spacing(5);
  56. foreach (var field in Model.HeaderFields)
  57. {
  58. grid.Item().Text(text =>
  59. {
  60. text.Span($"{field.Label}: ").SemiBold();
  61. text.Span(field.Value);
  62. });
  63. }
  64. });
  65. });
  66. }
  67. void ComposeContent(IContainer container)
  68. {
  69. container.PaddingVertical(20).Column(column =>
  70. {
  71. column.Spacing(20);
  72. column.Item().Component(new TableOfContentsTemplate(Model.Sections));
  73. column.Item().PageBreak();
  74. foreach (var section in Model.Sections)
  75. column.Item().Section(section.Title).Component(new SectionTemplate(section));
  76. column.Item().PageBreak();
  77. column.Item().Section("Photos");
  78. foreach (var photo in Model.Photos)
  79. column.Item().Component(new PhotoTemplate(photo));
  80. });
  81. }
  82. }
  83. }