Free Support Forum -

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 = ""

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=]

[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

DocumentBuilder builder = new DocumentBuilder(doc);



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;


aNode = aNode.NextSibling;


StringBuilder strBuilder = new StringBuilder();

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


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


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


displayText = strBuilder.ToString();


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




// remove all nodes that are part of hyperlink fields

foreach (Node node in nodesToRemove)






{ }


I hope, this helps.

Best Regards,