Free Support Forum - aspose.com

Validating the image stream retreived during an IMAGE: merge field?

Hello,

I’m hoping you might have a suggestion as to the best way to proceed on a small problem.

I have a merge template that works very well and includes a merge field with the IMAGE: tag, e.g.:
{ MERGEFIELD IMAGE:PHOTO_URL }

The problem I’m experiencing is that some records in the data table have no value for the PHOTO_URL column. Apparently if that column is empty, Words still attempts to insert an Image field and it causes Words to fail with:

exception ‘cannot load image
from field ‘property_image_url2’. the field contains data in
unsupported format. empty path name is not legal.’ occured in
aspose.words for .net 4.4.1.0.
for free technical support, please post this error and the file in the
aspose.words forums
http://www.aspose.com/forums/showforum.aspx?forumid=75.


The other related issue is that if the PHOTO_URL has a bad URL (file missing, etc), I get the same error. My question: how would you suggest I handle these two situations? I see that there’s an MergeImageFieldEventHandler, and I could use it to check to see if e.FieldValue is null or empty, and that might take care of an empty URL.

But in the case of an invalid URL, do you have any other suggestions as to how I can validate the imagestream as to whether it really is an image, and to prevent Words from attempting to create an image if the validation fails?

You guys always have such great ideas. Many thanks for your suggestions.

Mike

Hi

Thanks for your request. You was absolutely right; you should use the MergeImageField event. See the following code example.

public void TestMailMerge_99610()

{

//Create dataTable

DataTable table = new DataTable("table");

table.Columns.Add("PHOTO_URL");

DataRow row = table.NewRow();

row[0] = ""; //empty data

table.Rows.Add(row);

DataRow row1 = table.NewRow();

row1[0] = "http://www.drive1.ru/download/images/picture2.jpg"; //bad url

table.Rows.Add(row1);

DataRow row2 = table.NewRow();

row2[0] = "http://www.drive.ru/download/images/picture2.jpg"; //valid data

table.Rows.Add(row2);

//Open template

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

//add event handler

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField_99610);

//execute mail merge

doc.MailMerge.ExecuteWithRegions(table);

//save document

doc.Save(@"256_99610_DenverMike\out.doc");

}

void MailMerge_MergeImageField_99610(object sender, MergeImageFieldEventArgs e)

{

if (!String.IsNullOrEmpty(e.FieldValue.ToString()))

{

try

{

//get response stream from url

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(e.FieldValue.ToString());

request.Method = "GET";

request.ContentType = "multipart/form-data";

request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";

request.Proxy.Credentials = CredentialCache.DefaultCredentials;

request.Credentials = CredentialCache.DefaultCredentials;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream resStream = response.GetResponseStream();

//create image from stream

Image img = Image.FromStream(resStream);

//insert image into the document

e.Image = img;

}

catch

{

//remove field.

//Also you can insert the default image here.

e.Field.Remove();

}

}

else

{

//remove field.

//Also you can insert the default image here.

e.Field.Remove();

}

}

I hope that this code will help you to solve your problem.

Best regards.

Thank you, Alexey! That was exactly what I needed… it’s working great!

Mike