チュートリアル

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

チュートリアル».NET»Spire.Doc for .NET»脚注»C#:Word ドキュメントに脚注を挿入・削除する方法
2024-09-10

C#:Word ドキュメントに脚注を挿入・削除する方法

脚注は、ページの下部に配置される注釈です。MS Word では、脚注を使用して参考文献を引用したり、説明を追加したり、コメントを残したりすることができますが、メインテキストには影響を与えません。この記事では、Spire.Doc for .NET を使用して、C# で Word ドキュメントに脚注を挿入または削除する方法を説明します。

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

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

PM> Install-Package Spire.Doc

Word ドキュメントで段落の後に脚注を挿入

Spire.Doc for .NET の Paragraph.AppendFootnote(FootnoteType.Footnote) メソッドを使用すると、指定した段落の後に脚注を挿入できます。詳細な手順は以下の通りです。

  • Document インスタンスを作成します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • 最初のセクションを取得し、そのセクション内の指定した段落を取得します。
  • Paragraph.AppendFootnote(FootnoteType.Footnote) メソッドを使用して、段落の末尾に脚注を追加します。
  • 脚注のテキスト内容、フォント、色を設定し、脚注の上付き文字の番号の形式を設定します。
  • Document.SaveToFile() メソッドを使用して結果のドキュメントを保存します。
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace AddFootnote
{
    class Program
    {
        static void Main(string[] args)
        {
            // Documentインスタンスを作成
            Document document = new Document();

            // サンプルのWordドキュメントをロード
            document.LoadFromFile("Sample.docx");

            // 最初のセクションを取得
            Section section = document.Sections[0];

            // セクション内の指定した段落を取得
            Paragraph paragraph = section.Paragraphs[6];

            // 段落の末尾に脚注を追加
            Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);

            // 脚注のテキスト内容を設定
            TextRange text = footnote.TextBody.AddParagraph().AppendText("さらに、近代に入ると、デジタル通貨や仮想通貨の登場により、通貨の形態は大きく変化し、グローバルな取引のスピードと範囲が拡大した。");

            // テキストのフォントと色を設定
            text.CharacterFormat.FontName = "Yu Gothic UI";
            text.CharacterFormat.FontSize = 12;
            text.CharacterFormat.TextColor = Color.DarkBlue;

            // 脚注上付き番号のフォーマットを設定
            footnote.MarkerCharacterFormat.FontName = "Arial";
            footnote.MarkerCharacterFormat.FontSize = 15;
            footnote.MarkerCharacterFormat.Bold = true;
            footnote.MarkerCharacterFormat.TextColor = Color.DarkCyan;

            // 結果のドキュメントを保存
            document.SaveToFile("output/段落に脚注を追加.docx", FileFormat.Docx);
            document.Close();
        }
    }
}

C#:Word ドキュメントに脚注を挿入・削除する方法

Word ドキュメントでテキストの後に脚注を挿入

Spire.Doc for .NET を使用すると、ドキュメント内の任意の場所にある特定のテキストの後に脚注を挿入することもできます。詳細な手順は以下の通りです。

  • Document インスタンスを作成します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.FindString() メソッドを使用して、指定したテキストを検索します。
  • TextSelection.GetAsOneRange() メソッドを使用して、指定したテキストの範囲を取得します。
  • TextRange.OwnerParagraph プロパティを使用して、そのテキスト範囲がある段落を取得します。
  • Paragraph.ChildObjects.IndexOf() メソッドを使用して、段落内のテキスト範囲の位置インデックスを取得します。
  • Paragraph.AppendFootnote(FootnoteType.Footnote) メソッドを使用して脚注を追加し、Paragraph.ChildObjects.Insert() メソッドを使用して指定したテキストの後に脚注を挿入します。
  • 脚注のテキスト内容、フォント、色を設定し、脚注の上付き文字の番号の形式を設定します。
  • Document.SaveToFile() メソッドを使用して結果のドキュメントを保存します。
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertFootnote
{
    class Program
    {
        static void Main(string[] args)
        {
            // Documentインスタンスを作成
            Document document = new Document();

            // サンプルのWordドキュメントをロード
            document.LoadFromFile("Sample.docx");

            // 指定したテキスト文字列を検索
            TextSelection selection = document.FindString("通貨制度", false, true);

            // 指定したテキストの範囲を取得
            TextRange textRange = selection.GetAsOneRange();

            // テキスト範囲がある段落を取得
            Paragraph paragraph = textRange.OwnerParagraph;

            // 段落内のテキスト範囲の位置インデックスを取得
            int index = paragraph.ChildObjects.IndexOf(textRange);

            // 脚注を追加
            Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);

            // 指定した段落の後に脚注を挿入
            paragraph.ChildObjects.Insert(index + 1, footnote);

            // 脚注のテキスト内容を設定
            TextRange text = footnote.TextBody.AddParagraph().AppendText("通貨制度は、国や地域が採用する通貨の管理と流通に関する枠組みであり、これには中央銀行や政府の金融政策が含まれ、経済の安定と成長を支える役割を果たします。");

            // テキストのフォントと色を設定
            text.CharacterFormat.FontName = "Yu Gothic UI";
            text.CharacterFormat.FontSize = 12;
            text.CharacterFormat.TextColor = Color.DarkBlue;

            // 脚注上付き番号のフォーマットを設定
            footnote.MarkerCharacterFormat.FontName = "Arial";
            footnote.MarkerCharacterFormat.FontSize = 15;
            footnote.MarkerCharacterFormat.Bold = true;
            footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

            // 結果のドキュメントを保存
            document.SaveToFile("output/テキストに脚注を追加.docx", FileFormat.Docx);
            document.Close();
        }
    }
}

C#:Word ドキュメントに脚注を挿入・削除する方法

Word ドキュメントから脚注を削除

既存の脚注を手動で検索して削除するのは時間と労力がかかります。以下の手順で、すべての脚注を一度にプログラムで削除することができます。

  • Document インスタンスを作成します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.Sections プロパティを使用して、指定したセクションを取得します。
  • セクション内の各段落を巡回し、脚注を検索します。
  • Paragraph.ChildObjects.RemoveAt() メソッドを使用して脚注を削除します。
  • Document.SaveToFile() メソッドを使用して結果のドキュメントを保存します。
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace RemoveFootnote
{
    class Program
    {
        static void Main(string[] args)
        {
            // Documentインスタンスを作成
            Document document = new Document();

            // サンプルのWordドキュメントをロード
            document.LoadFromFile("output/テキストに脚注を追加.docx");

            // 最初のセクションを取得
            Section section = document.Sections[0];

            // セクション内の各段落を巡回して脚注を探す
            foreach (Paragraph para in section.Paragraphs)
            {
                int index = -1;
                for (int i = 0, cnt = para.ChildObjects.Count; i < cnt; i++)
                {
                    ParagraphBase pBase = para.ChildObjects[i] as ParagraphBase;
                    if (pBase is Footnote)
                    {
                        index = i;
                        break;
                    }
                }

                if (index > -1)

                    // 脚注を削除
                    para.ChildObjects.RemoveAt(index);
            }

            // 結果のドキュメントを保存
            document.SaveToFile("output/脚注を削除.docx", FileFormat.Docx);
            document.Close();
        }
    }
}

C#:Word ドキュメントに脚注を挿入・削除する方法

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

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

Read 30 times