超链接在 Word 文档中通常以蓝色带下划线的字体格式呈现。如果你不喜欢这种默认的超链接样式,可以更改超链接颜色或删除超链接的下划线,在把超链接设置成自己喜欢的样式的同时,保留超链接。本文将介绍如何使用 Spire.Doc for Java 以编程的方式更改超链接颜色或移除超链接下划线。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从这个链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
更改 Word 文档中超链接的颜色或删除超链接下划线
详细操作步骤如下:
- 创建 Document 类的对象。
- 用 Document.addSection() 方法在文档中添加一个节。
- 用 Section.addParagraph() 方法在节中添加一个段落。
- 用 Paragraph.appendHyperlink() 方法在段落中添加一个普通超链接。
- 添加一个段落和一个超链接,并用 TextRange.getCharacterFormat().setTextColor() 方法将超链接的颜色改为红色。
- 添加一个段落和一个超链接,并用 TextRange.getCharacterFormat().setUnderlineStyle() 方法移除超链接下划线。
- 用 Document.saveToFile() 方法保存文档。
- 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) {
        //创建 Document 类的对象
        Document document = new Document();
        //添加一个节
        Section section = document.addSection();
        //添加一个段落
        Paragraph para= section.addParagraph();
        para.appendText("普通超链接: ");
        //插入一个超链接
        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);
        //添加一个段落
        para = section.addParagraph();
        para.appendText("更改超链接颜色: ");
        //插入一个超链接
        textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
        textRange.getCharacterFormat().setFontName("Times New Roman");
        textRange.getCharacterFormat().setFontSize(12f);
        //将超链接颜色更改为红色
        textRange.getCharacterFormat().setTextColor(Color.RED);
        para.appendBreak(BreakType.Line_Break);
        //添加一个段落
        para = section.addParagraph();
        para.appendText("移除超链接下划线: ");
        //插入一个超链接
        textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
        textRange.getCharacterFormat().setFontName("Times New Roman");
        textRange.getCharacterFormat().setFontSize(12f);
        //移除超链接下划线
        textRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);
        //保存文档
        document.saveToFile("更改超链接样式.docx", FileFormat.Docx_2013);
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



