0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# Wordで脚注と文末脚注を挿入

Last updated at Posted at 2020-11-13

Wordで記事を執筆する時、ある文章の内容について、補足説明する必要があるなら、脚注機能を使うことがあります。例えば、

f:id:lendoris:20201111113925p:plain


こういうふうに使いますね。レポートや論文でたくさん使っている機能だから、皆さんもう慣れていらっしゃるでしょう?今回はWordで脚注と文末脚注を挿入する方法を紹介します。

閑話休題、本題に入りましょう!

下準備

1.E-iceblueの公式サイトからFree Spire.Doc for .NET無料版をダウンロードしてください。

f:id:lendoris:20201027114413p:plain



2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいDoc.dllを参照に追加してください。

Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→Spire.Doc.dll”というようです。)

 

f:id:lendoris:20200922164301p:plain

元のファイル

 

f:id:lendoris:20201111113930p:plain

サンプルコード

```c# using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing;

namespace ConsoleApplication24
{
class Program
{
static void Main(string[] args)
{
// Word objectを作成し、ドキュメントをロードします。
Document document = new Document();
document.LoadFromFile(@"テキスト.docx", FileFormat.Docx2010);

        //初めの段落を取得します。
        Paragraph paragraph = document.Sections[0].Paragraphs[0];

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

        //初めの段落で"夏時間"という文字列を探し、脚注に追加します。
        DocumentObject obj = null;

        for (int i = 0; i < paragraph.ChildObjects.Count; i++)
        {
            obj = paragraph.ChildObjects[i];
            if (obj.DocumentObjectType == DocumentObjectType.TextRange)
            {
                TextRange textRange = obj as TextRange;

                if (textRange.Text == "夏時間")
                {
                    // String文字列の書式を設定します。
                    textRange.CharacterFormat.Bold = true;
                    //脚注を挿入します。
                    paragraph.ChildObjects.Insert(i + 1, footnote);
                    break;
                }
            }
        }

        //脚注の内容を追加し、文字のフォントなどを設定します。
        TextRange text = footnote.TextBody.AddParagraph().AppendText("夏時間。カナダ、オーストラリアでも用いる)とは1年のうち夏を中心とする時期に太陽が出ている時間帯を有効に利用する目的で、標準時を1時間進める制度またはその進められた時刻のこと。ただし、オーストラリアのロード・ハウ島では夏時間と通常の時間の差が30分であるなど一律ではない。");
        text.CharacterFormat.FontName = "Arial Black";
        text.CharacterFormat.FontSize = 10;
        text.CharacterFormat.TextColor = Color.DarkGray;
        footnote.MarkerCharacterFormat.FontName = "Calibri";
        footnote.MarkerCharacterFormat.FontSize = 12;
        footnote.MarkerCharacterFormat.Bold = true;
        footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;


        //三つ目の段落を取得します。
        Paragraph paragraph2 = document.Sections[0].Paragraphs[2];

        //文末脚注を挿入し、スタイルを設定します。
        Footnote endnote = paragraph2.AppendFootnote(FootnoteType.Endnote);

        TextRange text2 = endnote.TextBody.AddParagraph().AppendText("出典元:Wikipedia");
        text2.CharacterFormat.FontName = "Arial Black";
        text2.CharacterFormat.FontSize = 10;
        text2.CharacterFormat.TextColor = Color.DarkGray;
        endnote.MarkerCharacterFormat.FontName = "Calibri";
        endnote.MarkerCharacterFormat.FontSize = 12;
        endnote.MarkerCharacterFormat.Bold = true;
        endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen;

        //保存します。
        document.SaveToFile("脚注.docx", FileFormat.Docx2010);
    }
}

}

<h4><strong> できました!</strong></h4>
<p> </p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20201111/20201111113938.png" alt="f:id:lendoris:20201111113938p:plain" title="f:id:lendoris:20201111113938p:plain" class="hatena-fotolife" itemprop="image" /></p>
<p> </p>
<p> </p>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?