Bullet lists are a common way to display collections of items in documents. GroupDocs.Assembly for Java makes it easy to generate bulleted lists by iterating over data collections using foreach data bands within list structures.
Add a foreach data band within the list to iterate over your collection
Reference collection item members in list items
Close the foreach data band
Assemble the document with your collection data
Note
The foreach data band automatically repeats list items for each element in the collection, creating a complete bulleted list with all data items.
Generate Bullet List from Java Collection
Create a bullet list from a collection of objects:
importcom.groupdocs.assembly.*;publicclassProduct{privatefinalStringname;privatefinalStringdescription;publicProduct(Stringname,Stringdescription){this.name=name;this.description=description;}publicStringgetName(){returnname;}publicStringgetDescription(){returndescription;}}publicstaticvoidgenerateBulletList()throwsException{// Create sample data
Product[]products={newProduct("Laptop","High-performance laptop"),newProduct("Mouse","Wireless mouse"),newProduct("Keyboard","Mechanical keyboard")};// Assemble document
DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("BulletListTemplate.docx","BulletListOutput.docx",newDataSourceInfo(products,"products"));System.out.println("Bullet list generated successfully.");}
Note
Because of Java type erasure, the engine cannot determine the item type of a java.util.List passed directly as a data source, so calling item methods such as getName() fails. Pass an array (as shown above), or expose the list through a getter of a wrapper object (for example, public List<Product> getProducts()) and iterate over it in the template: <<foreach [in data.getProducts()]>>.
Template Structure for Bullet Lists
In your template document (BulletListTemplate.docx), create a bullet list with this structure:
We provide the following products:
<<foreach [in products]>>• <<[getName()]>> - <<[getDescription()]>>
<</foreach>>
Put the opening <<foreach>> tag at the beginning of the bulleted paragraph and the closing <</foreach>> tag at the beginning of the next paragraph. Members of Java objects are accessed through their getter methods. After assembly, you’ll get:
We provide the following products:
• Laptop - High-performance laptop
• Mouse - Wireless mouse
• Keyboard - Mechanical keyboard
Generate Bullet List from DataTable
You can also generate bullet lists from a DataTable:
importcom.groupdocs.assembly.*;importcom.groupdocs.assembly.system.data.*;publicstaticvoidgenerateBulletListFromDataTable()throwsException{// Create DataTable
DataTableproductsTable=newDataTable("Products");productsTable.getColumns().add("Name",String.class);productsTable.getColumns().add("Description",String.class);// Add rows
productsTable.getRows().add("Laptop","High-performance laptop");productsTable.getRows().add("Mouse","Wireless mouse");productsTable.getRows().add("Keyboard","Mechanical keyboard");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("DataTableBulletListTemplate.docx","BulletListOutput.docx",newDataSourceInfo(productsTable,"products"));System.out.println("Bullet list generated from DataTable successfully.");}
DataTable columns are referenced by name rather than through getters, so the template (DataTableBulletListTemplate.docx) looks as follows:
We provide the following products:
<<foreach [in products]>>• <<[Name]>> - <<[Description]>>
<</foreach>>
Simple Bullet List (Name Only)
For simple lists showing only names:
importcom.groupdocs.assembly.*;importjava.util.*;publicstaticvoidgenerateSimpleBulletList()throwsException{List<String>items=Arrays.asList("Item 1","Item 2","Item 3");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("SimpleBulletListTemplate.docx","SimpleBulletListOutput.docx",newDataSourceInfo(items,"items"));System.out.println("Simple bullet list generated successfully.");}
Template for simple list:
Items:
<<foreach [item in items]>>• <<[item]>>
<</foreach>>
Here, the iteration variable item refers to the current string itself.
Bullet List in Spreadsheet
Bullet lists work in spreadsheet templates too:
importcom.groupdocs.assembly.*;publicstaticvoidgenerateBulletListInSpreadsheet()throwsException{Product[]products={newProduct("Laptop","High-performance laptop"),newProduct("Mouse","Wireless mouse")};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("BulletListTemplate.xlsx","BulletListOutput.xlsx",newDataSourceInfo(products,"products"));System.out.println("Bullet list generated in spreadsheet successfully.");}
Bullet List in Email
Generate bullet lists in email messages:
importcom.groupdocs.assembly.*;publicstaticvoidgenerateBulletListInEmail()throwsException{Product[]products={newProduct("Laptop","High-performance laptop"),newProduct("Mouse","Wireless mouse")};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("BulletListTemplate.msg","BulletListOutput.msg",newDataSourceInfo(products,"products"));System.out.println("Bullet list generated in email successfully.");}
Warning
In Word documents, ensure the bullet list formatting is applied in the template. The foreach data band will repeat the list item format for each collection element.
Advanced Usage Topics
To learn more about nested bullet lists, conditional list items, and advanced list formatting, 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: