Browse Source

Fix: `ShowIfContext.TotalPages` should be equal to `null` during the initial rendering phase

Marcin Ziąbek 7 months ago
parent
commit
30a845e9f5
1 changed files with 13 additions and 2 deletions
  1. 13 2
      Source/QuestPDF/Elements/ShowIf.cs

+ 13 - 2
Source/QuestPDF/Elements/ShowIf.cs

@@ -7,7 +7,18 @@ namespace QuestPDF.Elements;
 public sealed class ShowIfContext
 {
     public int PageNumber { get; internal set; }
-    public int TotalPages { get; internal set; }
+    
+    /// <summary>
+    /// Returns the total count of pages in the document.
+    /// </summary>
+    /// <remarks>
+    /// <para>
+    /// Document rendering process is performed in two phases.
+    /// During the first phase, the value of this property is equal to <c>null</c> to indicate its unavailability.
+    /// </para>
+    /// <para>Please note that using this property may result with unstable layouts and unpredicted behaviors, especially when generating conditional content of various sizes.</para>
+    /// </remarks>
+    public int? TotalPages { get; internal set; }
 }
 
 internal sealed class ShowIf : ContainerElement
@@ -33,7 +44,7 @@ internal sealed class ShowIf : ContainerElement
         var context = new ShowIfContext
         {
             PageNumber = PageContext.CurrentPage,
-            TotalPages = PageContext.DocumentLength
+            TotalPages = PageContext.IsInitialRenderingPhase ? null : PageContext.DocumentLength
         };
 
         return VisibilityPredicate(context);