Edit Hyperlinks in Word in Java

·

2 min read

In MS Word, if you find a hyperlink directs to a page or resource that no longer exists or is inaccessible, then it's quite necessary to modify/update it to avoid an error message appearing when the link is clicked. This article will share how to programmatically edit a hyperlink in a Word document using Free Spire.Doc for Java.

Install the Library (Two Methods)

Method 1: Download the free Java library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>5.2.0</version>
   </dependency>
</dependencies>

Sample Code

Free Spire.Doc for Java allows you to modify both the address (URL) and display text of an existing hyperlink. The complete sample code is shown below.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;

import java.util.ArrayList;

public class ModifyHyperlink {
    public static void main(String[] args) {
        //Create a Document object
        Document doc = new Document();

        //Load a sample Word document
        doc.loadFromFile("E:\\Files\\Java.docx");

        //Loop through the items in the sections to find all hyperlinks
        ArrayList<Field> hyperlinks = new ArrayList<Field>();
        for (Section section : (Iterable<Section>)doc.getSections())
        {
            for (DocumentObject object :  (Iterable<DocumentObject>)section.getBody().getChildObjects())
            {
                if (object.getDocumentObjectType().equals(DocumentObjectType.Paragraph))
                {
                    Paragraph paragraph=(Paragraph)object;
                    for (DocumentObject cObject : (Iterable<DocumentObject>)paragraph.getChildObjects())
                    {
                        if (cObject.getDocumentObjectType().equals(DocumentObjectType.Field))
                        {
                            Field field = (Field)cObject;
                            if (field.getType().equals( FieldType.Field_Hyperlink))
                            {
                                hyperlinks.add(field);
                            }
                        }
                    }
                }
            }
        }

        //Modify the address (URL) of the first hyperlink
        hyperlinks.get(0).setCode("HYPERLINK \"https://docs.oracle.com/javase/tutorial/index.html\"");

        //Modify the display text of the first hyperlink
        hyperlinks.get(0).setFieldText( "Java");

        //Save the result document
        doc.saveToFile("ModifyHyperlink.docx", FileFormat.Docx);
    }
}