Hi
Thanks for your request. In the database you can store explicit path to image, file name only, of image bytes.
During mail merge you can insert images if you have mergefield with name like “Image:myImage”.
For example in the document you have the following:
Full name |
Image |
«TableStart:myRegion»«Name» |
«Image:myImage»«TableEnd:myRegion» |
So in your datasource you should have table with name “myRegion” and this table should have columns with names “Name” and “myImage”.
For example in the “myImage” column will be stored name of image. In this case we should generate explicit path to image on fly. See the following code for example:
public void Test065()
{
// Here we create dummy datasource
DataTable tab = new DataTable("myRegion");
// Add columns
tab.Columns.Add("Name");
tab.Columns.Add("myImage");
// Add few rows
tab.Rows.Add(new object[] { "First Image", "1.jpg" });
tab.Rows.Add(new object[] { "Second Image", "2.jpg" });
// etc...
// Open document
Document doc = new Document(@"Test065\in.doc");
// Add MergeImageField event
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);
// Execute mail merge
doc.MailMerge.ExecuteWithRegions(tab);
// Save output
doc.Save(@"Test065\out.doc");
}
void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)
{
// Check field name
if (e.FieldName == "myImage")
{
// For example images stored in C:\Temp folder
string fullFileName = Path.Combine(@"C:\Temp", e.FieldValue.ToString());
// Set file name of image
e.ImageFileName = fullFileName;
}
}
If you store full path to image in your data source then you don’t need mergeImageField event handler at all.See the following code:
// Here we create dummy datasource
DataTable tab = new DataTable("myRegion");
// Add columns
tab.Columns.Add("Name");
tab.Columns.Add("myImage");
// Add few rows
tab.Rows.Add(new object[] { "First Image", @"C:\Temp\1.jpg" });
tab.Rows.Add(new object[] { "Second Image", @"C:\Temp\2.jpg" });
tab.Rows.Add(new object[] { "Second Image", @"http://www.aspose.com/Images/aspose-logo.jpg" });
// etc...
// Open document
Document doc = new Document(@"Test065\in.doc");
// Execute mail merge
doc.MailMerge.ExecuteWithRegions(tab);
// Save output
doc.Save(@"Test065\out.doc");
As you can see you can also use URL as a path to the image.
In the attachment you can find my template.
Hope this information could help you.
Best regards.