What functions and mathematical operators are supported in iText DITO?
Sometimes, it might be impossible to show the necessary information on the output form using only data binding. You might for example want to show a total at the end of an invoice, containing the sum of all items ordered in their respective quantities, multiplied by the VAT.
In order to simplify processing of the data without the need for pre-processing, iText DITO can handle many straightforward and more complex calculations.
Inserting a Calculation
Calculations can be inserted into rich text elements. To insert a calculation, click the f(x) icon in the command bar. Bear in mind that it can be hidden under the ellipsis (three vertical dots) icon.
Straightforward Calculations
iText DITO supports the following functions:
Function | Description | Example |
---|---|---|
sum() | calculates the sum of the arguments | sum({{field1}}+{{field2}}) or sum({{field1}}-{{field2}}) |
product() | calculates the product of the arguments | product({{field1}}*{{field2}}) or product({{field1}}/{{field2}}) |
concat() | concatenates text strings | concat({{first_name}},' ',{{last_name}}) |
size() | calculates the number of items in an array | size({{items}}) |
numberToText() | converts a numerical value into a text string. Optionally you can add a locale. | numberToText({{numerical_field}}, 'en-us') |
moneyToText() | converts a numerical value to a text string, with a currency name. | moneyToText({{numerical_field}}, 'en-us') |
And the following operators:
"+", "-", "*", "/"
Example
Imagine you are designing a report on consumption of service hours. The customer has a certain entitlement of support hours and has consumed some of those. Your report should calculate the total amount due for the hours consumed and the remainder of the entitlement. The data contains only these fields:
{{service_hour_entitlement}}, {{service_hour_consumption}}, {{service_hour_unit_price}}
To calculate the total amount due, you can use the product() function
To calculate the remainder of the service entitlement, you can use the sum() function
Calculations within Loops
All of the straightforward calculations can be used inside a repeatable element, like a table row that repeats for all items on an invoice template. In that case make sure to use relative references to the data elements.
More Complex Calculations
It is also possible to calculate a field using other fields which are the results of calculations themselves. In order to perform calculations on the variables in a field, you must name the fields. The field names act as variables that you can use in mathematical expressions.
Example
In the example below the aim is to calculate a grand total based on calculated subtotals.
First, create a table with repeating rows for each item in an array. In the last cell of the row, insert a rich text element with a product() calculation.
To be able to use that product() in another calculation, give the rich text element a name.
You can now use this name in other calculations, for instance to calculate the grand total. Note that you don't use double curly braces to refer to this named field. the double curly braces are only used to refer to elements in the data sample.