Remove hyperlinks including mouse over text

I would like to find all the hyperlinks in a Word document and remove them, including any mouse-over text.

If the hyperlink is in the format “Aspose”, the hyperlink should be removed, leaving “Aspose” as plain text in the document.

If the hyperlink is in the format “https://www.aspose.words.com”, the hyperlink should be removed, leaving the URL “https://www.aspose.words.com” as plain text.

@naveenVdas

To remove hyperlinks from a Word document, including any mouse-over text, you can utilize the Aspose.Words library. Below is a code example that demonstrates how to find and remove hyperlinks while preserving the display text.

C# Example

using System;
using System.Linq;
using Aspose.Words;
using Aspose.Words.Fields;

class Program
{
    static void Main()
    {
        // Load the document
        Document doc = new Document("path/to/your/document.docx");

        // Find all FieldStart nodes in the document
        NodeList fieldStarts = doc.SelectNodes("//FieldStart");

        foreach (FieldStart fieldStart in fieldStarts.OfType<FieldStart>())
        {
            if (fieldStart.FieldType == FieldType.FieldHyperlink)
            {
                Hyperlink hyperlink = new Hyperlink(fieldStart);

                // Check if the hyperlink is not local (i.e., it has a URL)
                if (!hyperlink.IsLocal)
                {
                    // Get the display name of the hyperlink
                    string displayName = hyperlink.Name;

                    // Remove the hyperlink field
                    fieldStart.Remove();

                    // Insert the display name as plain text
                    DocumentBuilder builder = new DocumentBuilder(doc);
                    builder.MoveTo(fieldStart.ParentNode);
                    builder.Write(displayName);
                }
            }
        }

        // Save the modified document
        doc.Save("path/to/your/modified_document.docx");
    }
}

Java Example

import com.aspose.words.*;

public class RemoveHyperlinks {
    public static void main(String[] args) throws Exception {
        // Load the document
        Document doc = new Document("path/to/your/document.docx");

        // Find all FieldStart nodes in the document
        NodeList fieldStarts = doc.selectNodes("//FieldStart");

        for (FieldStart fieldStart : (Iterable<FieldStart>) fieldStarts) {
            if (fieldStart.getFieldType() == FieldType.FIELD_HYPERLINK) {
                Hyperlink hyperlink = new Hyperlink(fieldStart);

                // Check if the hyperlink is not local (i.e., it has a URL)
                if (!hyperlink.isLocal()) {
                    // Get the display name of the hyperlink
                    String displayName = hyperlink.getName();

                    // Remove the hyperlink field
                    fieldStart.remove();

                    // Insert the display name as plain text
                    DocumentBuilder builder = new DocumentBuilder(doc);
                    builder.moveTo(fieldStart.getParentNode());
                    builder.write(displayName);
                }
            }
        }

        // Save the modified document
        doc.save("path/to/your/modified_document.docx");
    }
}

Explanation

  1. Load the Document: The document is loaded from a specified path.
  2. Find Hyperlinks: The code searches for all hyperlink fields in the document.
  3. Remove Hyperlinks: For each hyperlink found, it checks if it is not a local link. If it’s a URL, it removes the hyperlink field and inserts the display name as plain text.
  4. Save the Document: Finally, the modified document is saved to a new file.

This approach ensures that all hyperlinks are removed while retaining the text that was displayed as the hyperlink.

For more details on working with hyperlinks in Aspose.Words, you can refer to the official documentation and examples available on the Aspose website.

Sources:
[1]: Autolink | Aspose.Words Document Processing API
[2]: ExReplaceHyperlinks.cs
[3]: ExReplaceHyperlinks.java

@naveenVdas You can use the following code to achieve this:

Document doc = new Document(@"C:\Temp\in.docx");
            
doc.Range.Fields.Where(f => f.Type == FieldType.FieldHyperlink).ToList()
    .ForEach(f => f.Unlink());

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

@alexey.noskov, while the hyperlinks are being removed, the text retains a blue color and an underline, which still gives it the appearance of hyperlinks.

@naveenVdas Try to modify the code like this:

Document doc = new Document(@"C:\Temp\in.docx");
            
doc.Range.Fields.Where(f => f.Type == FieldType.FieldHyperlink).ToList()
    .ForEach(f => f.Unlink());

doc.GetChildNodes(NodeType.Run, true).Cast<Run>().Where(r => r.Font.StyleIdentifier == StyleIdentifier.Hyperlink).ToList()
    .ForEach(r => r.Font.StyleIdentifier = StyleIdentifier.Normal);

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

Hi @alexey.noskov,

I’m encountering an issue when setting the StyleIdentifier in this line:

doc.GetChildNodes(NodeType.Run, true).Cast<Run>()
    .Where(r => r.Font.StyleIdentifier == StyleIdentifier.Hyperlink)
    .ToList()
    .ForEach(r => r.Font.StyleIdentifier = StyleIdentifier.Normal);

The error says the style is not a character style. Could you help with this?

@naveenVdas Modify code like this:

doc.GetChildNodes(NodeType.Run, true).Cast<Run>()
    .Where(r => r.Font.StyleIdentifier == StyleIdentifier.Hyperlink)
    .ToList()
    .ForEach(r => r.Font.ClearFormatting());