Spire.Docは下図のように様々なフィールド操作を行うことができます。
今回はSpire.Docを使ってFieldDate(日付),FieldIf及びTOC(目次)といったフィールドの作成する方法を紹介します。
下準備
1.E-iceblueの公式サイトからFree Spire.Doc for .NET無料版をダウンロードしてください。
2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいDoc.dllを参照に追加してください。(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→Spire.Doc.dll”というようです。)
FieldDate
```C# using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields;namespace ConsoleApplication31
{
class Program
{
static void Main(string[] args)
{
//word objectを作成します。
Document document = new Document();
//sectionとparagraphを追加します。
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//paragraphにテキストを追加します。
paragraph.AppendText("今日の日付: ");
//日付フィールドを設定します。
Field field = paragraph.AppendField("Date", FieldType.FieldData) as Field;
field.Code = @"DATE \@" + "\"yyyy年MM月dd日 \"";
//保存します。
document.SaveToFile("Sample.docx", FileFormat.Docx2013);
}
}
}
<p> </p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210220/20210220121001.png" alt="f:id:lendoris:20210220121001p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong> </strong></p>
<h4><strong>FieldIf</strong></h4>
<p><span style="color: #ff5252;"><strong>IFフィールドを使うと、条件に応じて結果を分けることができます。 (IFフィールドの演算子の後はスペース必要)</strong></span></p>
<p><span style="color: #ff5252;"><strong>{IF 条件 真の場合 偽の場合}</strong></span></p>
```C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
namespace ConsoleApplication31
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//CreateFieldメソッドを使用して値を指定します。
CreateIfField(document, paragraph);
string[] fieldName = { "Count" };
string[] fieldValue = { "100" };
//Ifフィールドに値をマージします。
document.MailMerge.Execute(fieldName, fieldValue);
//フィールドを更新します。
document.IsUpdateFields = true;
//保存します
document.SaveToFile("sample.docx", FileFormat.Docx);
}
static void CreateIfField(Document document, Paragraph paragraph)
{
//Ifフィールドを追加して条件を設定します。
IfField ifField = new IfField(document);
ifField.Type = FieldType.FieldIf;
ifField.Code = "IF ";
paragraph.Items.Add(ifField);
paragraph.AppendField("Count", FieldType.FieldMergeField);
paragraph.AppendText(" > ");
paragraph.AppendText("\"60\" ");
paragraph.AppendText("\"合格\" ");
paragraph.AppendText("\"不合格\"");
IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);
(end as FieldMark).Type = FieldMarkType.FieldEnd;
paragraph.Items.Add(end);
ifField.End = end as FieldMark;
}
}
}
TOC
AppendTOC()メソッドで直接にTOCを追加できます。
using Spire.Doc;
using Spire.Doc.Documents;
namespace ConsoleApplication31
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
//TOCを追加します
paragraph.AppendTOC(1, 3);
//段落にテキストを追加します。
Paragraph para1 = section.AddParagraph();
para1.AppendText("Head1");
para1.ApplyStyle(BuiltinStyle.Heading1);
Paragraph para2 = section.AddParagraph();
para2.AppendText("Head2");
para2.ApplyStyle(BuiltinStyle.Heading2);
//TOCを更新します
document.UpdateTableOfContents();
//保存します
document.SaveToFile("TOC.docx", FileFormat.Docx);
}
}
}