Free Support Forum - aspose.com

Hyperlinks with mergefields

hi,
i am using mergefields and executewithregions option with mergefields.

now my problem is i have two mergefiels (ImageLink) and (WebsiteUrl) how to insert hyperlinks to merge fields with hyperlinks

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

Thanks for your inquiry. You can use MergeField event handler to achieve this. For example see the following code and attached documents:

public void Test275()

{

string[] names = { "ImageLink", "WebsiteUrl" };

string[] values = { "http://philip9876.files.wordpress.com/2007/10/nuclear-explosion.jpg", "http://www.aspose.com" };

//Open template

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

//And MergedField event handler

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField275);

//Execute mail merge

doc.MailMerge.Execute(names, values);

//Save document

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

}

void MailMerge_MergeField275(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "ImageLink" || e.FieldName == "WebsiteUrl")

{

//Create DocumentBuilder

DocumentBuilder builder = new DocumentBuilder(e.Document);

//Move to mergefield

builder.MoveToMergeField(e.FieldName);

//Insert hyperlink

builder.Font.Underline = Underline.Single;

builder.Font.Color = Color.Blue;

builder.InsertHyperlink(e.FieldValue.ToString(), e.FieldValue.ToString(), false);

}

}

Hope this helps.

Best regards.