DynamicTextSpanPositionCapture.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Globalization;
  5. using System.Linq;
  6. using NUnit.Framework;
  7. using QuestPDF.Elements;
  8. using QuestPDF.Examples.Engine;
  9. using QuestPDF.Fluent;
  10. using QuestPDF.Helpers;
  11. using QuestPDF.Infrastructure;
  12. namespace QuestPDF.Examples
  13. {
  14. public class DynamicTextSpanPositionCapture : IDynamicComponent
  15. {
  16. public DynamicComponentComposeResult Compose(DynamicContext context)
  17. {
  18. var containerLocation = context.GetElementCapturedLocations("container").FirstOrDefault();
  19. var spanStartLocation = context.GetElementCapturedLocations("span_start").FirstOrDefault();
  20. var spanEndLocation = context.GetElementCapturedLocations("span_end").FirstOrDefault();
  21. if (containerLocation == null || spanStartLocation == null || spanEndLocation == null)
  22. {
  23. return new DynamicComponentComposeResult
  24. {
  25. Content = context.CreateElement(container => { }),
  26. HasMoreContent = false
  27. };
  28. }
  29. var content = context.CreateElement(container =>
  30. {
  31. container
  32. .TranslateX(0)
  33. .TranslateY(spanStartLocation.Y - containerLocation.Y)
  34. .Width(5)
  35. .Height(spanEndLocation.Y - spanStartLocation.Y)
  36. .Background(Colors.Red.Medium);
  37. });
  38. return new DynamicComponentComposeResult
  39. {
  40. Content = content,
  41. HasMoreContent = false
  42. };
  43. }
  44. }
  45. public static class DynamicTextSpanPositionCaptureExample
  46. {
  47. [Test]
  48. public static void Dynamic()
  49. {
  50. RenderingTest
  51. .Create()
  52. .PageSize(PageSizes.A4)
  53. .ShowResults()
  54. .ProducePdf()
  55. .Render(container =>
  56. {
  57. container
  58. .Background(Colors.White)
  59. .Padding(25)
  60. .Row(row =>
  61. {
  62. row.Spacing(10);
  63. row.ConstantItem(5).Dynamic(new DynamicTextSpanPositionCapture());
  64. row.RelativeItem().CaptureLocation("container").Text(text =>
  65. {
  66. text.Justify();
  67. text.DefaultTextStyle(x => x.FontSize(18));
  68. text.Span("Lorem Ipsum is simply dummy text of the printing and typesetting industry. ");
  69. text.Element(TextInjectedElementAlignment.Top).CaptureLocation("span_start");
  70. 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);
  71. text.Element(TextInjectedElementAlignment.Bottom).CaptureLocation("span_end");
  72. 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.");
  73. });
  74. });
  75. });
  76. }
  77. }
  78. }