チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル».NET»Spire.Doc for .NET»ハイパーリンク»C#/VB.NET:Word ドキュメントにハイパーリンクを挿入する方法
2022-08-23

C#/VB.NET:Word ドキュメントにハイパーリンクを挿入する方法

Word ドキュメント内のハイパーリンクを使用すると、読者はドキュメントの任意の位置、ファイル、Web サイト、または電子メールにジャンプすることができます。ハイパーリンクを使用すると、読者は関連する情報へすばやく簡単に移動することができます。この記事では、Spire.Doc for .NET を使用して Word ドキュメント内のテキストや画像にハイパーリンクを挿入する方法について説明します。

Spire.Doc for .NET をインストールします

まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。

PM> Install-Package Spire.Doc

Word ドキュメントにテキストを追加しながら、ハイパーリンクを挿入する

Spire.Doc では、段落内のテキストや画像に Web リンク、メールリンク、ファイルリンク、ブックマークリンクを追加する Paragraph.AppendHyperlink() メソッドを提供しています。以下は、その詳細な手順です。

  • Document クラスのオブジェクトを作成します。
  • ドキュメントにセクションと段落を追加します。
  • Paragraph.AppendHyerplink(string link, string text, HyperlinkType type) メソッドを使用して、テキストを元にしたハイパーリンクを挿入します。
  • Paragraph.AppendPicture() メソッドを使用して、段落に画像を追加します。
  • Paragraph.AppendHyerplink(string link, Spire.Doc.Fields.DocPicture picture, HyperlinkType type) メソッドを使用して、画像に基づくハイパーリンクを挿入します。
  • Document.SaveToFile() メソッドを使用して、ドキュメントを保存します。
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace InsertHyperlinks
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Wordドキュメントを作成する
            Document doc = new Document();

            //セクションを追加する
            Section section = doc.AddSection();

            //段落を追加する
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendHyperlink("https://jp.e-iceblue.com/", "公式サイト", HyperlinkType.WebLink);

            //改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak);
            paragraph.AppendBreak(BreakType.LineBreak);

            //メールリンクを追加する
            paragraph.AppendHyperlink("mailto:support @e-iceblue.com", "メールでのお問い合わせ", HyperlinkType.EMailLink);

            //改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak);
            paragraph.AppendBreak(BreakType.LineBreak);

            //ファイルリンクを追加する
            string filePath = @"C:\レポート.xlsx";
            paragraph.AppendHyperlink(filePath, "クリックするとレポートが開きます", HyperlinkType.FileLink);

            //改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak);
            paragraph.AppendBreak(BreakType.LineBreak);

            //別のセクションを追加し、ブックマークを作成する
            Section section2 = doc.AddSection();
            Paragraph bookmarkParagrapg = section2.AddParagraph();
            bookmarkParagrapg.AppendText("これは1つのブックマークです");
            BookmarkStart start = bookmarkParagrapg.AppendBookmarkStart("私のブックマーク");
            bookmarkParagrapg.Items.Insert(0, start);
            bookmarkParagrapg.AppendBookmarkEnd("私のブックマーク");

            //ブックマークへのリンクを追加する
            paragraph.AppendHyperlink("私のブックマーク", "このドキュメントの中の特定の位置へジャンプする", HyperlinkType.Bookmark);

            //改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak);
            paragraph.AppendBreak(BreakType.LineBreak);
            paragraph.AppendBreak(BreakType.LineBreak);

            //画像に基づくハイパーリンクを追加する
            Image image = Image.FromFile(@"C:\.net.png");
            Spire.Doc.Fields.DocPicture picture = paragraph.AppendPicture(image);
            paragraph.AppendHyperlink("https://docs.microsoft.com/ja-jp/dotnet/", picture, HyperlinkType.WebLink);

            //ドキュメントを保存する
            doc.SaveToFile("ハイパーリンクの挿入.docx", FileFormat.Docx2013);
        }
    }
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing

Namespace InsertHyperlinks
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Wordドキュメントを作成する
            Dim doc As Document = New Document()

            'セクションを追加する
            Dim section As Section = doc.AddSection()

            '段落を追加する
            Dim paragraph As Paragraph = section.AddParagraph()
            paragraph.AppendHyperlink("https://jp.e-iceblue.com/", "公式サイト", HyperlinkType.WebLink)

            '改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak)
            paragraph.AppendBreak(BreakType.LineBreak)

            'メールリンクを追加する
            paragraph.AppendHyperlink("mailto:support @e-iceblue.com", "メールでのお問い合わせ", HyperlinkType.EMailLink)

            '改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak)
            paragraph.AppendBreak(BreakType.LineBreak)

            'ファイルリンクを追加する
            Dim filePath As String = "C:\レポート.xlsx"
            paragraph.AppendHyperlink(filePath, "クリックするとレポートが開きます", HyperlinkType.FileLink)

            '改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak)
            paragraph.AppendBreak(BreakType.LineBreak)

            '別のセクションを追加し、ブックマークを作成する
            Dim section2 As Section = doc.AddSection()
            Dim bookmarkParagrapg As Paragraph = section2.AddParagraph()
            bookmarkParagrapg.AppendText("これは1つのブックマークです")
            Dim start As BookmarkStart = bookmarkParagrapg.AppendBookmarkStart("私のブックマーク")
            bookmarkParagrapg.Items.Insert(0, start)
            bookmarkParagrapg.AppendBookmarkEnd("私のブックマーク")

            'ブックマークへのリンクを追加する
            paragraph.AppendHyperlink("私のブックマーク", "このドキュメントの中の特定の位置へジャンプする", HyperlinkType.Bookmark)

            '改行を追加する
            paragraph.AppendBreak(BreakType.LineBreak)
            paragraph.AppendBreak(BreakType.LineBreak)
            paragraph.AppendBreak(BreakType.LineBreak)

            '画像に基づくハイパーリンクを追加する
            Dim image As Image = Image.FromFile("C:\.net.png")
            Dim picture As Spire.Doc.Fields.DocPicture = paragraph.AppendPicture(image)
            paragraph.AppendHyperlink("https://docs.microsoft.com/ja-jp/dotnet/", picture, HyperlinkType.WebLink)

            'ドキュメントを保存する
            doc.SaveToFile("ハイパーリンクの挿入.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

C#/VB.NET:Word ドキュメントにハイパーリンクを挿入する方法

Word ドキュメント内の既存のテキストにハイパーリンクを追加する

ドキュメント内の既存のテキストにハイパーリンクを追加するのは、少し複雑です。対象となる文字列を見つけ、それをハイパーリンクフィールドに置き換える必要があります。以下はその手順です。

  • Document クラスのオブジェクトを作成します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.FindAllString() メソッドを使用してドキュメント内の対象文字列をすべて検索し、インデックスで対象文字列を取得します。
  • その文字列が存在する段落と、その段落内での位置を取得します。
  • その文字列を段落から削除します。
  • ハイパーリンクフィールドを作成し、元の文字列がある位置に挿入します。
  • Document.SaveToFle() メソッドを使用してドキュメントを保存します。
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;

namespace InsertHyperlinks
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Documentクラスのオブジェクトを作成する
            Document document = new Document();

            //Wordドキュメントを読み込む
            document.LoadFromFile(@"C:\例.docx");

            //ドキュメント内の「.NET Framework」文字列をすべて検索する
            TextSelection[] selections = document.FindAllString(".NET Framework", true, true);

            //2番目に見つかった文字列を取得する
            TextRange range = selections[1].GetAsOneRange();

            //この文字列が含まれる段落を取得する
            Paragraph parapgraph = range.OwnerParagraph;

            //段落内のこの文字列の位置を取得する
            int index = parapgraph.Items.IndexOf(range);

            //この文字列を段落から削除する
            parapgraph.Items.Remove(range);

            //ハイパーリンクフィールドを作成する
            Spire.Doc.Fields.Field field = new Spire.Doc.Fields.Field(document);
            field.Type = Spire.Doc.FieldType.FieldHyperlink;
            Hyperlink hyperlink = new Hyperlink(field);
            hyperlink.Type = HyperlinkType.WebLink;
            hyperlink.Uri = "https://ja.wikipedia.org/wiki/.NET_Framework";
            parapgraph.Items.Insert(index, field);

            //段落にフィールドマーク「start」を挿入する
            IParagraphBase start = document.CreateParagraphItem(ParagraphItemType.FieldMark);
            (start as FieldMark).Type = FieldMarkType.FieldSeparator;
            parapgraph.Items.Insert(index + 1, start);

            //2つのフィールドマークの間にテキスト範囲を挿入する
            ITextRange textRange = new Spire.Doc.Fields.TextRange(document);
            textRange.Text = ".NET Framework";
            textRange.CharacterFormat.FontName = range.CharacterFormat.FontName;
            textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
            textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
            parapgraph.Items.Insert(index + 2, textRange);

            //段落にフィールドマーク「end」を挿入する
            IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);
            (end as FieldMark).Type = FieldMarkType.FieldEnd;
            parapgraph.Items.Insert(index + 3, end);

            //ドキュメントを保存する
            document.SaveToFile("ハイパーリンクの追加.docx", Spire.Doc.FileFormat.Docx);
        }
    }
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Interface

Namespace InsertHyperlinks
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Documentクラスのオブジェクトを作成する
            Dim document As Document = New Document()

            'Wordドキュメントを読み込む
            document.LoadFromFile("C:\例.docx")

            'ドキュメント内の「.NET Framework」文字列をすべて検索する
            Dim selections() As TextSelection = document.FindAllString(".NET Framework", True, True)

            '2番目に見つかった文字列を取得する
            Dim range As TextRange = selections(1).GetAsOneRange()

            'この文字列が含まれる段落を取得する
            Dim parapgraph As Paragraph = range.OwnerParagraph

            '段落内のこの文字列の位置を取得する
            Dim index As Integer = parapgraph.Items.IndexOf(range)

            'この文字列を段落から削除する
            parapgraph.Items.Remove(range)

            'ハイパーリンクフィールドを作成する
            Dim field As Spire.Doc.Fields.Field = New Spire.Doc.Fields.Field(document)
            field.Type = Spire.Doc.FieldType.FieldHyperlink
            Dim hyperlink As Hyperlink = New Hyperlink(field)
            hyperlink.Type = HyperlinkType.WebLink
            hyperlink.Uri = "https://ja.wikipedia.org/wiki/.NET_Framework"
            parapgraph.Items.Insert(index, field)

            '段落にフィールドマーク「start」を挿入する
            Dim start As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark)
            (start as FieldMark).Type = FieldMarkType.FieldSeparator
            parapgraph.Items.Insert(index + 1, start)

            '2つのフィールドマークの間にテキスト範囲を挿入する
            Dim textRange As ITextRange = New Spire.Doc.Fields.TextRange(document)
            textRange.Text = ".NET Framework"
            textRange.CharacterFormat.FontName = range.CharacterFormat.FontName
            textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue
            textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single
            parapgraph.Items.Insert(index + 2, textRange)

            '段落にフィールドマーク「end」を挿入する
            Dim nd As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark)
            (nd as FieldMark).Type = FieldMarkType.FieldEnd
            parapgraph.Items.Insert(index + 3, nd)

            'ドキュメントを保存する
            document.SaveToFile("ハイパーリンクの追加.docx", Spire.Doc.FileFormat.Docx)
        End Sub
    End Class
End Namespace

C#/VB.NET:Word ドキュメントにハイパーリンクを挿入する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 873 times