瀏覽代碼

Improved the Document Composition Fluent API for Page, Table and Decoration elements. An exception is now thrown if any layer is configured more than once, preventing unexpected behavior.

Marcin Ziąbek 1 年之前
父節點
當前提交
c7a81b2874

+ 10 - 0
Source/QuestPDF/Fluent/DecorationExtensions.cs

@@ -1,4 +1,5 @@
 using System;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
 
@@ -16,6 +17,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer Before()
         {
+            if (Decoration.Before != Empty.Instance)
+                throw new DocumentComposeException("The 'Decoration.Before' layer has already been defined. Please call this method only once.");
+
             var container = new RepeatContent();
             Decoration.Before = container;
             return container;
@@ -40,6 +44,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer Content()
         {
+            if (Decoration.Content != Empty.Instance)
+                throw new DocumentComposeException("The 'Decoration.Content' layer has already been defined. Please call this method only once.");
+            
             var container = new Container();
             Decoration.Content = container;
             return container;
@@ -64,6 +71,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer After()
         {
+            if (Decoration.After != Empty.Instance)
+                throw new DocumentComposeException("The 'Decoration.After' layer has already been defined. Please call this method only once.");
+            
             var container = new RepeatContent();
             Decoration.After = container;
             return container;

+ 16 - 0
Source/QuestPDF/Fluent/PageExtensions.cs

@@ -1,5 +1,6 @@
 using System;
 using QuestPDF.Drawing;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Elements;
 using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
@@ -209,6 +210,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer Background()
         {
+            if (Page.Background != Empty.Instance)
+                throw new DocumentComposeException("The 'Page.Background' layer has already been defined. Please call this method only once.");
+            
             var container = new Container();
             Page.Background = container;
             return container;
@@ -222,6 +226,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer Foreground()
         {
+            if (Page.Foreground != Empty.Instance)
+                throw new DocumentComposeException("The 'Page.Foreground' layer has already been defined. Please call this method only once.");
+            
             var container = new Container();
             Page.Foreground = container;
             return container;
@@ -235,6 +242,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer Header()
         {
+            if (Page.Header != Empty.Instance)
+                throw new DocumentComposeException("The 'Page.Header' layer has already been defined. Please call this method only once.");
+            
             var container = new Container();
             Page.Header = container;
             return container;
@@ -248,6 +258,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer Content()
         {
+            if (Page.Content != Empty.Instance)
+                throw new DocumentComposeException("The 'Page.Content' layer has already been defined. Please call this method only once.");
+            
             var container = new Container();
             Page.Content = container;
             return container;
@@ -261,6 +274,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         public IContainer Footer()
         {
+            if (Page.Footer != Empty.Instance)
+                throw new DocumentComposeException("The 'Page.Footer' layer has already been defined. Please call this method only once.");
+            
             var container = new Container();
             Page.Footer = container;
             return container;

+ 9 - 0
Source/QuestPDF/Fluent/TableExtensions.cs

@@ -78,6 +78,9 @@ namespace QuestPDF.Fluent
         /// <param name="handler">Handler to define columns of the table.</param>
         public void ColumnsDefinition(Action<TableColumnsDefinitionDescriptor> handler)
         {
+            if (ContentTable.Columns.Any())
+                throw new DocumentComposeException("Table columns have already been defined. Please call the 'Table.ColumnsDefinition' method only once.");
+            
             var descriptor = new TableColumnsDefinitionDescriptor();
             handler(descriptor);
 
@@ -105,6 +108,9 @@ namespace QuestPDF.Fluent
         /// <param name="handler">Handler for configuring the header cells.</param>
         public void Header(Action<TableCellDescriptor> handler)
         {
+            if (HeaderTable.Cells.Any())
+                throw new DocumentComposeException("The 'Table.Header' layer has already been defined. Please call this method only once.");
+            
             var descriptor = new TableCellDescriptor(HeaderTable.Cells);
             handler(descriptor);
         }
@@ -116,6 +122,9 @@ namespace QuestPDF.Fluent
         /// </summary>
         public void Footer(Action<TableCellDescriptor> handler)
         {
+            if (FooterTable.Cells.Any())
+                throw new DocumentComposeException("The 'Table.Footer' layer has already been defined. Please call this method only once.");
+            
             var descriptor = new TableCellDescriptor(FooterTable.Cells);
             handler(descriptor);
         }

+ 1 - 0
Source/QuestPDF/Resources/ReleaseNotes.txt

@@ -27,3 +27,4 @@ Version 2024.6.1
 - Text element: improved the default sizing behavior for the Text element. Now, when the text is empty, it works more consistently by taking up zero width while still reserving vertical space based on the font size.
 - Fixed compatibility with .NET Standard 2.0.
 - Fixed an issue causing invisibility of Table elements containing only Header/Footer elements without content.
+- Improved the Document Composition Fluent API for Page, Table and Decoration elements. An exception is now thrown if any layer is configured more than once, preventing unexpected behavior.