Adding Hyperlinks to Word Documents in Java

Hyperlinks in Word documents enable navigation from text or images to external locations. This guide demonstrates how to insert both text and image hyperlinks using Java.

Dependancy Setup

Option 1: Download the libray package, extract it, and add the Spire.Doc.jar file from the lib folder to your project classpath.

Option 2: Configure Maven to install the dependancy:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>2.7.3</version>
    </dependency>
</dependencies>

Implementation Code

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.documents.ParagraphStyle;
import com.spire.doc.fields.DocPicture;

public class AddHyperlinks {

    public static void main(String[] args) {
        // Initialize document
        Document document = new Document();
        Section currentSection = document.addSection();

        // Web hyperlink
        Paragraph para = currentSection.addParagraph();
        para.appendText("Website Link: ");
        para.appendHyperlink("https://www.google.com/", "Google Home", HyperlinkType.Web_Link);

        // Email hyperlink
        para = currentSection.addParagraph();
        para.appendText("Email Link: ");
        para.appendHyperlink("mailto:test@example.com", "test@example.com", HyperlinkType.E_Mail_Link);

        // File hyperlink
        para = currentSection.addParagraph();
        para.appendText("File Link: ");
        String pdfPath = "C:\\Documents\\sample.pdf";
        para.appendHyperlink(pdfPath, "Open PDF", HyperlinkType.File_Link);

        // Image hyperlink
        para = currentSection.addParagraph();
        para.appendText("Image Link:");
        para = currentSection.addParagraph();
        DocPicture image = para.appendPicture("C:\\Images\\logo.png");
        para.appendHyperlink("https://www.google.com/", image, HyperlinkType.Web_Link);

        // Apply styling
        ParagraphStyle textStyle = new ParagraphStyle(document);
        textStyle.setName("customStyle");
        textStyle.getCharacterFormat().setFontName("SimSun");
        document.getStyles().add(textStyle);

        for (int i = 0; i < currentSection.getParagraphs().getCount(); i++) {
            currentSection.getParagraphs().get(i).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            currentSection.getParagraphs().get(i).getFormat().setAfterAutoSpacing(true);
            currentSection.getParagraphs().get(i).applyStyle(textStyle.getName());
        }

        // Export document
        document.saveToFile("HyperlinkDocument.docx", FileFormat.Docx_2013);
    }
}

Link Types Supported

Type Description Use Case
Web_Link Navigates to URL External websites
E_Mail_Link Opens email client Contact links
File_Link Opens local file Reference documents

The code adds multiple hyperlink types to a Word document and applies consistent styling. The appendHyperlink method accepts the target address, display text, and hyperlink type as parameters.

Tags: java Word Hyperlink Spire.Doc Document Generation

Posted on Fri, 29 May 2026 18:33:29 +0000 by Aurasia