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