How to remove the hyperlinks from the word document in C#

With the help of Spire.Doc, we can easily add the hyperlinks and edit the hyperlinks on the word document. This article will focus on demonstrate how to remove all the hyperlinks from the word document in C#.

Here comes to the details of how to remove the hyperlinks from the word document. Firstly, view the sample document with many hyperlinks:

How to remove the hyperlinks from the word document in C#

Step 1: Create a new word document and load the sample document from file:

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Create a method FindAllHyperlinks() to get all the hyperlinks from the sample document.

private List FindAllHyperlinks(Document document)
 {
     List hyperlinks = new List();
     foreach (Section section in document.Sections)
     {
         foreach (DocumentObject sec in section.Body.ChildObjects)
         {
             if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
             {
                 foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                 {
                     if (para.DocumentObjectType == DocumentObjectType.Field)
                     {
                         Field field = para as Field;

                         if (field.Type == FieldType.FieldHyperlink)
                         {
                             hyperlinks.Add(field);
                         }
                     }
                 }
             } 
         }
     }
     return hyperlinks;
 }

Step 3: Remove the font color and underline format of the hyperlinks.

private void FormatFieldResultText(Body ownerBody,int sepOwnerParaIndex,int endOwnerParaIndex,int sepIndex,int endIndex)
{
    for (int i = sepOwnerParaIndex; i <= endOwnerParaIndex; i++)
    {
        Paragraph para = ownerBody.ChildObjects[i] as Paragraph;
        if (i == sepOwnerParaIndex && i == endOwnerParaIndex)
        {
            for (int j = sepIndex + 1; j < endIndex; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }
            
        }
        else if (i == sepOwnerParaIndex)
        {
            for (int j = sepIndex + 1; j < para .ChildObjects .Count ; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }
        }
        else if (i == endOwnerParaIndex)
        {
            for (int j = 0; j < endIndex; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }
        }
        else
        {
            for (int j = 0; j < para.ChildObjects.Count; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }
        }
    }
}
private void FormatText(TextRange tr)
{
    tr.CharacterFormat.TextColor = Color.Black;
    tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
}

Step 4: Flatten the hyperlink field.

private void FlattenHyperlinks(Field field)
{
    int ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.OwnerParagraph);
    int fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field);
    Paragraph sepOwnerPara = field.Separator.OwnerParagraph;
    int sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.Separator.OwnerParagraph);
    int sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf(field.Separator);
    int endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End);
    int endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.End.OwnerParagraph);

    FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex);

    field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex);

    for (int i = sepOwnerParaIndex; i >= ownerParaIndex; i--)
    {
        if (i == sepOwnerParaIndex && i == ownerParaIndex)
        {
            for (int j = sepIndex; j >= fieldIndex; j--)
            {
                field.OwnerParagraph.ChildObjects.RemoveAt(j);

            }
        }
        else if (i == ownerParaIndex)
        {
            for (int j = field.OwnerParagraph.ChildObjects.Count - 1; j >= fieldIndex; j--)
            {
                field.OwnerParagraph.ChildObjects.RemoveAt(j);
            }

        }
        else if (i == sepOwnerParaIndex)
        {
            for (int j = sepIndex; j >= 0; j--)
            {
                sepOwnerPara.ChildObjects.RemoveAt(j);
            }
        }
        else
        {
            field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i);
        }
    }
}

Step 5: Save the document to file.

doc.SaveToFile("removehyperlinks.docx", FileFormat.Docx);

Effective screenshot after remove the hyperlinks from the word document:

How to remove the hyperlinks from the word document in C#

Full codes of how to remove the hyperlinks from word document:

public Removehyperlink()
{
    Document doc = new Document();
    doc.LoadFromFile("Sample.docx");

    List hyperlinks = FindAllHyperlinks(doc);

    for (int i = hyperlinks.Count - 1; i >= 0; i--)
    {
        FlattenHyperlinks(hyperlinks[i]);
    }

    doc.SaveToFile("removehyperlinks.docx", FileFormat.Docx);

}
private List FindAllHyperlinks(Document document)
        {
            List hyperlinks = new List();
            foreach (Section section in document.Sections)
            {
                foreach (DocumentObject sec in section.Body.ChildObjects)
                {
                    if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                        {
                            if (para.DocumentObjectType == DocumentObjectType.Field)
                            {
                                Field field = para as Field;

                                if (field.Type == FieldType.FieldHyperlink)
                                {
                                    hyperlinks.Add(field);
                                }
                            }
                        }
                    }
                }
            }
            return hyperlinks;
        }

private void FlattenHyperlinks(Field field)
{
    int ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.OwnerParagraph);
    int fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field);
    Paragraph sepOwnerPara = field.Separator.OwnerParagraph;
    int sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.Separator.OwnerParagraph);
    int sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf(field.Separator);
    int endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End);
    int endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.End.OwnerParagraph);

    FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex);

    field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex);

    for (int i = sepOwnerParaIndex; i >= ownerParaIndex; i--)
    {
        if (i == sepOwnerParaIndex && i == ownerParaIndex)
        {
            for (int j = sepIndex; j >= fieldIndex; j--)
            {
                field.OwnerParagraph.ChildObjects.RemoveAt(j);

            }
        }
        else if (i == ownerParaIndex)
        {
            for (int j = field.OwnerParagraph.ChildObjects.Count - 1; j >= fieldIndex; j--)
            {
                field.OwnerParagraph.ChildObjects.RemoveAt(j);
            }

        }
        else if (i == sepOwnerParaIndex)
        {
            for (int j = sepIndex; j >= 0; j--)
            {
                sepOwnerPara.ChildObjects.RemoveAt(j);
            }
        }
        else
        {
            field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i);
        }
    }
}

private void FormatFieldResultText(Body ownerBody, int sepOwnerParaIndex, int endOwnerParaIndex, int sepIndex, int endIndex)
{
    for (int i = sepOwnerParaIndex; i <= endOwnerParaIndex; i++)
    {
        Paragraph para = ownerBody.ChildObjects[i] as Paragraph;
        if (i == sepOwnerParaIndex && i == endOwnerParaIndex)
        {
            for (int j = sepIndex + 1; j < endIndex; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }

        }
        else if (i == sepOwnerParaIndex)
        {
            for (int j = sepIndex + 1; j < para.ChildObjects.Count; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }
        }
        else if (i == endOwnerParaIndex)
        {
            for (int j = 0; j < endIndex; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }
        }
        else
        {
            for (int j = 0; j < para.ChildObjects.Count; j++)
            {
                FormatText(para.ChildObjects[j] as TextRange);
            }
        }
    }
}
private void FormatText(TextRange tr)
{
    tr.CharacterFormat.TextColor = Color.Black;
    tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
}