Conditional Expressions in Word Templates

Master the power of conditional logic in PDF4me Word templates to create dynamic, intelligent documents that adapt based on your data.

How Conditional Expressions Work

The Generate Documents action in PDF4me lets you use conditional expressions to populate data into templates. Based on the status of a condition, dynamic data can be added to a document. You can even combine expressions to satisfy multiple conditions before a particular data is filled.

Basic If-Else Condition Syntax

A fundamental syntax for implementing if-else conditions in your Word templates:

<<if [condition1]>>
  Add first data
  <<elseif [condition2]>>
    Add second data
  <<else>>
    Add third data
<</if>>

Practical Example: Invoice Payment Status

Let’s examine a real-world example to better understand how conditional expressions work in practice:

We can configure a template with a condition to see if an Invoice is paid or not. If it is paid, a particular data is filled, else an alternate data. If both the above conditions are not satisfied a third data can be filled.

Let us look at the below condition -

<<if [invoicePaid == false]>>
The invoice has not been paid.
<<else>>
The invoice has been paid.
<</if>>

Prepare the data for the above condition in the following way

{
  "VendorName": "Ynoox GmbH",
  "InvoicePaid": false,
  "InvoiceDue": "15-09-2022",
  "InvoiceAmount": 1200,
  "InvoiceDate": "01-09-2022"
}

Advanced Conditional Structure with Multiple Conditions

Now, let’s explore a more sophisticated conditional structure that validates data and populates results with dynamic dates. This example demonstrates how to combine multiple conditions for complex business logic:

<<if [invoicePaid == false && paymentDue < generatedOn]>>
  The invoice is unpaid and overdue.
<<elseif [invoicePaid == false && paymentDue > generatedOn]>>
  The invoice is not paid and due on <<[invoiceDue]:"dd.MM.yyyy" >>
<<else>>
  The invoice is paid.
<</if>>

The data for testing the above condition can be created in the following JSON format -

{
  "companyName": "Ynoox GmbH",
  "invoicePaid": false,
  "paymentDue": "15-09-2022",
  "generatedOn": "15-08-2022",
  "invoiceValue": "$1200"
}

Summary: Mastering Conditional Expressions

You’ve now learned the fundamentals of conditional expressions in PDF4me Word templates:

  • Basic if-else syntax for simple conditional logic
  • Practical examples with real-world scenarios
  • Advanced structures combining multiple conditions
  • Dynamic date formatting and complex business logic

Next Steps

  1. Practice with your own data - Try creating templates with your specific use cases
  2. Explore more features - Learn about loops, variables, and other template features
  3. Join the community - Get help and share your templates with other users

Ready to create dynamic documents? Start building your first conditional template today!