CodePatternElementPositionLocatorExample.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using QuestPDF.Elements;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. namespace QuestPDF.DocumentationExamples.CodePatterns;
  6. public class CodePatternElementPositionLocatorExample
  7. {
  8. [Test]
  9. public void Example()
  10. {
  11. var content = GenerateReport();
  12. File.WriteAllBytes("code-pattern-element-position-locator.pdf", content);
  13. }
  14. public byte[] GenerateReport()
  15. {
  16. return Document
  17. .Create(document =>
  18. {
  19. document.Page(page =>
  20. {
  21. page.Size(PageSizes.A6);
  22. page.Margin(25);
  23. page.DefaultTextStyle(x => x.FontSize(20));
  24. page.Content()
  25. .Background(Colors.White)
  26. .Row(row =>
  27. {
  28. row.Spacing(10);
  29. row.ConstantItem(5).Dynamic(new DynamicTextSpanPositionCapture());
  30. row.RelativeItem().CapturePosition("container").Text(text =>
  31. {
  32. text.Justify();
  33. text.DefaultTextStyle(x => x.FontSize(18));
  34. text.Span("Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
  35. text.Element(TextInjectedElementAlignment.Top).CapturePosition("span_start");
  36. text.Span("Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.").BackgroundColor(Colors.Red.Lighten4);
  37. text.Element(TextInjectedElementAlignment.Bottom).CapturePosition("span_end");
  38. text.Span(" It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
  39. });
  40. });
  41. page.Footer().Text(text =>
  42. {
  43. text.Span("Page ");
  44. text.CurrentPageNumber();
  45. text.Span(" of ");
  46. text.TotalPages();
  47. });
  48. });
  49. })
  50. .GeneratePdf();
  51. }
  52. public class DynamicTextSpanPositionCapture : IDynamicComponent
  53. {
  54. public DynamicComponentComposeResult Compose(DynamicContext context)
  55. {
  56. var containerLocation = context.GetElementCapturedPositions("container").FirstOrDefault(x => x.PageNumber == context.PageNumber);
  57. var spanStartLocation = context.GetElementCapturedPositions("span_start").FirstOrDefault();
  58. var spanEndLocation = context.GetElementCapturedPositions("span_end").FirstOrDefault();
  59. if (containerLocation == null || spanStartLocation == null || spanEndLocation == null || containerLocation.PageNumber > spanStartLocation.PageNumber || containerLocation.PageNumber < spanEndLocation.PageNumber)
  60. {
  61. return new DynamicComponentComposeResult
  62. {
  63. Content = context.CreateElement(container => { }),
  64. HasMoreContent = false
  65. };
  66. }
  67. var positionStart = containerLocation.PageNumber > spanStartLocation.PageNumber ? containerLocation.Y : spanStartLocation.Y;
  68. var positionEnd = containerLocation.PageNumber < spanEndLocation.PageNumber ? (containerLocation.Y + containerLocation.Height) : (spanEndLocation.Y + spanEndLocation.Height);
  69. var content = context.CreateElement(container =>
  70. {
  71. container
  72. .TranslateX(0)
  73. .TranslateY(positionStart - containerLocation.Y)
  74. .Width(5)
  75. .Height(positionEnd - positionStart)
  76. .Background(Colors.Red.Medium);
  77. });
  78. return new DynamicComponentComposeResult
  79. {
  80. Content = content,
  81. HasMoreContent = false
  82. };
  83. }
  84. }
  85. }