网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Java添加超链接到Word文档

在Word文档中,超链接是指在特定文本或者图片中插入的能跳转到其他位置或网页的链接,它也是我们在编辑制作Word文档时广泛使用到的功能之一。今天这篇文章就将为大家演示如何使用Free Spire.Doc for Java在Word文档中添加文本超链接和图片超链接。

创新互联专注于五原企业网站建设,响应式网站,购物商城网站建设。五原网站建设公司,为五原等地区提供建站服务。全流程按需网站设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

Jar包导入

方法一: 下载 Free Spire.Doc for Java 包并解压缩,然后 将lib文件夹下 的Spire.Doc.jar包 作为依赖项导入 到Java应用程序中。

方法二: 通过Maven仓库安装JAR包, 配置pom.xml文件 的代码如下


    
        com.e-iceblue
        http://repo.e-iceblue.cn/repository/maven-public/
    


    
        e-iceblue
        spire.doc.free
        2.7.3
    

Java代码

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.documents.ParagraphStyle;
import com.spire.doc.fields.DocPicture;
public class InsertHyperlinks {
    public static void main(String[] args) {
        //创建Word文档
        Document doc = new Document();
        Section section = doc.addSection();
        //添加网页链接
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("网页链接:");
        paragraph.appendHyperlink("https://www.baidu.com/","主页", HyperlinkType.Web_Link);
        //添加邮箱链接
        paragraph = section.addParagraph();
        paragraph.appendText("邮箱链接:");
        paragraph.appendHyperlink("mailto:xxxxx@163.com","xxxxx@163.com", HyperlinkType.E_Mail_Link);
        //添加文档链接
        paragraph = section.addParagraph();
        paragraph.appendText("文档链接:");
        String filePath = "C:\\Users\\Administrator\\Desktop\\报表.pdf";
        paragraph.appendHyperlink(filePath,"点击打开报表", HyperlinkType.File_Link);
        //添加图片超链接
        paragraph = section.addParagraph();
        paragraph.appendText("图片链接:");
        paragraph = section.addParagraph();
        DocPicture picture = paragraph.appendPicture("C:\\Users\\Administrator\\IdeaProjects\\Spire.Doc\\logo (2).jpg");
        paragraph.appendHyperlink("https://www.baidu.com/",picture, HyperlinkType.Web_Link);
        //创建段落样式
        ParagraphStyle style1 = new ParagraphStyle(doc);
        style1.setName("style");
        style1.getCharacterFormat().setFontName("宋体");
        doc.getStyles().add(style1);
        for (int i = 0; i < section.getParagraphs().getCount(); i++) {
            //将段落居中
            section.getParagraphs().get(i).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            //段落末尾自动添加间隔
            section.getParagraphs().get(i).getFormat().setAfterAutoSpacing(true);
            //应用段落样式
            section.getParagraphs().get(i).applyStyle(style1.getName());
        }
        //保存文档
        doc.saveToFile("InsertHyperlinks.docx", FileFormat.Docx_2013);
    }
}

Java 添加超链接到 Word 文档


新闻名称:Java添加超链接到Word文档
转载注明:http://bjjierui.cn/article/gjposc.html

其他资讯