Hello Aspose Team
Can I get help to know How to Find & Replace Hyperlinks in word document.
The sample code is not available in the below link
https://docs.aspose.com/words/net/working-with-fields/
Thanks in advance !
Regards
-David
Hello Aspose Team
Can I get help to know How to Find & Replace Hyperlinks in word document.
The sample code is not available in the below link
https://docs.aspose.com/words/net/working-with-fields/
Thanks in advance !
Regards
-David
Hi David,
Thanks for your inquiry. Please check the following page:
Download ReplaceHyperlinks.cs file
Moreover, a Hyperlink in Word document is represented by FieldHyperlink class. Here is sample code to get Hyperlinks to modify them:
foreach (Field field in doc.Range.Fields)
{
if (field.Type == FieldType.FieldHyperlink)
{
FieldHyperlink link = (FieldHyperlink)field;
// modify the link object as you need
}
}
Hope, this helps.
Best regards,
Hi
Thanks for your response.
In Aspose.Cells, we can able to find whether workbook has any external links (eg workbook.HasExernalLinks()).
Do we have the same functionality in Aspose.Word?
Also in Aspose.Cells, we can able to find whether the links are used or not using below code
var links = workbook.Worksheets.ExternalLinks;
for (int i = 0; i < links.Count; i++)
{
logEntry.Links = links[i].DataSource;
logEntry.LinksReferred = links[i].IsReferred;
}
Do we have the same functionality in Aspose.Word?
Also, I am replacing the existing link with the new link.
Existing link: C:\KM Project\broshare Statics Report.xlsx
New link: http://community.com/sites/km/TEST/EXFIX%20Testing/broshar
Statics Report.xlsx
I use below to replace it
Document objdoc = new Document(objMemory);
NodeList fieldStarts = objdoc.SelectNodes("//FieldStart");
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
{
// The field is a hyperlink field, use the "facade" class to help to deal with the field.
Hyperlink hyperlink = new Hyperlink(fieldStart);
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these.
if (hyperlink.IsLocal)
continue;
// The Hyperlink class allows to set the target URL and the display name
// of the link easily by setting the properties.
if (hyperlink.Target.ToString() == OldLink)
{
hyperlink.Target = NewLink;
iLlinksUpdated++;
}
// hyperlink.Name = NewName;
}
}
While checking the existing links with old link
if (hyperlink.Target.ToString() == OldLink)
In runtime, hyperlink.Target.ToString()
returns "C:\\KM Project\\broshare Statics Report.xlsx"
value which doesnt match with my oldLink input value.
Kindly assist
Regards,
David.
Hi David,
Thanks for your inquiry. We are working over your query and will get back to you soon.
Best regards,
HI,
Any updates??
Regards,
David.
Hi David,
Thanks for being patient. Please spare us some time for the investigation of this scenario. We will get back to you soon.
Best regards,
Hi David,
David:
In Aspose.Cells, we can able to find whether workbook has any external links (eg workbook.HasExernalLinks()).
You can iterate through all Hyperlinks in Word document and see if the FieldHyperlink.Address property is specified and check FieldHyperlink.SubAddress property to determine if it is a internal or external link.
David:
Also in Aspose.Cells, we can able to find whether the links are used or not using below code
An internal link in Word document most likely jumps to a Bookmark location. In this case you can get the Bookmark name from FieldHyperlink.SubAddress property.
David:
While checking the existing links with old link
if (hyperlink.Target.ToString() == OldLink)
In runtime, hyperlink.Target.ToString() returns “C:\KM Project\broshare Statics Report.xlsx” value which doesnt match with my oldLink input value.
Please use the following new code:
Document doc = new Document(MyDir + @"input.docx");
NodeList fieldStarts = doc.SelectNodes("//FieldStart");
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
{
FieldHyperlink hyperlink = (FieldHyperlink)fieldStart.GetField();
if (!string.IsNullOrEmpty(hyperlink.SubAddress))
continue;
if (hyperlink.Address.ToString() == @"C:\KM Project\broshare Statics Report.xlsx")
{
hyperlink.Address = "http://community.com/sites/km/TEST/EXFIX%20Testing/broshare Statics Report.xlsx";
}
}
}
doc.Save(MyDir + @"16.3.0.docx");
Hope, this helps.
Best regards,