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);
}
}
Author And Source
この問題について(Java Word文書でハイパーリンクの挿入・読み取り), 我々は、より多くの情報をここで見つけました https://qiita.com/iceblue/items/dbec5253681477d437cf著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .