Forráskód Böngészése

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 éve
szülő
commit
c7a81b2874

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

@@ -1,4 +1,5 @@
 using System;
 using System;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Elements;
 using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
 
 
@@ -16,6 +17,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer Before()
         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();
             var container = new RepeatContent();
             Decoration.Before = container;
             Decoration.Before = container;
             return container;
             return container;
@@ -40,6 +44,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer Content()
         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();
             var container = new Container();
             Decoration.Content = container;
             Decoration.Content = container;
             return container;
             return container;
@@ -64,6 +71,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer After()
         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();
             var container = new RepeatContent();
             Decoration.After = container;
             Decoration.After = container;
             return container;
             return container;

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

@@ -1,5 +1,6 @@
 using System;
 using System;
 using QuestPDF.Drawing;
 using QuestPDF.Drawing;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Elements;
 using QuestPDF.Elements;
 using QuestPDF.Helpers;
 using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
@@ -209,6 +210,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer Background()
         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();
             var container = new Container();
             Page.Background = container;
             Page.Background = container;
             return container;
             return container;
@@ -222,6 +226,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer Foreground()
         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();
             var container = new Container();
             Page.Foreground = container;
             Page.Foreground = container;
             return container;
             return container;
@@ -235,6 +242,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer Header()
         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();
             var container = new Container();
             Page.Header = container;
             Page.Header = container;
             return container;
             return container;
@@ -248,6 +258,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer Content()
         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();
             var container = new Container();
             Page.Content = container;
             Page.Content = container;
             return container;
             return container;
@@ -261,6 +274,9 @@ namespace QuestPDF.Fluent
         /// </remarks>
         /// </remarks>
         public IContainer Footer()
         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();
             var container = new Container();
             Page.Footer = container;
             Page.Footer = container;
             return 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>
         /// <param name="handler">Handler to define columns of the table.</param>
         public void ColumnsDefinition(Action<TableColumnsDefinitionDescriptor> handler)
         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();
             var descriptor = new TableColumnsDefinitionDescriptor();
             handler(descriptor);
             handler(descriptor);
 
 
@@ -105,6 +108,9 @@ namespace QuestPDF.Fluent
         /// <param name="handler">Handler for configuring the header cells.</param>
         /// <param name="handler">Handler for configuring the header cells.</param>
         public void Header(Action<TableCellDescriptor> handler)
         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);
             var descriptor = new TableCellDescriptor(HeaderTable.Cells);
             handler(descriptor);
             handler(descriptor);
         }
         }
@@ -116,6 +122,9 @@ namespace QuestPDF.Fluent
         /// </summary>
         /// </summary>
         public void Footer(Action<TableCellDescriptor> handler)
         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);
             var descriptor = new TableCellDescriptor(FooterTable.Cells);
             handler(descriptor);
             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.
 - 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 compatibility with .NET Standard 2.0.
 - Fixed an issue causing invisibility of Table elements containing only Header/Footer elements without content.
 - 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.