Program.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Diagnostics;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. using QuestPDF.Previewer;
  6. Document
  7. .Create(container =>
  8. {
  9. container.Page(page =>
  10. {
  11. page.Size(PageSizes.A4);
  12. page.Margin(2, Unit.Centimetre);
  13. page.PageColor(Colors.White);
  14. page.DefaultTextStyle(x => x.FontSize(14));
  15. page.Header()
  16. .Text("Hello PDF!")
  17. .SemiBold().FontSize(36).FontColor(Colors.Blue.Medium);
  18. page.Content()
  19. .PaddingVertical(1, Unit.Centimetre)
  20. .Table(table =>
  21. {
  22. table.ColumnsDefinition(columns =>
  23. {
  24. columns.ConstantColumn(30);
  25. columns.RelativeColumn();
  26. columns.ConstantColumn(75);
  27. columns.ConstantColumn(75);
  28. columns.ConstantColumn(75);
  29. });
  30. table.Header(header =>
  31. {
  32. var textStyle = TextStyle.Default.SemiBold();
  33. header.Cell().Element(DefaultCellStyle).Text("#").Style(textStyle);
  34. header.Cell().Element(DefaultCellStyle).Text("Item name").Style(textStyle);
  35. header.Cell().Element(DefaultCellStyle).AlignRight().Text("Price").Style(textStyle);
  36. header.Cell().Element(DefaultCellStyle).AlignRight().Text("Count").Style(textStyle);
  37. header.Cell().Element(DefaultCellStyle).AlignRight().Text("Total").Style(textStyle);
  38. static IContainer DefaultCellStyle(IContainer container)
  39. {
  40. return container.PaddingBottom(5).BorderBottom(1).PaddingBottom(5);
  41. }
  42. });
  43. foreach(var i in Enumerable.Range(1, 100))
  44. {
  45. var price = Placeholders.Random.Next(100, 999) / 100f;
  46. var count = Placeholders.Random.Next(1, 10);
  47. var total = price * count;
  48. table.Cell().Element(DefaultCellStyle).Text(i);
  49. table.Cell().Element(DefaultCellStyle).Text(Placeholders.Label());
  50. table.Cell().Element(DefaultCellStyle).AlignRight().Text($"{price:N2} $");
  51. table.Cell().Element(DefaultCellStyle).AlignRight().Text(count);
  52. table.Cell().Element(DefaultCellStyle).AlignRight().Text($"{total:N2} $");
  53. static IContainer DefaultCellStyle(IContainer container)
  54. {
  55. return container.PaddingBottom(5).BorderBottom(1).BorderColor(Colors.Grey.Lighten1).PaddingBottom(5);
  56. }
  57. }
  58. });
  59. page.Footer()
  60. .AlignCenter()
  61. .Text(x =>
  62. {
  63. x.Span("Page ");
  64. x.CurrentPageNumber();
  65. x.Span(" of ");
  66. x.TotalPages();
  67. });
  68. });
  69. })
  70. .ShowInPreviewer();