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:
Warning
Since version 26.11, external resources whose references come from data, such as an http or https URI returned by an image expression, are skipped by default and give an empty image. Allow the hosts you trust with LoadSaveOptions.WhitelistedResources, as shown below, or set LoadSaveOptions.SkipExternalResources to false. See Controlling External Resources.
usingGroupDocs.Assembly;publicclassProduct{publicstringName{get;set;}publicstringImageUrl{get;set;}}publicstaticvoidInsertImageFromUrl(){varproduct=newProduct{Name="Laptop",ImageUrl="https://example.com/images/laptop.jpg"};// The URL comes from data, so its host must be allowedLoadSaveOptionsoptions=newLoadSaveOptions();options.WhitelistedResources.Add("example.com");DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ImageTemplate.docx","ImageOutput.docx",options,newDataSourceInfo(product,"product"));Console.WriteLine("Image inserted from URL successfully.");}
A relative file path returned by an image expression, such as "../Images/no-photo.jpg", is resolved against the folder of the template first, by analogy with linked resources of Microsoft Office documents. If no file exists there, the current directory of the application is used. A template loaded from a stream has no folder: set it with LoadSaveOptions.ResourceLoadBaseUri.
In the example, templates are in C:\Reports\Templates, images are in C:\Reports\Images, and the template inserts <<image [expression]>>:
usingGroupDocs.Assembly;usingSystem.IO;publicstaticvoidInsertImageFromRelativePath(){conststringrelativePath="../Images/no-photo.jpg";DocumentAssemblerassembler=newDocumentAssembler();// A template loaded from a file: the path is resolved against the folder of the template.assembler.AssembleDocument(@"C:\Reports\Templates\Inserting image.docx","output.docx",newDataSourceInfo(relativePath,"expression"));// A template loaded from a stream has no folder: set it with ResourceLoadBaseUri.LoadSaveOptionsoptions=newLoadSaveOptions();options.ResourceLoadBaseUri=@"C:\Reports\Templates";using(FileStreamtemplate=File.OpenRead(@"C:\Reports\Templates\Inserting image.docx"))using(FileStreamreport=File.Create("output-from-stream.docx")){assembler.AssembleDocument(template,report,options,newDataSourceInfo(relativePath,"expression"));}}
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.");}
In a presentation template, a table cell is an image container as well: the image becomes the picture fill of the cell, including cells of rows repeated by <<foreach>>. A template table listing products with their pictures looks like this:
Name
Photo
<<foreach [p in products]>><<[p.Name]>>
<<image [p.Photo] -fitHeight>><</foreach>>
usingGroupDocs.Assembly;publicclassProduct{publicstringName{get;set;}publicstringPhoto{get;set;}}publicstaticvoidInsertImagesIntoTableCells(){Product[]products={newProduct{Name="Logo",Photo="Images/Logo.jpg"},newProduct{Name="No photo",Photo="Images/no-photo.jpg"},};newDocumentAssembler().AssembleDocument("Image in table cell.pptx","output.pptx",newDataSourceInfo(products,"products"));}
With -fitHeight, the row grows to show the image with its aspect ratio. The other fit switches keep the aspect ratio of the image within the cell, because the width of a cell is the width of its whole column. Tags in shapes of group shapes, including nested groups, are processed like tags in other shapes. Table cells and group shapes of presentations are supported since version 26.11. Example: InsertImageIntoTableCell.cs.
Warning
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: