Browse Source

Documentation example: CapturePosition

Marcin Ziąbek 7 months ago
parent
commit
edae494565

+ 48 - 46
Source/QuestPDF.DocumentationExamples/CodePatterns/CodePatternElementPositionLocatorExample.cs

@@ -10,86 +10,88 @@ public class CodePatternElementPositionLocatorExample
     [Test]
     [Test]
     public void Example()
     public void Example()
     {
     {
-        var content = GenerateReport();
-        File.WriteAllBytes("code-pattern-element-position-locator.pdf", content);
-    }
-    
-    public byte[] GenerateReport()
-    {
-        return Document
+        Document
             .Create(document =>
             .Create(document =>
             {
             {
                 document.Page(page =>
                 document.Page(page =>
                 {
                 {
-                    page.Size(PageSizes.A6);
-                    page.Margin(25);
+                    page.ContinuousSize(575);
                     page.DefaultTextStyle(x => x.FontSize(20));
                     page.DefaultTextStyle(x => x.FontSize(20));
-                    
+                    page.Margin(25);
+
                     page.Content()
                     page.Content()
                         .Background(Colors.White)
                         .Background(Colors.White)
                         .Row(row =>
                         .Row(row =>
                         {
                         {
-                            row.Spacing(10);
+                            row.Spacing(25);
+
+                            row.ConstantItem(0).Dynamic(new DynamicTextSpanPositionCapture());
 
 
-                            row.ConstantItem(5).Dynamic(new DynamicTextSpanPositionCapture());
- 
                             row.RelativeItem().CapturePosition("container").Text(text =>
                             row.RelativeItem().CapturePosition("container").Text(text =>
                             {
                             {
-               
                                 text.Justify();
                                 text.Justify();
-                                text.DefaultTextStyle(x => x.FontSize(18));
+                                
+                                var mistakeTextStyle = TextStyle.Default
+                                    .FontColor(Colors.Red.Darken3)
+                                    .BackgroundColor(Colors.Red.Lighten4)
+                                    .Strikethrough()
+                                    .DecorationThickness(2);
+                                
+                                var correctionTextStyle = TextStyle.Default
+                                    .FontColor(Colors.Green.Darken3)
+                                    .BackgroundColor(Colors.Green.Lighten4);
 
 
-                                text.Span("Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
-                                text.Element(TextInjectedElementAlignment.Top).CapturePosition("span_start");
-                                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);
-                                text.Element(TextInjectedElementAlignment.Bottom).CapturePosition("span_end");
-                                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.");
+                                text.Span("Proofreading").Bold().Underline().DecorationThickness(2);
+                                text.Span(" technical documentation is a critical quality assurance step that ensures clarity, accuracy, and consistency across all written content. It involves more than just checking for grammar and ");
+                                text.Span("spilling").Style(mistakeTextStyle);
+                                text.Span("spelling").Style(correctionTextStyle);
+                                text.Element(TextInjectedElementAlignment.Middle).CapturePosition("mistake");
+                                text.Span(" errors—it also includes verifying terminology, code syntax, formatting standards, and logical flow. A common best practice is to have the content reviewed by both a subject matter ");
+                                text.Span("export").Style(mistakeTextStyle);
+                                text.Span("expert").Style(correctionTextStyle);
+                                text.Element(TextInjectedElementAlignment.Middle).CapturePosition("mistake");
+                                text.Span(" and a language specialist, ensuring that the material is technically sound while also being accessible to the intended audience.");
                             });
                             });
                         });
                         });
-
-                    page.Footer().Text(text =>
-                    {
-                        text.Span("Page ");
-                        text.CurrentPageNumber();
-                        text.Span(" of ");
-                        text.TotalPages();
-                    });
                 });
                 });
             })
             })
-            .GeneratePdf();
+            .GenerateImages(x => "code-pattern-element-position-locator.webp", new ImageGenerationSettings() { ImageFormat = ImageFormat.Webp, ImageCompressionQuality = ImageCompressionQuality.VeryHigh, RasterDpi = 144 });
     }
     }
-    
 
 
     public class DynamicTextSpanPositionCapture : IDynamicComponent
     public class DynamicTextSpanPositionCapture : IDynamicComponent
     {
     {
         public DynamicComponentComposeResult Compose(DynamicContext context)
         public DynamicComponentComposeResult Compose(DynamicContext context)
         {
         {
             var containerLocation = context.GetElementCapturedPositions("container").FirstOrDefault(x => x.PageNumber == context.PageNumber);
             var containerLocation = context.GetElementCapturedPositions("container").FirstOrDefault(x => x.PageNumber == context.PageNumber);
-
-            var spanStartLocation = context.GetElementCapturedPositions("span_start").FirstOrDefault();
-
-            var spanEndLocation = context.GetElementCapturedPositions("span_end").FirstOrDefault();
-
-            if (containerLocation == null || spanStartLocation == null || spanEndLocation == null || containerLocation.PageNumber > spanStartLocation.PageNumber || containerLocation.PageNumber < spanEndLocation.PageNumber)
+            var mistakeLocations = context.GetElementCapturedPositions("mistake").Where(x => x.PageNumber == context.PageNumber).ToList();
+            
+            if (containerLocation == null || mistakeLocations.Count == 0)
             {
             {
                 return new DynamicComponentComposeResult
                 return new DynamicComponentComposeResult
                 {
                 {
-                    Content = context.CreateElement(container => { }),
+                    Content = context.CreateElement(_ => { }),
                     HasMoreContent = false
                     HasMoreContent = false
                 };
                 };
             }
             }
-            
-            var positionStart = containerLocation.PageNumber > spanStartLocation.PageNumber ? containerLocation.Y : spanStartLocation.Y;
-            var positionEnd = containerLocation.PageNumber < spanEndLocation.PageNumber ? (containerLocation.Y + containerLocation.Height) : (spanEndLocation.Y + spanEndLocation.Height);
 
 
             var content = context.CreateElement(container =>
             var content = context.CreateElement(container =>
             {
             {
-                container
-                    .TranslateX(0)
-                    .TranslateY(positionStart - containerLocation.Y)
-                    .Width(5)
-                    .Height(positionEnd - positionStart)
-                    .Background(Colors.Red.Medium);
+                container.Layers(layers =>
+                {
+                    layers.PrimaryLayer();
+
+                    foreach (var mistakeLocation in mistakeLocations)
+                    {
+                        layers
+                            .Layer()
+                            .Unconstrained() 
+                            .TranslateY(mistakeLocation.Y - containerLocation.Y)
+                            .TranslateX(-12)
+                            .TranslateY(-12)
+                            .Width(24)
+                            .Svg("Resources/proofreading.svg");
+                    }
+                });
             });
             });
 
 
             return new DynamicComponentComposeResult
             return new DynamicComponentComposeResult

+ 1 - 0
Source/QuestPDF.DocumentationExamples/Resources/proofreading.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.75 20.85C11.53 20.15 11.14 18.22 10.24 17C9.35 15.75 8.12 14.89 6.88 14.06C6 13.5 5.19 12.8 4.54 12C4.26 11.67 3.69 11.06 4.27 10.94C4.86 10.82 5.88 11.4 6.4 11.62C7.31 12 8.21 12.44 9.05 12.96L10.06 11.26C8.5 10.23 6.5 9.32 4.64 9.05C3.58 8.89 2.46 9.11 2.1 10.26C1.78 11.25 2.29 12.25 2.87 13.03C4.24 14.86 6.37 15.74 7.96 17.32C8.3 17.65 8.71 18.04 8.91 18.5C9.12 18.94 9.07 18.97 8.6 18.97C7.36 18.97 5.81 18 4.8 17.36L3.79 19.06C5.32 20 7.88 21.47 9.75 20.85M18.96 7.33L13.29 13H11V10.71L16.67 5.03L18.96 7.33M22.36 6.55C22.35 6.85 22.04 7.16 21.72 7.47L19.2 10L18.33 9.13L20.93 6.54L20.34 5.95L19.67 6.62L17.38 4.33L19.53 2.18C19.77 1.94 20.16 1.94 20.39 2.18L21.82 3.61C22.06 3.83 22.06 4.23 21.82 4.47C21.61 4.68 21.41 4.88 21.41 5.08C21.39 5.28 21.59 5.5 21.79 5.67C22.08 5.97 22.37 6.25 22.36 6.55Z" /></svg>