facebooktwittermenuarrow-up

GemBox Support Forum

Hyperlink Find and Replace

I’m trialing GemBox.Document for a specific task as an in-house path updater for our stored and archived documents.

Initially, I would like it simply to find->replace hyperlinks in our documents. I ran into an issue where I can get the hyperlink.Content to update but the hyperlink.Address does not.

The example I have created:
Assuming links in doc are Google | http://www.google.com
replacing with Microsoft | http://www.microsoft.com

using GemBox.Document;

private void SaveLink("file-to-open.docx")
{
    ComponentInfo.SetLicense("FREE-LIMITED-KEY");

    var document = DocumentModel.Load("file-to-open.docx");
    Hyperlink[] doclinks = document.GetChildElements(true, ElementType.Hyperlink).Cast<Hyperlink>().ToArray();

    foreach (var hyperlink in doclinks)
    {
        if (hyperlink.Address == "http://www.google.com/")
        {
            Hyperlink newlink = new Hyperlink(document, "http://www.microsoft.com/", "Microsoft");

            //works
            hyperlink.Content.Replace(hyperlink.Content.ToString(), newlink.Content.ToString());
            //does not work
            hyperlink.Address.Replace(hyperlink.Address, newlink.Address);

            /* Also tried this below. Will update 'Content' but not address if is hyperlink.
             * But as expected if finds the non-hyperlink word 'Google' will replace with 'Microsoft AND will make it a link */
            foreach (ContentRange searchedContent in document.Content.Find(hyperlink.Content.ToString()).Reverse())
            {
                Hyperlink newlink = new Hyperlink(document, "http://www.microsoft.com/", "Microsoft");
                searchedContent.Set(newlink.Content);
            }

            document.Save("newfile.docx");
        }
    }
}

Any direction would be appreciated

Hi Ron,

The method you commented with “works” is ContentRange.Replace, and the method you commented with “does not work” is String.Replace.
The String objects are immutable, the Replace method returns a new String object.

Anyway, can you please try this:

if (hyperlink.Address == "http://www.google.com/")
{
    hyperlink.DisplayInlines.Content.LoadText("Microsoft");
    hyperlink.Address = "http://www.microsoft.com/";
}

Does this solve your issue?

As an FYI, the reason why that “searchedContent” replacement didn’t work is because that is a display inline of a parent hyperlink.
Instead, you would need to use something like this:

Hyperlink newlink = new Hyperlink(document, "http://www.microsoft.com/", "Microsoft");
hyperlink.Content.Set(newlink.Content);

However, note that newlink will have the default formatting while the original hyperlink might have something else.
So in short, you’re replacing the previous hyperlink with a new one that has a default style.
But in the first example, the LoadText will use the previous formatting when loading the new text.

Regards,
Mario

That was great, thank you. I am putting together a proof of concept for purchase for management.