GroupDocs.Assembly for .NET allows you to insert images dynamically into documents using image tags. Images can be loaded from file paths, URLs, streams, byte arrays, 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.
Insert Image from File Path
Insert an image from a file path:
usingGroupDocs.Assembly;publicclassProduct{publicstringName{get;set;}publicstringImagePath{get;set;}}publicstaticvoidInsertImageFromFilePath(){// Sample data with image pathvarproduct=newProduct{Name="Laptop",ImagePath=@"C:\Images\laptop.jpg"};// Assemble documentDocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));Console.WriteLine("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.Name]>>
[Textbox with image tag:]
<<image [product.ImagePath]>>
Insert Image from URL
Insert an image from a URL:
usingGroupDocs.Assembly;publicclassProduct{publicstringName{get;set;}publicstringImageUrl{get;set;}}publicstaticvoidInsertImageFromUrl(){varproduct=newProduct{Name="Laptop",ImageUrl="https://example.com/images/laptop.jpg"};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));Console.WriteLine("Image inserted from URL successfully.");}
usingGroupDocs.Assembly;usingSystem.IO;publicclassProduct{publicstringName{get;set;}publicStreamImageStream{get;set;}}publicstaticvoidInsertImageFromStream(){using(FileStreamimageStream=newFileStream(@"C:\Images\laptop.jpg",FileMode.Open)){varproduct=newProduct{Name="Laptop",ImageStream=imageStream};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));Console.WriteLine("Image inserted from stream successfully.");}}
Image Sizing Options
Control how images are sized within textboxes:
usingGroupDocs.Assembly;publicstaticvoidInsertImageWithSizing(){varproduct=newProduct{Name="Laptop",ImagePath=@"C:\Images\laptop.jpg"};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ImageTemplate.docx","ImageOutput.docx",newDataSourceInfo(product,"product"));Console.WriteLine("Image inserted with sizing options.");}
Template sizing options:
<<image [product.ImagePath]>> - Stretch to fill textbox (default)
<<image [product.ImagePath] -keepRatio>> - Keep aspect ratio within textbox
<<image [product.ImagePath] -fitHeight>> - Fit to textbox height
<<image [product.ImagePath] -fitWidth>> - Fit to textbox width
<<image [product.ImagePath] -fitSize>> - Resize textbox to image size
<<image [product.ImagePath] -fitSizeLim>> - Resize textbox up to image size
Insert Multiple Images from Collection
Insert images for each item in a collection:
usingGroupDocs.Assembly;usingSystem.Collections.Generic;publicstaticvoidInsertImagesFromCollection(){List<Product>products=newList<Product>{newProduct{Name="Laptop",ImagePath=@"C:\Images\laptop.jpg"},newProduct{Name="Mouse",ImagePath=@"C:\Images\mouse.jpg"}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ImagesTemplate.docx","ImagesOutput.docx",newDataSourceInfo(products,"products"));Console.WriteLine("Images inserted from collection successfully.");}
If an image expression returns a stream, the stream is automatically closed by the assembler after the image is loaded. Do not dispose the stream manually.
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: