Free Support Forum - aspose.com

Replace Links with Special Tag

I have a word document. I want to replace ALL LINKS with a special formatted
string like [display text=URL].

So for example, say I have a word document with 2 links.

Link 1: display text = "click here", URL = "http://mywebsite.com"

Link 2: display text = "Jump to Recipe", URL = "b1" where this is an internal bookmark

I want to REPLACE these with the special tags, so the final same doc would now have the tags:

[click here=http://mywebsite.com]

[Jump to Recipe=b1]

Hi Nitin,


Thanks for your inquiry. Sure, you can use the following code snippet to be able to replace all HYPERLINK fields in your document with special tags:

Document doc = new
Document(@“C:\Temp\in.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);

try

{

ArrayList nodesToRemove = new ArrayList();

string displayText;

string url;

// get all FieldStart nodes in a document

NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);

// iterate through document field starts collection

foreach (FieldStart fieldStart in fieldStarts)

{

// if field start belongs to hyperlink

if (fieldStart.FieldType == FieldType.FieldHyperlink)

{

Node fieldSeparator = null;

// then mark for removal all nodes that are between field start and field end

Node aNode = fieldStart;

while (aNode.NodeType != NodeType.FieldEnd)

{

if (aNode.NodeType == NodeType.FieldSeparator)

fieldSeparator = aNode;

nodesToRemove.Add(aNode);

aNode = aNode.NextSibling;

}

StringBuilder strBuilder = new StringBuilder();

for (Node child = fieldStart; !child.Equals(fieldSeparator); child = child.NextSibling)

strBuilder.Append(child.GetText());

url = strBuilder.ToString().Replace("HYPERLINK", "").Replace("\\l", "").Trim();

strBuilder.Clear();

for (Node child = fieldSeparator; !child.Equals(aNode); child = child.NextSibling)

strBuilder.Append(child.GetText());

displayText = strBuilder.ToString();

builder.MoveTo(fieldStart);

builder.Write("[" + displayText + "=" + url + "]");

nodesToRemove.Add(aNode);

}

}

// remove all nodes that are part of hyperlink fields

foreach (Node node in nodesToRemove)

{

node.Remove();

}

}

catch

{ }

doc.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best Regards,