[Java] Insert Hyperlinks in Word Documents
Tags: java, hyperlink, word
Published: 2020-07-31 07:31:38

In a Word document, a hyperlink refers to a link inserted in the specific text or picture, which enables you to jump between different text elements in a document or between different websites.  This article will demonstrate how to add a text hyperlink, an image hyperlink, an email link and a file link to a Word document by using Free Spire.Doc for Java.

**Installation**
**Method 1:** Download the [Free Spire.Doc for Java](https://www.e-iceblue.com/Download/doc-for-java-free.html) and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

**Method 2:** You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

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

**Code Snippets**

```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.HyperlinkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;

public class InsertHyperlinks {

    public static void main(String[] args) {

        //create a Word document
        Document doc = new Document();
        Section section = doc.addSection();

        //insert web link
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("Web Link: ");
        paragraph.appendHyperlink("https://www.google.com/", "Home Page", HyperlinkType.Web_Link);

        //insert email link
        paragraph = section.addParagraph();
        paragraph.appendText("Email Link: ");
        paragraph.appendHyperlink("mailto:xxx@outlook.com", "xxx@outlook.com", HyperlinkType.E_Mail_Link);

        //insert file link
        paragraph = section.addParagraph();
        paragraph.appendText("File Link: ");
        String filePath = "C:\\Users\\Administrator\\Desktop\\sample.pptx";
        paragraph.appendHyperlink(filePath, "Click to open report", HyperlinkType.File_Link);

        //insert image hyperlink
        paragraph = section.addParagraph();
        paragraph.appendText("Image Hyper Link: ");
        paragraph = section.addParagraph();
        DocPicture picture = paragraph.appendPicture("C:\\Users\\Administrator\\IdeaProjects\\Spire.Doc\\logo (2).jpg");
        paragraph.appendHyperlink("https://www.google.com/", picture, HyperlinkType.Web_Link);

        for (int i = 0; i < section.getParagraphs().getCount(); i++) {

            //align paragraph to center
            section.getParagraphs().get(i).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            //add auto spacing after paragraph
            section.getParagraphs().get(i).getFormat().setAfterAutoSpacing(true);

            //save to file
            doc.saveToFile("InsertHyperlinks.docx", FileFormat.Docx_2013);
        }
    }
}
```

![](https://dev-to-uploads.s3.amazonaws.com/i/oqc46kbck8venwftzdfe.jpg)