GroupDocs.Assembly for Java allows you to insert images dynamically into documents using image tags. Images can be loaded from file paths, URLs, streams, byte arrays, BufferedImage objects, or Base64-encoded strings, making it easy to include dynamic images in your reports.
Add a textbox to your template at the location where you want the image
Set the textbox size and appearance to match desired image dimensions
Insert an image tag <<image [expression]>> within the textbox
Provide image data from file path, URL, stream, or byte array
Assemble the document
Note
Image tags are replaced with actual images during document assembly. The textbox acts as a placeholder and container for the image.
Note
The expression of an image tag must return a value of one of the following types: byte[] (image data), java.io.InputStream (a stream to read image data from), java.awt.image.BufferedImage, or String (a file path, a URI such as https://... or file:/..., or Base64-encoded image bytes). The type is checked by the declared return type of the getter, so declare getters with one of these types rather than Object.
Insert Image from File Path
Insert an image from a file path:
importcom.groupdocs.assembly.*;publicclassProduct{privateStringname;privateStringimagePath;publicProduct(Stringname,StringimagePath){this.name=name;this.imagePath=imagePath;}publicStringgetName(){returnname;}publicStringgetImagePath(){returnimagePath;}}publicstaticvoidinsertImageFromFilePath()throwsException{// Sample data with image path
Productproduct=newProduct("Laptop","C:\\Images\\laptop.jpg");// Assemble document
DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));System.out.println("Image inserted from file path successfully.");}
Template Structure for Images
In your template (ImageTemplate.docx):
Insert a textbox where you want the image
Set the textbox size (width and height)
Add the image tag inside the textbox:
Product: <<[product.getName()]>>
[Textbox with image tag:]
<<image [product.getImagePath()]>>
Insert Image from URL
Insert an image from a URL:
importcom.groupdocs.assembly.*;publicclassOnlineProduct{privateStringname;privateStringimageUrl;publicOnlineProduct(Stringname,StringimageUrl){this.name=name;this.imageUrl=imageUrl;}publicStringgetName(){returnname;}publicStringgetImageUrl(){returnimageUrl;}}publicstaticvoidinsertImageFromUrl()throwsException{OnlineProductproduct=newOnlineProduct("Laptop","https://example.com/images/laptop.jpg");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));System.out.println("Image inserted from URL successfully.");}
importcom.groupdocs.assembly.*;importjava.io.FileInputStream;importjava.io.InputStream;publicclassProductWithImageStream{privateStringname;privateInputStreamimageStream;publicProductWithImageStream(Stringname,InputStreamimageStream){this.name=name;this.imageStream=imageStream;}publicStringgetName(){returnname;}publicInputStreamgetImageStream(){returnimageStream;}}publicstaticvoidinsertImageFromStream()throwsException{try(InputStreamimageStream=newFileInputStream("C:\\Images\\laptop.jpg")){ProductWithImageStreamproduct=newProductWithImageStream("Laptop",imageStream);DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));System.out.println("Image inserted from stream successfully.");}}
Image Sizing Options
Control how images are sized within textboxes:
importcom.groupdocs.assembly.*;publicstaticvoidinsertImageWithSizing()throwsException{Productproduct=newProduct("Laptop","C:\\Images\\laptop.jpg");DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));System.out.println("Image inserted with sizing options.");}
Template sizing options:
<<image [product.getImagePath()]>> - Stretch to fill textbox (default)
<<image [product.getImagePath()] -keepRatio>> - Keep aspect ratio within textbox
<<image [product.getImagePath()] -fitHeight>> - Fit to textbox height
<<image [product.getImagePath()] -fitWidth>> - Fit to textbox width
<<image [product.getImagePath()] -fitSize>> - Resize textbox to image size
<<image [product.getImagePath()] -fitSizeLim>> - Resize textbox up to image size
Insert Multiple Images from Collection
Insert images for each item in a collection:
importcom.groupdocs.assembly.*;publicstaticvoidinsertImagesFromCollection()throwsException{// Pass an array (or a POJO with a typed List<Product> getter) so that the engine
// knows the item type; a raw java.util.List passed directly loses its element type
Product[]products=newProduct[]{newProduct("Laptop","C:\\Images\\laptop.jpg"),newProduct("Mouse","C:\\Images\\mouse.jpg")};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("ImagesTemplate.docx","ImagesOutput.docx",newDataSourceInfo(products,"products"));System.out.println("Images inserted from collection successfully.");}
If an image expression returns a stream, the assembler reads the image data from it but does not close it. Close the stream yourself after the document is assembled, for example with a try-with-resources statement as shown above.
Advanced Usage Topics
To learn more about image formatting, Base64-encoded images, image optimization, and advanced image handling, 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: