Java Word文書でハイパーリンクの挿入・読み取り


ハイパーリンクを挿入することで、ワード内の文字列や図形に別の場所の情報をリンクさせることができます。今回は、Spire.Doc for Javaという使いやすいライブラリを活用して、Word文書にハイパーリンクを挿入、そしてWord文書内のハイパーリンクを読み取る方法を紹介していきます。

1.E-iceblueの公式サイトからFree Spire.doc for Java無料版をダウンロードしてください。

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.doc.jarを参照に追加してください。

 

ハイパーリンクを挿入

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.google.com/","ホームページ", HyperlinkType.Web_Link);

        //メールのリンクを追加します。
        paragraph = section.addParagraph();
        paragraph.appendText("メールのリンク:");
        paragraph.appendHyperlink("mailto:[email protected]","[email protected]", HyperlinkType.E_Mail_Link);

        //段落の形式を設定します。
        ParagraphStyle style1 = new ParagraphStyle(doc);
        style1.setName("style");
        style1.getCharacterFormat().setFontName("Ms mincho");
        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);
    }
}

実行結果

リンクを読み取る

import com.spire.doc.*;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Field;

import java.util.ArrayList;
import java.util.List;

public class ExtractHyperlinkAddress {
    public static void main(String[] args){
        //Wordファイルをロードします。
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //sectionを取得します。
        Section section = doc.getSections().get(0);

        //リンクの数列を取得します。
        List hyperlinks = new ArrayList();

        //段落をループします。
        for (Paragraph para : (Iterable) section.getParagraphs()) {
            for (DocumentObject obj:(Iterable) para.getChildObjects()) {
                if (obj.getDocumentObjectType().equals(DocumentObjectType.Field)) {
                    Field field = (Field) obj;
                    if (field.getType().equals(FieldType.Field_Hyperlink)) {
                        hyperlinks.add(field);
                    }
                }
            }
        }

        //リンクを取得します。
        String text = hyperlinks.get(0).getFieldText();
        String address = hyperlinks.get(0).getValue();
        System.out.println("リンクのテキスト:"+ text + "\n" + "リンクの位置:"+ address);
    }
}