· 

Java - Add Hyperlinks to Text or Images in Word

A hyperlink in a Microsoft Word document enables readers to jump from the link to a website, an external file, an email address, or a different location within the same document. In this article, I am going to introduce how to create various types of hyperlinks based on text or images using Free Spire.Doc for Java.

Installing Spire.Doc.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<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>3.9.0</version>
    </
dependency>
</
dependencies>

Example 1. Add a link to text

import com.spire.doc.BookmarkStart;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HyperlinkType;
import com.spire.doc.documents.Paragraph;

public class AddLinkToText {

   
public static void main(String[] args) {

       
//Create a Document object
       
Document doc = new Document();
       
       
//Add a section
       
Section section1 = doc.addSection();

       
//Link to a website
       
Paragraph paragraph = section1.addParagraph();
        paragraph.appendText(
"Link to a website: ");
        paragraph.appendHyperlink(
"https://www.google.com/","Our Website", HyperlinkType.Web_Link);

        
//Link to an email address
       
paragraph = section1.addParagraph();
        paragraph.appendText(
"Link to an email address: ");
        paragraph.appendHyperlink(
"mailto:support@google.com","Contact Us", HyperlinkType.E_Mail_Link);

       
//Link to an external file
       
paragraph = section1.addParagraph();
        paragraph.appendText(
"Link to an external file: ");
        paragraph.appendHyperlink(
"C:\\Users\\Administrator\\Desktop\\Report.pdf","Click to open a file", HyperlinkType.File_Link);
       
       
//Add second section
       
Section section2 = doc.addSection();
       
       
//Insert a paragraph in section 2 and add a bookmark named "myBookmark" to it
       
Paragraph bookmarkParagrapg = section2.addParagraph();
        
bookmarkParagrapg.appendText("Here is a bookmark");
       
BookmarkStart start = bookmarkParagrapg.appendBookmarkStart("myBookmark");
       
bookmarkParagrapg.getItems().insert(0,start);
       
bookmarkParagrapg.appendBookmarkEnd("myBookmark");
        
       
//Link to the bookmark
       
paragraph = section1.addParagraph();
        paragraph.appendText(
"Link to a bookmark: ");
        paragraph.appendHyperlink(
"myBookmark","Jump to a location in this document", HyperlinkType.Bookmark);

       
//Save to file
       
doc.saveToFile("output/AddLinkToText.docx", FileFormat.Docx_2013
);
    }
}

Example 2. Add a link to an image

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

public class AddLinkToImage {

   
public static void main(String[] args) {

       
//Create a Document object
       
Document doc = new Document();

       
//Add a section
       
Section section = doc.addSection();

       
//Link to an image
       
Paragraph paragraph = section.addParagraph();
        paragraph =
section.addParagraph();
       
DocPicture picture = paragraph.appendPicture("C:\\Users\\Administrator\\Desktop\\logo.png");
        paragraph.appendHyperlink(
"https://www.google.com/",picture, HyperlinkType.Web_Link);

       
//Save to file
       
doc.saveToFile("output/AddLinkToImage.docx", FileFormat.Docx_2013);
    }
}

Write a comment

Comments: 0