Java 添加、修改Word超连接

在平常使用Word编辑文档时,有时需经过某些内容连接到其余内容,好比连接到特定的段落,图片或其余的文档,甚至是网页或邮箱地址。经过点击这些超连接,能够快速从当前文档跳转至指定的网页或打开指定的外部文件。本文将经过使用Java程序来演示如何在Word文档中添加和修改超连接。html

使用工具:Free Spire.Doc for Java(免费版)

Jar文件获取及导入:

方法1:经过官网下载获取jar包。解压后将lib文件夹下的Spire.Doc.jar文件导入Java程序。(以下图)java

安装图.png
方法2:经过maven仓库安装导入。具体安装教程详见此网页app

【示例1】添加超连接

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 InsertHyperlink {  
    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.jianshu.com/u/96431825b792","我的主页", HyperlinkType.Web_Link);  
  
  //添加邮箱连接  
  paragraph = section.addParagraph();  
        paragraph.appendText("邮箱连接:");  
        paragraph.appendHyperlink("mailto:2540321664@qq.com","2540321664@qq.com", HyperlinkType.E_Mail_Link);  
  
  //添加文档连接  
  paragraph = section.addParagraph();  
        paragraph.appendText("文档连接:");  
        String filePath = "C:\\Users\\Test1\\Desktop\\财务预算.xlsx";  
        paragraph.appendHyperlink(filePath,"点击查看财务预算", HyperlinkType.File_Link);  
  
  //添加图片超连接  
  paragraph = section.addParagraph();  
        paragraph.appendText("图片连接:");  
        paragraph = section.addParagraph();  
        DocPicture picture = paragraph.appendPicture("C:\\Users\\Test1\\Desktop\\签名.png");  
        paragraph.appendHyperlink("https://www.jianshu.com/u/96431825b792",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("output/InsertHyperlinks.docx", FileFormat.Docx_2013);  
    }  
}

添加效果:maven

效果1.png

【示例2】修改超连接

原文档以下:工具

源文档1.png

代码示例:spa

import com.spire.doc.*;  
import com.spire.doc.documents.*;  
import com.spire.doc.fields.Field;  
import java.util.ArrayList;  
import java.util.List;  
  
public class EditHyperlink {  
    public static void main(String[] args) {  
      //加载Word文档  
  Document doc = new Document();  
        doc.loadFromFile("C\\Users\\Test1\\Desktop\\Sample.docx");  
  
        List<Field> hyperlinks = new ArrayList<Field>();  
  
     //遍历文档中的节  
  for (Section section : (Iterable<? extends Section>) doc.getSections()  
        ) {  
     //遍历每一个节中的段落  
  for (Paragraph para : (Iterable<Paragraph>) section.getParagraphs()  
            ) {  
                for (DocumentObject obj:(Iterable<DocumentObject>) para.getChildObjects()  
                ) {  
     //找到超连接并将其添加至list中  
  if (obj.getDocumentObjectType().equals(DocumentObjectType.Field)) {  
                        Field field = (Field) obj;  
                        if (field.getType().equals(FieldType.Field_Hyperlink)) {  
                            hyperlinks.add(field);  
                        }  
                    }  
                }  
            }  
        }  
  
    //修改第一个超连接的展现文字和连接地址  
  hyperlinks.get(0).setCode("HYPERLINK \"https://www.zhihu.com/topic/19649017/hot");  
        hyperlinks.get(0).setFieldText("徐志摩(现代新月派表明诗人)");  
  
   //保存文档  
  doc.saveToFile("output/EditHyperlink.docx", FileFormat.Docx_2013);  
    }  
}

修改效果code

效果2.png

(本文完)orm

相关文章
相关标签/搜索