ハイパーリンクは、デジタル文書において、2つのものを結びつけるために使用する最も重要なツールです。読者が Word ドキュメント内のハイパーリンクをクリックすると、ドキュメント内の別の場所、別のファイルやウェブサイト、または新しい電子メールメッセージに移動することができます。この記事では、Spire.Doc for Java を使用して、Java で Word ドキュメントにハイパーリンクを追加する方法について紹介します。 Word 文書に段落を追加する際にハイパーリンクを追加する Word 文書で既存のテキストにハイパーリンクを挿入する Spire.Doc for Java をインストールします まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>11.4.2</version> </dependency> </dependencies> Word 文書に段落を追加する際にハイパーリンクを追加する Spire.Doc for Java では、段落内のテキストや画像に Web リンク、メールリンク、ファイルリンク、ブックマークリンクを追加する Paragraph.appendHyperlink() メソッドを提供しています。以下は、その詳細な手順です。 Document のオブジェクトを作成します。 ドキュメントにセクションと段落を追加します。 Paragraph.appendHyerplink(String link, String text, HyperlinkType type) メソッドを使用して、テキストに基づくハイパーリンクを挿入します。 Paragraph.appendPicture() メソッドを使用して、段落に画像を追加します。 Paragraph.appendHyerplink(String link, DocPicture picture, HyperlinkType type) メソッドを使用して、画像にハイパーリンクを追加します。 Document.saveToFile() メソッドを使用してドキュメントを保存します。 Java import com.spire.doc.BookmarkStart; import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.BreakType; import com.spire.doc.documents.HyperlinkType; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.DocPicture; public class AddHyperlinks { public static void main(String[] args) { //Wordドキュメントを作成する Document doc = new Document(); //セクションを追加する Section section = doc.addSection(); //段落を追加する Paragraph paragraph = section.addParagraph(); paragraph.getStyle().getCharacterFormat().setFontName("BIZ UDGothic"); paragraph.appendBreak(BreakType.Line_Break); paragraph.appendHyperlink("https://www-iceblue.com/", "ホーム ページ", HyperlinkType.Web_Link); //改行を追加する paragraph.appendBreak(BreakType.Line_Break); paragraph.appendBreak(BreakType.Line_Break); //メールリンクを追加する paragraph.appendHyperlink("mailto:このメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 ", "メールで問い合わせる。", HyperlinkType.E_Mail_Link); //改行を追加する paragraph.appendBreak(BreakType.Line_Break); paragraph.appendBreak(BreakType.Line_Break); //ファイルリンクを追加する String filePath = "C:/報告.xlsx"; paragraph.appendHyperlink(filePath, "クリックすると報告が開きます。", HyperlinkType.File_Link); //改行を追加する paragraph.appendBreak(BreakType.Line_Break); paragraph.appendBreak(BreakType.Line_Break); //別のセクションを追加し、ブックマークを作成する Section section2 = doc.addSection(); Paragraph bookmarkParagraph = section2.addParagraph(); bookmarkParagraph.appendText("ここにブックマークがあります。"); BookmarkStart start = bookmarkParagraph.appendBookmarkStart("myBookmark"); bookmarkParagraph.getItems().insert(0, start); bookmarkParagraph.appendBookmarkEnd("myBookmark"); //ブックマークにリンクする paragraph.appendHyperlink("myBookmark", "このドキュメント内の場所にジャンプします。", HyperlinkType.Bookmark); //改行を追加する paragraph.appendBreak(BreakType.Line_Break); paragraph.appendBreak(BreakType.Line_Break); //画像リンクを追加する String imagePath = "C:/ロゴ.png"; DocPicture picture = paragraph.appendPicture(imagePath); paragraph.appendHyperlink("https://www.e-iceblue.com/", picture, HyperlinkType.Web_Link); //ドキュメントを保存する doc.saveToFile("ハイパーリンクの追加.docx", FileFormat.Docx_2013); } } Word 文書で既存のテキストにハイパーリンクを挿入する ドキュメント内の既存のテキストにハイパーリンクを追加するのは、少し複雑です。まず対象となる文字列を見つけ、段落内の文字列をハイパーリンクフィールドに置き換える必要があります。以下はその手順です。 Document のオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.findAllString() メソッドを使用して、ドキュメント内の対象文字列の出現箇所をすべて検索し、特定の文字列をコレクションからそのインデックスで取得します。 その文字列が属する段落と段落内の位置を取得します。 その段落から文字列を削除します。 ハイパーリンクフィールドを作成し、その文字列がある位置に挿入します。 Document.saveToFle() メソッドを使用してドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FieldType; import com.spire.doc.FileFormat; import com.spire.doc.Hyperlink; import com.spire.doc.documents.*; import com.spire.doc.fields.Field; import com.spire.doc.fields.FieldMark; import com.spire.doc.fields.TextRange; import com.spire.doc.interfaces.IParagraphBase; import com.spire.doc.interfaces.ITextRange; import java.awt.*; public class AddHyperlinksToExistingText { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Wordドキュメントを読み込む document.loadFromFile("C:/サンプル.docx"); //ドキュメント内で「二酸化炭素」という文字列が出現する箇所をすべて探す TextSelection[] selections = document.findAllString("二酸化炭素", true, true); //最初の出現を得る TextRange range = selections[0].getAsOneRange(); //そのテキストが含まれる段落を取得する Paragraph paragraph = range.getOwnerParagraph(); //このテキストの段落内での位置を取得する int index = paragraph.getItems().indexOf(range); //このテキストを段落から削除する paragraph.getItems().remove(range); //ハイパーリンク フィールドを作成する Field field = new Field(document); field.setType(FieldType.Field_Hyperlink); Hyperlink hyperlink = new Hyperlink(field); hyperlink.setType(HyperlinkType.Web_Link); hyperlink.setUri("https://ja.wikipedia.org/wiki/%E4%BA%8C%E9%85%B8%E5%8C%96%E7%82%AD%E7%B4%A0"); paragraph.getItems().insert(index, field); //段落にフィールドマーク「start」を挿入する IParagraphBase item = document.createParagraphItem(ParagraphItemType.Field_Mark); FieldMark start = (FieldMark)item; start.setType(FieldMarkType.Field_Separator); paragraph.getItems().insert(index + 1, start); //2つのフィールドマークの間にテキスト範囲を挿入する ITextRange textRange = new TextRange(document); textRange.setText("二酸化炭素"); textRange.getCharacterFormat().setFontName(range.getCharacterFormat().getFontName()); textRange.getCharacterFormat().setFontSize(range.getCharacterFormat().getFontSize()); textRange.getCharacterFormat().setBold(range.getCharacterFormat().getBold()); textRange.getCharacterFormat().setTextColor(Color.blue); textRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.Single); paragraph.getItems().insert(index + 2, textRange); //段落にフィールドマーク「end」を挿入する IParagraphBase item2 = document.createParagraphItem(ParagraphItemType.Field_Mark); FieldMark end = (FieldMark)item2; end.setType(FieldMarkType.Field_End); paragraph.getItems().insert(index + 3, end); //ドキュメントを保存する document.saveToFile("既存のテキストにハイパーリンクの挿入.docx", FileFormat.Docx_2013); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。