We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to insert Images using mail merge?

public void ExecuteWithRegionsDataTable()

{

Document doc2 = new Document(Server.MapPath("MailMerge_Table.doc"));

doc2.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);

DataTable orderTable = GetTestOrder();

orderTable.TableName = "Order";

doc2.MailMerge.ExecuteWithRegions(orderTable);

doc2.Save("MailMerge_Table_Demo.doc",SaveFormat.Doc,SaveType.OpenInBrowser,Response);

}

void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)

{

MemoryStream objImage = new MemoryStream();

WebClient objwebClient = new WebClient();

string sURL = "http://localhost/Aspose/Sunset.jpg";

objImage = new MemoryStream(objwebClient.DownloadData(sURL));

e.ImageStream = objImage;

}

I have the above code but how can I insert the image? and what should I mention in the word document template like Image:MyFieldName? what should be the filename?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

Hi Alexey,

I’ve just tried your example above using the full URL path, but I get the exception "Cannot load image from field…data in unsupported format."

can you help?

===========

more info:

I got an 502 bad gateway error due to the URL image
that I try to look up to is a website in my dev server. I understand
that this issue will be resovled once I put my website up on the
internet but I need to proof this is working to my client on my dev
machine, can you please give me some advise?

more info:

I got an 502 bad gateway error due to the URL image that I try to look up to is a website in my dev server. I understand that this issue will be resovled once I put my website up on the internet but I need to proof this is working to my client on my dev machine, can you please give me some advise?

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information.

Let’s distinguish the possible causes. First, you should check whether file referenced by this URL is accessible in the context of your application. Second, it should have recognizable format. From your post I doubt what case is yours. If you really need only to demonstrate how your application runs in development environment you can temporarily switch to using a local file. Just ensure that logon user has enough permission to read it.

Regards,

Thanks. I’ve managed to solve my issue by turning to false in my web.config.

However, I then get a 401 unauthorise error as my site does not use anonymous access.

I then convert the image to byte[] instead of using the URL path but that seems to only work on the doc.MailMerge.Execute method but not for the doc.MailMerge.ExecuteWithRegion method.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. This works fine on my side.

//Create dummy data source

DataTable myTable = new DataTable("myTable");

//Add columns

myTable.Columns.Add("Name", typeof(string));

myTable.Columns.Add("myImg", typeof(byte[]));

//Add few rows

myTable.Rows.Add(new object[] { "First Image", File.ReadAllBytes(@"Test110\test1.jpg") });

myTable.Rows.Add(new object[] { "Second Image", File.ReadAllBytes(@"Test110\test2.jpg") });

myTable.Rows.Add(new object[] { "Third Image", File.ReadAllBytes(@"Test110\test3.jpg") });

//Open template document

Document doc = new Document(@"Test110\in.doc");

//Execute mail merge with regions

doc.MailMerge.ExecuteWithRegions(myTable);

//Save document

doc.Save(@"Test110\out.doc");

Please provide me your code for testing.

Best regards.

i check to your in.doc and i see i was missing <> and <<Image: and <>

what does are this tag exactly. there is no word datasource with the attachmente.

do i have to create thos like fields?

i tried to add this fields but then when i add them do the document the : after tableend is missing but from the field list there is. i attach my template with the datasource

regards

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. You can use ‘_’ instead of semicolon. So TableStart:myTable can also be TableStart_myTable.

Data source attached to the document is not needed if you execute mail merge using Apsose.Words. in my example, I used DataTable object as a data source for mail merge.

Hope this helps.

Best regards.