Hyperlinks in Word documents are usually displayed blue with an underline. Actually, if you don't like the default style of hyperlinks, you can change the color of hyperlinks or remove the underline to make your own hyperlink style while keeping the link. This article shows how to change the color or remove the underline of hyperlinks in Word by programming using Spire.Doc for Java.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>13.5.3</version> </dependency> </dependencies>
Change the Color of a Hyperlink or Remove the Underline of a Hyperlink in a Word Document
The detailed steps are as follows:
- Create an object of Document class.
- Add a section to the document using Document.addSection() method.
- Add a paragraph to the section using Section.addParagraph() method.
- Insert a normal hyperlink using Paragraph.appendHyperlink() method.
- Add a paragraph and a hyperlink, and then change the color of the hyperlink text to red using TextRange.getCharacterFormat().setTextColor() method.
- Add a paragraph and a hyperlink, and then remove the underline of the hyperlink using TextRange.getCharacterFormat().setUnderlineStyle() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.*; import com.spire.doc.fields.TextRange; import java.awt.*; public class ChangeHyperlink { 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("A regular hyperlink: "); //Insert a hyperlink TextRange textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link); textRange.getCharacterFormat().setFontName("Times New Roman"); textRange.getCharacterFormat().setFontSize(12f); para.appendBreak(BreakType.Line_Break); //Add a paragraph para = section.addParagraph(); para.appendText("Change the color of the hyperlink: "); //Insert a hyperlink textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link); textRange.getCharacterFormat().setFontName("Times New Roman"); textRange.getCharacterFormat().setFontSize(12f); //Change the color of the hyperlink to red textRange.getCharacterFormat().setTextColor(Color.RED); para.appendBreak(BreakType.Line_Break); //Add a paragraph para = section.addParagraph(); para.appendText("Remove the underline of the hyperlink: "); //Insert a hyperlink textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link); textRange.getCharacterFormat().setFontName("Times New Roman"); textRange.getCharacterFormat().setFontSize(12f); //Remove the underline of the hyperlink textRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None); //Save the document document.saveToFile("ChangeHyperlink.docx", FileFormat.Docx_2013); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.