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