Formatting in Word Templates - Styling Guide
During document generation, you may need to apply specific formatting to dynamically populated content, such as formatting dates, transforming text case, or applying number formats. PDF4me provides powerful formatting capabilities to control how data appears in generated documents.
Basic Formatting Syntax
The fundamental syntax for applying formatting to merge fields:
<<[token]:"format">>
The format specification is placed after the field name, separated by a colon and enclosed in quotes.
String Formatting
Transform and format text content using the following string formatting options:
Text Case Transformation
Upper Case - Converts entire string to uppercase
Syntax: <<[token]:upper>>
Example: <<[name]:upper>> → "JOHN SMITH"
Capitalize Words - Capitalizes first letter of every word
Syntax: <<[token]:caps>>
Example: <<[companyName]:caps>> → "Pdf4me Solutions"
Capitalize First Letter - Capitalizes only the first letter of the string
Syntax: <<[token]:firstCap>>
Example: <<[sentence]:firstCap>> → "This is a sentence."
String Formatting Examples
Template:
Customer Name: <<[customerName]:upper>>
Product Title: <<[productName]:caps>>
Description: <<[description]:firstCap>>
Data:
{
"customerName": "john smith",
"productName": "premium software package",
"description": "this is a premium product."
}
Output:
Customer Name: JOHN SMITH
Product Title: Premium Software Package
Description: This is a premium product.
Date and Time Formatting
Format date and time values using custom patterns to display dates in your preferred format.
Common Date Format Patterns
Full Date with Time
Syntax: <<[token]:"dd MMM yy H:mm:ss">>
Example: <<[date]:"dd MMM yy H:mm:ss">> → "15 Dec 12 10:10:21"
Standard Date Format
Syntax: <<[token]:"dd-MM-yyyy">>
Example: <<[invoiceDate]:"dd-MM-yyyy">> → "15-12-2022"
European Date Format
Syntax: <<[token]:"dd.MM.yyyy">>
Example: <<[invoiceDate]:"dd.MM.yyyy">> → "15.12.2022"
US Date Format
Syntax: <<[token]:"MM/dd/yyyy">>
Example: <<[date]:"MM/dd/yyyy">> → "12/15/2022"
ISO Date Format
Syntax: <<[token]:"yyyy-MM-dd">>
Example: <<[date]:"yyyy-MM-dd">> → "2022-12-15"
Long Date Format
Syntax: <<[token]:"MMMM dd, yyyy">>
Example: <<[date]:"MMMM dd, yyyy">> → "December 15, 2022"
Short Date with Time
Syntax: <<[token]:"dd/MM/yyyy HH:mm">>
Example: <<[timestamp]:"dd/MM/yyyy HH:mm">> → "15/12/2022 14:30"
Date Formatting Example
Template:
Invoice Date: <<[invoiceDate]:"dd.MM.yyyy">>
Due Date: <<[dueDate]:"MMMM dd, yyyy">>
Timestamp: <<[createdAt]:"dd MMM yy H:mm:ss">>
Data:
{
"invoiceDate": "2022/09/12",
"dueDate": "2022/10/12",
"createdAt": "2022/09/12 10:30:45"
}
Output:
Invoice Date: 12.09.2022
Due Date: October 12, 2022
Timestamp: 12 Sep 22 10:30:45
Date Format Components
Understanding date format components helps you create custom date formats:
| Component | Description | Example |
|---|---|---|
d | Day (1-31) | 5, 15, 28 |
dd | Day (01-31) | 05, 15, 28 |
M | Month (1-12) | 3, 9, 12 |
MM | Month (01-12) | 03, 09, 12 |
MMM | Month abbreviation | Jan, Sep, Dec |
MMMM | Month full name | January, September, December |
yy | Year (2 digits) | 22, 23 |
yyyy | Year (4 digits) | 2022, 2023 |
H | Hour (0-23) | 0, 14, 23 |
HH | Hour (00-23) | 00, 14, 23 |
h | Hour (1-12) | 1, 6, 12 |
hh | Hour (01-12) | 01, 06, 12 |
m | Minute (0-59) | 5, 30, 59 |
mm | Minute (00-59) | 05, 30, 59 |
s | Second (0-59) | 5, 30, 59 |
ss | Second (00-59) | 05, 30, 59 |
tt | AM/PM | AM, PM |
Number Formatting
While not explicitly shown in the basic examples, number formatting follows similar patterns:
Currency Format:
Amount: <<[amount]:"C2">>
Decimal Places:
Value: <<[value]:"F2">>
Percentage:
Rate: <<[rate]:"P1">>
Combining Formats with Expressions
Formatting can be combined with enumeration methods and expressions:
Example:
Average Age: <<[employees.Average(p => p.Age)]:"F1">> years
Total Revenue: <<[orders.Sum(o => o.Total)]:"C2">>
Last Update: <<[updateDate]:"MMMM dd, yyyy 'at' HH:mm">>
Practical Use Cases
Invoice Example
Template:
Invoice #: <<[invoiceNumber]:upper>>
Date: <<[invoiceDate]:"dd.MM.yyyy">>
Company: <<[companyName]:caps>>
Total Amount: $<<[totalAmount]:"F2">>
Report Header Example
Template:
Report Generated: <<[generatedDate]:"MMMM dd, yyyy 'at' HH:mm:ss">>
Generated By: <<[userName]:caps>>
Period: <<[startDate]:"MMM yyyy">> - <<[endDate]:"MMM yyyy">>
Best Practices
- Consistent date formats - Use the same date format throughout a document
- Locale considerations - Choose date formats appropriate for your audience
- Test formatting - Verify formats with actual data before production
- String transformations - Apply string formatting thoughtfully to maintain readability
- Number precision - Use appropriate decimal places for monetary and numeric values
Always test your formatting with sample data that includes edge cases such as null values, very long strings, or dates in different formats to ensure consistent output.