Make MailMerge field as hyperlink

Hi,
How do I make the mailmerge field appeared as hyperlink.The text and the Url behind it are from the xml.
For example my XML is

<MyNode>
     <Text>This is the text to display</Text>
     <Url>http://theurl.com</Url>
<MyNode>

If in the word template I have the mergefield as <>, how can I make the url xml data linked to it. Thank you.
Regards
TlD

Hi Lan,

Thanks for your inquiry. I think, you can achieve what you need after reading the article suggested here:
https://docs.aspose.com/words/java/mail-merge-and-reporting/

Moreover, to insert a Hyperlink instead of plain text, please try using the DocumentBuilder.InsertHyperlink method inside IFieldMergingCallback.FieldMerging event as follows:

builder.InsertHyperlink("Aspose", "https://www.aspose.com/", false);

Please let us know if you need more information, We are always glad to help you.

Best Regards,

Hi,
What I need is to replace the mergefield <> with the Text from the XML (like any other mail merge field) but I don’t know how to place the the url behind with the Url vale from the XML. I don’t get how InsertHyperlink help in this case. Please give more information.
Thank you.
Regards

Hi,

Thanks for your inquiry. To be able to replace the MergeField text with a Hyperlink, please see the following code:

Document doc = new Document(@"c:\test\mergefield.docx");
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldAlternatingRows();
doc.MailMerge.Execute(new string[] { "mergefield" }, new object[] { "http://www.aspose.com/" });
doc.Save(@"c:\test\mergefield_out.docx");

==========================================================

private class HandleMergeFieldAlternatingRows : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (mBuilder == null)
            mBuilder = new DocumentBuilder(e.Document);
        if (e.FieldName.Equals("mergefield"))
        {
            mBuilder.MoveToMergeField(e.FieldName);
            // I think, if you need a custom Text for the link, you have to query again here:
            mBuilder.InsertHyperlink(e.FieldValue.ToString(), e.FieldValue.ToString(), false);
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        throw new NotImplementedException();
    }
    private DocumentBuilder mBuilder;
}

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi,
Thanks for the code, but I still confused here

doc.MailMerge.Execute(new string[] { "mergefield" }, new object[] { "http://www.aspose.com/" });

This is not used anywhere, especially the url "http://www.aspose.com/
In the function, you specifically check the mergefield name, it is not the field passed from somewhere else. Anyway, I just try to put my mergefield name there, and the value of that mergefield becomes the hyperlink. But still it is not what I really want. Please look at my xml in the first note, so what I need to achieve is to have
This is the text to display as an hyperlink and when you hover over it you will see the url http://theurl.com and I don’t know how to do it. Thank you.

Hi,

Thanks for your inquiry.

First, to be able to achieve what you are looking for, I would like to suggest you to please change the structure of your XML data source file to the following:

<?xml version="1.0" encoding="utf-8" ?>
<linksDs>
	<links link="Aspose,**http://www.aspose.com/**"/>
	<links link="Google,**http://www.google.com/**"/>
	<links link="Microsoft,**http://www.microsoft.com/**"/>
</linksDs>

Then based on the above XML data source, please try using the following code:

DataSet linksDs = new DataSet();
linksDs.ReadXml(@"C:\test\XMLFile1.xml");
Document doc = new Document(@"C:\test\MailMergeXML.docx");
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldAlternatingRows();
doc.MailMerge.Execute(linksDs.Tables["links"]);
doc.Save(@"c:\test\MailMergeXML_out.docx");

=============================================================================
private class HandleMergeFieldAlternatingRows : IFieldMergingCallback

{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (mBuilder == null)
            mBuilder = new DocumentBuilder(e.Document);
        if (e.FieldName.Equals("link"))
        {
            string[] splits = e.FieldValue.ToString().Split(new char[] { ',' });
            string displayText = splits[0];
            string url = splits[1];
            mBuilder.MoveToMergeField(e.FieldName);
            mBuilder.InsertHyperlink(displayText, url, false);
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        throw new NotImplementedException();
    }
    private DocumentBuilder mBuilder;
}

Moreover, I have attached a Word document (i.e. MailMergeXML.docx) here so that you can actually run the above sample by yourself.

I hope, this will help.

Best Regards,

Hi,
Thank you for the code, but I have to change it a bit to make it work for me. So it’s all good. But one more thing I want to ask why it’s not appear as the link (eg Test)with the underline, it is just appeared as a normal text “Test” unless you hover it then you can see it’s a hyperlink. Thank you.

Hi,

Thanks for your request.

You should apply Hyperlink style before using the InsertHyperlink method e.g.

mBuilder.MoveToMergeField(e.FieldName);
mBuilder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
mBuilder.InsertHyperlink(displayText, url, false);

Best Regards,

Wonderful. Thank you.
Regards
Tld