Use Simple Aggregates (Sum, Count, Average)
Leave feedback
Overview
GroupDocs.Assembly for Java supports LINQ-like aggregate extension methods that allow you to calculate sums, counts, averages, and other aggregate values directly in templates. These functions work with collections and enable you to display totals, summaries, and statistical information in your documents.
The main aggregate functions are:
sum() - calculate sum of numeric values
count() - count number of items
average() - calculate average of numeric values
min() - find minimum value
max() - find maximum value
Here are the steps to use aggregates:
Create a template with aggregate expressions
Use aggregate extension methods on collections
Reference aggregate results in your template
Assemble the document with your data
Note
Aggregate functions use LINQ-like syntax with lambda expressions (i => i.getPrice()) and work with any enumerable collection, including arrays, java.util collections, DataTable objects, and other Iterable implementations. See Enumeration Extension Methods for the full list of available methods.
Calculate Sum
Calculate the sum of values in a collection:
importcom.groupdocs.assembly.*;publicclassOrderItem{privateStringproductName;privatedoubleprice;privateintquantity;publicOrderItem(StringproductName,doubleprice,intquantity){this.productName=productName;this.price=price;this.quantity=quantity;}publicStringgetProductName(){returnproductName;}publicdoublegetPrice(){returnprice;}publicintgetQuantity(){returnquantity;}}publicstaticvoidcalculateSum()throwsException{// Pass an array (or a POJO with a typed List<OrderItem> getter) so that the engine
// knows the item type; a raw java.util.List passed directly loses its element type
OrderItem[]items=newOrderItem[]{newOrderItem("Laptop",999.99,1),newOrderItem("Mouse",29.99,2),newOrderItem("Keyboard",79.99,1)};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("AggregatesTemplate.docx","AggregatesOutput.docx",newDataSourceInfo(items,"items"));System.out.println("Sum calculated successfully.");}
importcom.groupdocs.assembly.*;publicstaticvoidcalculateMinMax()throwsException{Sale[]sales=newSale[]{newSale(1000.00),newSale(1500.00),newSale(800.00)};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("AggregatesTemplate.docx","AggregatesOutput.docx",newDataSourceInfo(sales,"sales"));System.out.println("Min and max calculated successfully.");}
Template:
Minimum Sale: <<[sales.min(s => s.getAmount())]:"$#,##0.00">>
Maximum Sale: <<[sales.max(s => s.getAmount())]:"$#,##0.00">>
<<foreach [in orders]>>
Order #<<[getOrderId()]>>
Items:
<<foreach [in getItems()]>>
<<[getProductName()]>> - <<[getPrice()]:"$#,##0.00">> x <<[getQuantity()]>>
<</foreach>>
Order Total: <<[getItems().sum(i => i.getPrice() * i.getQuantity())]:"$#,##0.00">>
<</foreach>>
Note
A List<OrderItem> returned by a getter keeps its item type (the engine reads the generic return type of the getter), so lambdas such as i => i.getPrice() work on it. A raw java.util.List of POJOs passed directly to DataSourceInfo does not keep its item type; pass an array or wrap the list in a POJO instead.
Warning
Aggregate functions require collections. Ensure your data source is a collection type (array, java.util collection, DataTable, etc.) when using aggregates.
Advanced Usage Topics
To learn more about grouping, filtering with aggregates, custom aggregate functions, and performance optimization, 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: