Free Support Forum - aspose.com

Insert hyperlink from picture to another url using mergeimagefieldeventargs

Hi,

I am currently inserting an image into a document, using a mailmerge and the MergeImageFieldEventArgs with a handler. I do this because the image is coming from the database as a blob, I need to do some resizing, etc.

I have a new feature where we want the user to be able to click on this image, and it link to another page (in this case, another page that will show a full res version of the image).

I am currently using e.ImageStream to set the image contents.

How can I then create a link around this image that takes the user to another URL?

Hi

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

Thanks for your inquiry. You should set HRef property of the shape. Please follow the link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.drawing.shapebase.href.html

So, I suppose, to achieve this you will be compelled to use DocumentBulder.InsertImage method to insert pictures.

Best regards.

Yeah, this is what I found earlier. It looks like I am going to have to rewrite this part of my code.

This particular document only has this one image, so no need to even do a merge or have that handler. But Im assuming if you did have a handler, with multiple images, you would just have to do the documentbuilder.insertimage in the handler as well?

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

Thanks for your request. Yes, in case of using mail merge you can try using FieldMergingCallback, please see the following code:

// Open template

Document doc = new Document("in.doc");

// Prepare dummy data for mail merge

string[] names = { "myImg" };

object[] values = { File.ReadAllBytes("image.jpg") };

// Add merge image field event handler

doc.MailMerge.FieldMergingCallback = new MergeImageFieldHrefImage();

// Execute mail merge

doc.MailMerge.Execute(names, values);

// Save output document

doc.Save("out.doc");

private class MergeImageFieldHrefImage: IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

// Do nothing.

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Create DocuemntBuilder and move its cursor to the mergefield

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToField(e.Field, true);

// Insert image and specify its size

Shape shape = builder.InsertImage((byte[])e.FieldValue);

shape.HRef = "http://www.aspose.com/Community/Forums/75/ShowForum.aspx";

shape.ScreenTip = "Aspose.Words Support Forums";

e.Field.Remove();

}

}

Best regards,

I ended up just redoing my code to not use the handler since I didn’t really need it in this particular report. This worked just like needed. Only thing I noticed is that I set the ‘ScreenTip’ to something, and I don’t see it show up in IE (I assumed it was the alt text for the image).

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

Thanks for your inquiry. ScreenTip property defines the text displayed when the mouse pointer moves over the shape in MS Word. And as I can see it works as expected in MS Word.

Best regards,