Conditional blocks allow you to display different content in your documents based on data values. GroupDocs.Assembly for .NET supports if/elseif/else syntax similar to C#, enabling you to create dynamic documents that adapt their content based on conditions.
The main syntax elements are:
<<if [condition]>> - display content if condition is true
<<elseif [condition]>> - display content if previous conditions are false and this condition is true
<<else>> - display content if all conditions are false
<</if>> - closing tag
Here are the steps to use conditional content:
Create a template with conditional blocks
Define conditions using C# expressions that return boolean values
Provide content for each condition branch
Assemble the document with your data
Note
Conditional expressions must return boolean values. You can use comparison operators (==, !=, <, >, <=, >=), logical operators (&&, ||, !), and method calls that return bool.
Note
Boolean values of JSON, XML and CSV data, and bool? members of your own classes, are nullable because the value may be missing. They can be used directly as conditions: in <<if [o.Paid]>>, in the ?:, && and || operators, and in predicates of Where, Count, Any and other extension methods, such as <<foreach [o in orders.Where(x => x.Paid)]>>. A missing value counts as false, so o.Paid == true is not needed.
Customer: <<[customer.Name]>>
<<if [customer.Balance > 1000]>>
Status: Premium Customer
You have a high balance and qualify for special benefits.
<<else>>
Status: Standard Customer
Thank you for your business.
<</if>>
Order Amount: <<[order.Amount]:"C">>
<<if [order.Amount > 1000]>>
Discount: 10% (Large order discount)
<<elseif [order.Amount > 500]>>
Discount: 5% (Medium order discount)
<<else>>
Discount: No discount available
<</if>>
Conditional Content with Collections
Check if collections have items:
usingGroupDocs.Assembly;usingSystem.Collections.Generic;publicclassReport{publicList<string>Items{get;set;}}publicstaticvoidConditionalWithCollections(){varreport=newReport{Items=newList<string>{"Item 1","Item 2","Item 3"}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ConditionalTemplate.docx","ConditionalOutput.docx",newDataSourceInfo(report,"report"));Console.WriteLine("Conditional content with collections generated successfully.");}
Template:
<<if [report.Items.Any()]>>
Items:
<<foreach [item in report.Items]>>- <<[item]>>
<</foreach>><<else>>
No items available.
<</if>>
The items are strings, so the data band names its iteration variable (item) and outputs it with <<[item]>>. The band opens in the item’s paragraph and closes at the start of the next one, so it repeats one paragraph per item; a <<foreach>> tag on a line of its own would repeat an empty paragraph too.
Conditional Content in Data Bands
Use conditional blocks within foreach loops:
usingGroupDocs.Assembly;usingSystem.Collections.Generic;publicclassProduct{publicstringName{get;set;}publicdecimalPrice{get;set;}publicboolInStock{get;set;}}publicstaticvoidConditionalInDataBand(){List<Product>products=newList<Product>{newProduct{Name="Laptop",Price=999.99m,InStock=true},newProduct{Name="Mouse",Price=29.99m,InStock=false},newProduct{Name="Keyboard",Price=79.99m,InStock=true}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ConditionalTemplate.docx","ConditionalOutput.docx",newDataSourceInfo(products,"products"));Console.WriteLine("Conditional content in data band generated successfully.");}
Template:
<<foreach [in products]>><<[Name]>> - <<[Price]:"C">>
<<if [InStock]>>✓ In Stock<<else>>✗ Out of Stock<</if>>
<</foreach>>
Each product gives three paragraphs: its name and price, its stock status, and an empty line.
usingGroupDocs.Assembly;usingSystem.Collections.Generic;publicclassOrder{publicList<OrderItem>Items{get;set;}publicboolHasItems(){returnItems!=null&&Items.Count>0;}}publicclassOrderItem{publicstringName{get;set;}}publicstaticvoidConditionalWithMethods(){varorder=newOrder{Items=newList<OrderItem>{newOrderItem{Name="Product"}}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ConditionalTemplate.docx","ConditionalOutput.docx",newDataSourceInfo(order,"order"));Console.WriteLine("Conditional with methods processed successfully.");}
Template:
<<if [order.HasItems()]>>
Order contains items.
<<else>>
Order is empty.
<</if>>
Warning
Conditional blocks must be properly closed with <</if>>. Nested conditional blocks are supported, but ensure proper nesting and closing of all blocks. A block closed with <<if>> instead fails with “Tag ‘if’ is not well-formed. Tag end is unexpected.”
Advanced Usage Topics
To learn more about table-row conditional blocks, conditional formatting in different document types, and advanced conditional scenarios, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: