チュートリアル

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

チュートリアル».NET»Spire.Doc for .NET»ハイパーリンク»C#:Word 文書からハイパーリンクを削除する方法
2024-08-14

C#:Word 文書からハイパーリンクを削除する方法

Word 文書のハイパーリンクは、読者がウェブサイトや他の文書に移動できるクリック可能なリンクです。ハイパーリンクは有用な追加情報を提供することがありますが、場合によっては注意を逸らしたり、不要に煩わしいと感じることもあります。そのため、ハイパーリンクを削除したい場合があります。この記事では、C# を使用して、Spire.Doc for .NETWord ドキュメントからハイパーリンクを削除する方法を説明します。

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

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

PM> Install-Package Spire.Doc

Word 文書からすべてのハイパーリンクを削除

Word文書内のすべてのハイパーリンクを一度に削除するには、まず文書内のすべてのハイパーリンクを見つけ、それらを一括して処理するカスタムメソッド FlattenHyperlinks() を作成する必要があります。以下はその詳細な手順です。

  • Document オブジェクトを作成します。
  • Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。
  • カスタムメソッド FindAllHyperlinks() を使用して、文書内のすべてのハイパーリンクを見つけます。
  • ハイパーリンクをループして、カスタムメソッド FlattenHyperlinks() を使用してすべてのリンクを一括処理します。
  • Document.SaveToFile() メソッドを使用して文書を保存します。
  • C#
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace removeWordHyperlink
{
    class Program
    {
        static void Main(string[] args)
        {
            // ドキュメントインスタンスを作成
            Document doc = new Document();

            // サンプルのWordドキュメントを読み込む
            doc.LoadFromFile("Sample.docx");

            // すべてのハイパーリンクを探す
            List<Field> hyperlinks = FindAllHyperlinks(doc);

            // すべてのハイパーリンクをフラット化する
            for (int i = hyperlinks.Count - 1; i >= 0; i--)
            {
                FlattenHyperlinks(hyperlinks[i]);
            }

            // 結果のドキュメントを保存する
            doc.SaveToFile("output/RemoveHyperlinks.docx", FileFormat.Docx);

        }

        // サンプルドキュメントからすべてのハイパーリンクを取得するためのFindAllHyperlinks()メソッドを作成
        private static List<Field> FindAllHyperlinks(Document document)
        {
            List<Field> hyperlinks = new List<Field>();

            // セクション内の項目を繰り返し処理して、すべてのハイパーリンクを探す
            foreach (Section section in document.Sections)
            {
                foreach (DocumentObject sec in section.Body.ChildObjects)
                {
                    if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                        {
                            if (para.DocumentObjectType == DocumentObjectType.Field)
                            {
                                Field field = para as Field;

                                if (field.Type == FieldType.FieldHyperlink)
                                {
                                    hyperlinks.Add(field);
                                }
                            }
                        }
                    }
                }
            }
            return hyperlinks;
        }

        // ハイパーリンクフィールドをフラット化するためのFlattenHyperlinks()メソッドを作成
        private static void FlattenHyperlinks(Field field)
        {
            int ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.OwnerParagraph);
            int fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field);
            Paragraph sepOwnerPara = field.Separator.OwnerParagraph;
            int sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.Separator.OwnerParagraph);
            int sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf(field.Separator);
            int endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End);
            int endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(field.End.OwnerParagraph);
            FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex);

            field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex);

            for (int i = sepOwnerParaIndex; i >= ownerParaIndex; i--)
            {
                if (i == sepOwnerParaIndex && i == ownerParaIndex)
                {
                    for (int j = sepIndex; j >= fieldIndex; j--)
                    {
                        field.OwnerParagraph.ChildObjects.RemoveAt(j);
                    }
                }
                else if (i == ownerParaIndex)
                {
                    for (int j = field.OwnerParagraph.ChildObjects.Count - 1; j >= fieldIndex; j--)
                    {
                        field.OwnerParagraph.ChildObjects.RemoveAt(j);
                    }
                }
                else if (i == sepOwnerParaIndex)
                {
                    for (int j = sepIndex; j >= 0; j--)
                    {
                        sepOwnerPara.ChildObjects.RemoveAt(j);
                    }
                }
                else
                {
                    field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i);
                }
            }
        }

        // ハイパーリンクのフォントカラーと下線フォーマットを削除するためのFormatFieldResultText()メソッドを作成
        private static void FormatFieldResultText(Body ownerBody, int sepOwnerParaIndex, int endOwnerParaIndex, int sepIndex, int endIndex)
        {
            for (int i = sepOwnerParaIndex; i <= endOwnerParaIndex; i++)
            {
                Paragraph para = ownerBody.ChildObjects[i] as Paragraph;
                if (i == sepOwnerParaIndex && i == endOwnerParaIndex)
                {
                    for (int j = sepIndex + 1; j < endIndex; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else if (i == sepOwnerParaIndex)
                {
                    for (int j = sepIndex + 1; j < para.ChildObjects.Count; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else if (i == endOwnerParaIndex)
                {
                    for (int j = 0; j < endIndex; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
                else
                {
                    for (int j = 0; j < para.ChildObjects.Count; j++)
                    {
                        FormatText(para.ChildObjects[j] as TextRange);
                    }
                }
            }
        }

        // テキストの色を黒に変更し、下線を削除するためのFormatText()メソッドを作成
        private static void FormatText(TextRange tr)
        {
            // テキストの色を黒に設定
            tr.CharacterFormat.TextColor = Color.Black;

            // テキストの下線スタイルをなしに設定
            tr.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
        }
    }
}

C#:Word 文書からハイパーリンクを削除する方法

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

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

Read 82 times