LoginSignup
1
0

C#Wordの脚注と相互参照機能を紹介します

Last updated at Posted at 2022-03-22

脚注は通常、Word文書ページの下部にあり、解釈、説明、または文書内の特定のコンテンツへの関連する参照を提供するために使用されます。同じ脚注がドキュメント内の2つの場所で参照されている場合、脚注は、2番目の引用の代わりに「相互参照」という機能を使用して、前の引用の場所にのみ挿入する必要があります。この記事では、Free Spire.DocコンポーネントとC#を使用して、脚注を挿入し、Wordで相互参照を設定する方法を説明します。

次のコードを使用する前に、Visual StudioでC#アプリケーションを作成し、Spire.Doc.dllをプロジェクトに参照する必要があります。

使用する名前空間:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

主要なコード

//ドキュメントをロードする                      
Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\Input.docx");

//脚注が必要なテキストと位置している段落を見つける
TextSelection ts = document.FindString("楕円の法則", true, true);
TextRange textrRange = ts.GetAsOneRange();
Paragraph paragraph = textrRange.OwnerParagraph;

//脚注を作成する
Footnote footnote = new Footnote(document);
footnote.FootnoteType = FootnoteType.Footnote;

//脚注のメモテキストを追加し、そのメモ参照マークアップを上付き文字としてフォーマットする
Paragraph footPar = footnote.TextBody.AddParagraph();
footPar.AppendText("ドイツの天文学者ケプラーによって提案された惑星運動の3つの法則の1つです。");
footnote.MarkerCharacterFormat.SubSuperScript = SubSuperScript.SuperScript;

//テキストの後に脚注を挿入する
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textrRange) + 1, footnote);

//ステップ2:相互参照フィールドを作成し、脚注にリンクする

//脚注のテキストの場所にブックマーク「_FootNote1」を追加する
BookmarkStart start = new BookmarkStart(document, "_FootNote1");
BookmarkEnd end = new BookmarkEnd(document, "_FootNote1");
footPar.ChildObjects.Insert(0, start);
footPar.ChildObjects.Insert(footPar.ChildObjects.Count - 1, end);

//相互参照フィールドを作成し、脚注のメモテキストにブックマークする
Field field = new Field(document);
field.Type = FieldType.FieldNoteRef;
field.Code = @" NOTEREF _FootNote1 \f \h  \* MERGEFORMAT ";

//同じ脚注とそれが含まれている段落を参照する必要があるテキストを検索する
textrRange = document.FindString("軌道法", true, false).GetAsOneRange();
paragraph = textrRange.OwnerParagraph;

//テキストの後に相互参照フィールドを挿入する
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textrRange) + 1, field);

//セパレーターを追加する
FieldMark fieldmark = new FieldMark(document, FieldMarkType.FieldSeparator);
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textrRange) + 2, fieldmark);

//テキストの後に上付き文字を追加する   
TextRange tr = new TextRange(document);
tr.Text = "1";
tr.CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textrRange) + 3, tr);

//フィールド終了タグを追加する
FieldMark fieldEnd = new FieldMark(document, FieldMarkType.FieldEnd);
paragraph.ChildObjects.Add(fieldEnd);

//ドキュメントを保存する
document.SaveToFile("脚注.docx", FileFormat.Docx);

脚注のテキストが本文の内容と一致しないと思う場合は、次のコードを使用してフォントの形式と色を設定できます。
TextRange text = footPar.AppendText("ドイツの天文学者ケプラーによって提案された惑星運動の3つの法則の1つです。");
text.CharacterFormat.FontName = "Arial Black";
text.CharacterFormat.FontSize = 10;
text.CharacterFormat.TextColor = Color.DarkGray;

結果は以下のようになります:
01.png

今回のWordの脚注と相互参照機能の紹介は以上でした、最後まで読んでいただきありがとうございます。

1
0
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
1
0