Thursday, 13 August 2020 07:49

Change Hyperlink Appearance in Word in Java

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to change the appearance of hyperlinks in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.HyperlinkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.UnderlineStyle;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class ChangeHyperlinkAppearance {

    public static void main(String[] args) {

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

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

        //Add a paragraph
        Paragraph para= section.addParagraph();
        para.appendText("Regular Link: ");

        //Insert a hyperlink
        TextRange txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        para.appendBreak(BreakType.Line_Break);

        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Change Color: ");

        //Insert a hyperlink and set its color to red
        txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        txtRange.getCharacterFormat().setTextColor(Color.RED);
        para.appendBreak(BreakType.Line_Break);

        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Remove Underline: ");

        //Insert a hyperlink and remove its underline
        txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        txtRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);

        //Save the document
        document.saveToFile("output/ChangeHyperlinkAppearance.docx", FileFormat.Docx_2013);
    }
}

Change Hyperlink Appearance in Word in Java

Additional Info

  • tutorial_title: