Hi Inessa,
Thanks for your request. You should just remove field code, field start, field separator and field end. Please try using the following code:
[VB]
Dim doc As Document = New Document("C:\Temp\in.doc")
BreakHyperlinks(doc)
doc.Save("C:\Temp\out.doc")
'''
''' Method breaks hyperlink in the document.
''' Hyperlinks in the word document is HYPERLINK fields.
''' Fields looks like the following [FieldStart]field code[FieldSeparator]field value[FieldEnd]
'''
''' Input document
Private Sub BreakHyperlinks(ByVal doc As Document)
'Get collection of FieldStart nodes in the document
Dim starts As NodeCollection = doc.GetChildNodes(NodeType.FieldStart, True)
'Create temporary arraylist where we will store FieldStart nodes of Hyperlinks
'After processing all these nodes will be removed
Dim hyperlinkStarts As ArrayList = New ArrayList()
'Loop through all FieldStart Nodes and search for Hyperlink field
For Each start As FieldStart In starts
If (start.FieldType = FieldType.FieldHyperlink) Then
hyperlinkStarts.Add(start)
Dim currentNode As Node = start.NextSibling
Dim fieldSeparator As Node = Nothing
'Remove all nodes between FieldStart and FieldSeparator nodes
'Field code will be removed
While (Not currentNode.NodeType.Equals(NodeType.FieldSeparator))
currentNode = currentNode.NextSibling
currentNode.PreviousSibling.Remove()
End While
'Look for Field end node
fieldSeparator = currentNode
While (Not currentNode.NodeType.Equals(NodeType.FieldEnd))
If (currentNode.NodeType.Equals(NodeType.Run)) Then
With DirectCast(currentNode, Run)
.Font.Color = Color.Black
.Font.Underline = Underline.None
End With
End If
currentNode = currentNode.NextSibling
End While
'Remove FieldSeparator and FieldEnd nodes
fieldSeparator.Remove()
currentNode.Remove()
End If
Next
'Now we can remove FieldStart nodes of Hyperlink fields
For Each start As FieldStart In hyperlinkStarts
start.Remove()
Next
End Sub
[C#]
Document doc = new Document(@"Test202\in.doc");
BreakHyperlinks(doc);
doc.Save(@"Test202\out.doc");
///
/// Method breaks hyperlink in the document.
/// Hyperlinks in the word document is HYPERLINK fields.
/// Fields looks like the following [FieldStart]field code[FieldSeparator]field value[FieldEnd]
///
/// Input document
private void BreakHyperlinks(Document doc)
{
// Get collection of FieldStart nodes in the document
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
// Create temporary arraylist where we will store FieldStart nodes of Hyperlinks
// After processing all these nodes will be removed
ArrayList hyperlinkStarts = new ArrayList();
// Loop through all FieldStart Nodes and search for Hyperlink field
foreach (FieldStart start in starts)
{
if (start.FieldType == FieldType.FieldHyperlink)
{
hyperlinkStarts.Add(start);
Node currentNode = start.NextSibling;
Node fieldSeparator = null;
// Remove all nodes between FieldStart and FieldSeparator nodes
// Field code will be removed
while (currentNode.NodeType != NodeType.FieldSeparator)
{
currentNode = currentNode.NextSibling;
currentNode.PreviousSibling.Remove();
}
// Look for Field end node
fieldSeparator = currentNode;
while (currentNode.NodeType != NodeType.FieldEnd)
{
if (currentNode.NodeType.Equals(NodeType.Run))
{
(currentNode as Run).Font.Color = Color.Black;
(currentNode as Run).Font.Underline = Underline.None;
}
currentNode = currentNode.NextSibling;
}
// Remove FieldSeparator and FieldEnd nodes
fieldSeparator.Remove();
currentNode.Remove();
}
}
// Now we can remove FieldStart nodes of Hyperlink fields
foreach (FieldStart start in hyperlinkStarts)
{
start.Remove();
}
}
Hope this could help you.
Best regards,