Java adds hyperlinks to Word documents

In general, when editing a Word document, inserting hyperlinks in specific text or images allows users to quickly jump to other target locations or web pages. In today's article, I'll show you how to use Free Spire.Doc for Java to add text and image hyperlinks to your Word documents.

** Import JAR package ** ** Method 1: ** Download Free Spire.Doc for Java, unzip it, and in the lib folder Import the Spire.Doc.jar package into your Java application as a dependency. ** Method 2: ** Install the JAR package via the Maven repository and configure the pom.xml file as follows:

<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>

** Java 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.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:[email protected]", "[email protected]", HyperlinkType.E_Mail_Link);

        //Insert a 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 hyperlink: ");
        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++) {

            //Center paragraph
            section.getParagraphs().get(i).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            //Automatically add spaces at the end of paragraphs
            section.getParagraphs().get(i).getFormat().setAfterAutoSpacing(true);

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

link.png

Recommended Posts

Java adds hyperlinks to Word documents
Add footnotes to Word documents in Java
Merge Java Word documents
Java adds and removes watermarks in word documents
Java adds page numbers to existing PDF documents
Java adds table to PDF
Java applications convert Word (DOC / DOCX) documents to PDF
Add multi-letter watermark to Java Word
Java adds SmartArt graphics to PowerPoint
[Java] Add WordArt to Word document
Java encryption, decryption of Word documents
Java adds form fields to PDF
How to print a Java Word document
[Java] Introduction to Java
Introduction to java
Java adds a text box to PowerPoint slides
Change paragraph text color in Java Word documents
Launch Docker from Java to convert Office documents to PDF
Changes from Java 8 to Java 11
Sum from Java_1 to 100
[Java] Connect to MySQL
Kotlin's improvements to Java
From Java to Ruby !!
Java joins and splitting table cells in Word documents
Introduction to java command
Java reserved word summary
How to lower java version
[Java] How to use Map
Convert Java Powerpoint to XPS
Java to play with Function
Java --How to make JTable
How to use java Optional
New features from Java7 to Java8
How to minimize Java images
How to write java comments
Create hyperlinks in Java PowerPoint
How to use java class
Output javadoc to word file
[Java] How to use Optional ②
Connect from Java to PostgreSQL
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] Introduction to lambda expressions
[Java] How to use string.format
Shell to kill Java process
How to use Java Map
How to set Java constants
Connect to DB with Java
Connect to MySQL 8 with Java
[java] Reasons to use static
How to use Java variables
[Java] Introduction to Stream API
Java8 to start now ~ Optional ~
How to convert Java radix
[Java] Convert ArrayList to array
Java creates a Word document
Java thread to understand loosely
[Java] How to implement multithreading
[Java] How to use Optional ①
From Ineffective Java to Effective Java
How to initialize Java array