Apply StyleIdentifier.Hyperlink to a subset of the string while applying the mailmerge

Hi,
We are currently using the mailmerge functionality and inserting HTML in the word. We recently noticed that if the HTML contains a link, the link gets embedded in the Word file but the styling doesn’t seem to apply (underline and blue color). I tried exploring the docs and found that it can be added by adding
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink

but if I add it before doing builder.InsertHtml((string)args.FieldValue, true);, it gets applied to the entire text and not just the text containing hyperlinks. So if the text is 20 lines long with one hyperlink in between, it should apply the styling just for the text containing the hyperlink. Can you please advise how to achieve this?

To apply specific formatting, such as underline and blue color, only to the hyperlinks in the HTML content inserted during mail merge using Aspose.Words, you can follow these steps:

Parse the HTML content to identify hyperlinks.
Apply the desired formatting to the identified hyperlinks.
Here’s how you can achieve this:

using Aspose.Words;
using Aspose.Words.Markup;

namespace MailMergeWithHTML
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document("template.docx");

            // Simulate mail merge
            doc.MailMerge.Execute(new string[] { "HyperlinkField" }, new object[] { "<a href=\"https://www.example.com\">Click here</a>" });

            // Apply formatting to hyperlinks
            ApplyHyperlinkFormatting(doc);

            doc.Save("output.docx");
        }

        static void ApplyHyperlinkFormatting(Document doc)
        {
            NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);

            foreach (FieldStart fieldStart in fieldStarts)
            {
                if (fieldStart.FieldType == FieldType.FieldHyperlink)
                {
                    FieldHyperlink field = new FieldHyperlink(fieldStart);
                    Node linkNode = fieldStart.GetAncestor(NodeType.Run);

                    if (linkNode != null)
                    {
                        foreach (Run run in linkNode.GetChildNodes(NodeType.Run, true))
                        {
                            run.Font.Color = Color.Blue;
                            run.Font.Underline = Underline.Single;
                        }
                    }
                }
            }
        }
    }
}

In this code:

Load the Word document.
Perform mail merge with HTML content containing hyperlinks.
Apply formatting to the hyperlinks using the ApplyHyperlinkFormatting method.
The ApplyHyperlinkFormatting method iterates through the field starts in the document, identifies hyperlinks, and applies formatting (blue color and underline) to the runs containing the hyperlink text.

Please adjust the code according to your specific use case and styling requirements. As always, refer to the latest Aspose.Words documentation and adapt the code to any potential updates or changes in the library.

@shrey, could you please create a minimal console application the shows the issue and attach it along with sample documents? This will help us an analyze the issue.

Hi,
The above solution didn’t work as linkNode is always null so the process doesn’t get triggered. Please find the attached console app for better understanding of the original issue.

Thanks,
Shrey

HyperLink With Aspose.Words.zip (51.7 KB)

@shrey, you could apply StyleIdentifier.Hyperlink to all hyperlinks after the mailmerge completed using the following code:

foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldHyperlink)
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field;
        Node current = hyperlink.Start;
        while ((current != null) && (current != field.End))
        {
            Inline inline = current as Inline;
            if (inline != null)
            {
                inline.Font.ClearFormatting();
                // By default MS Word applies Hyperlink style to the hyperlinks. Do the same.
                inline.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
            }

            current = current.NextSibling;
        }
    }
}

Hi @denis.shvydkiy,
The above logic worked. Thank you so much.

1 Like