チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
新しく作成した Word ドキュメントに目次を作成することは、文書の質を向上させ、読みやすさを改善し、情報を効果的に伝達する重要な手段です。目次は文書のナビゲーションガイドとして機能し、読者が文書の内容を迅速に見つけて移動できるようにします。目次を通じて、読者は文書の構造と内容の整理を迅速に把握でき、読書時間を節約し、作業効率を向上させることができます。本記事では、C# プロジェクト内で Spire.Doc for .NET を使用して新しい Word ドキュメントの目次を作成する方法を説明します。
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
見出しスタイルを使用して目次を作成することは、異なるレベルの見出しスタイルを使用して文書内のタイトルやサブタイトルを特定することで、Word 文書内に目次を自動生成する方法です。以下に詳細な手順を示します:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// 新しいドキュメントオブジェクトを作成
Document doc = new Document();
// ドキュメントにセクションを追加
Section section = doc.AddSection();
// CharacterFormatオブジェクトを作成し、フォントを設定
CharacterFormat characterFormat1 = new CharacterFormat(doc);
characterFormat1.FontName = "Yu Gothic UI";
// 別のCharacterFormatオブジェクトを作成
CharacterFormat characterFormat2 = new CharacterFormat(doc);
characterFormat2.FontName = "Yu Gothic UI";
characterFormat2.FontSize = 12;
// 段落を追加
Paragraph TOCparagraph = section.AddParagraph();
TableOfContent TOC = TOCparagraph.AppendTOC(1, 3);
TOC.ApplyCharacterFormat(characterFormat1);
// 見出し1スタイルの段落を追加
Paragraph paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(BuiltinStyle.Heading1);
// テキストを追加し、文字書式を適用
TextRange textRange1 = paragraph.AppendText("概要");
textRange1.ApplyCharacterFormat(characterFormat1);
// 通常のコンテンツを追加
paragraph = section.Body.AddParagraph();
TextRange textRange2 = paragraph.AppendText("Spire.Doc for .NETは、開発者が.NETプラットフォーム上でWordドキュメントを作成、読み取り、書き込み、変換、比較、および印刷するために特別に設計されたプロフェッショナルなWord .NETライブラリです(対象.NET Framework、.NET Core、.NET Standard、.NET 5.0、.NET 6.0、Xamarin&Mono Android)。高速で高品質なパフォーマンスを提供します。");
textRange2.ApplyCharacterFormat(characterFormat2);
// 見出し1スタイルの段落を追加
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(BuiltinStyle.Heading1);
textRange1 = paragraph.AppendText("主な機能");
textRange1.ApplyCharacterFormat(characterFormat1);
// 見出し2スタイルの段落を追加
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(BuiltinStyle.Heading2);
textRange1 = paragraph.AppendText("Spire.Docのみ、Microsoft Office Automationなし");
textRange1.ApplyCharacterFormat(characterFormat1);
// 通常のコンテンツを追加
paragraph = section.Body.AddParagraph();
textRange2 = paragraph.AppendText("Spire.Doc for .NETは、Microsoft Officeがシステムにインストールされていなくても動作する完全に独立した.NET Wordクラスライブラリです。Microsoft Office Automationは不安定で遅く、MS Wordドキュメントを生成するのにスケーラブルではありません。Spire.Doc for .NETはMicrosoft Word Automationよりもはるかに高速で、より優れた安定性とスケーラビリティを持っています。");
textRange2.ApplyCharacterFormat(characterFormat2);
// 見出し3スタイルの段落を追加
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(BuiltinStyle.Heading3);
textRange1 = paragraph.AppendText("Wordバージョン");
textRange1.ApplyCharacterFormat(characterFormat1);
paragraph = section.Body.AddParagraph();
textRange2 = paragraph.AppendText("Word97-03 Word2007 Word2010 Word2013 Word2016 Word2019");
textRange2.ApplyCharacterFormat(characterFormat2);
// 見出し2スタイルの段落を追加
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(BuiltinStyle.Heading2);
textRange1 = paragraph.AppendText("高品質なファイルドキュメントの変換");
textRange1.ApplyCharacterFormat(characterFormat1);
// 通常のコンテンツを追加
paragraph = section.Body.AddParagraph();
textRange2 = paragraph.AppendText("Spire.Doc for .NETを使用することで、ユーザーはWord Doc/Docxをストリームとして保存したり、Webレスポンスとして保存したり、Word Doc/DocxをXML、Markdown、RTF、EMF、TXT、XPS、EPUB、HTML、SVG、ODTなどに変換することができます。Spire.Doc for .NETは、Word Doc/DocxをPDFやHTMLを画像に変換することもサポートしています。");
textRange2.ApplyCharacterFormat(characterFormat2);
// 見出し2スタイルの段落を追加
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(BuiltinStyle.Heading2);
textRange1 = paragraph.AppendText("その他の技術的特徴");
textRange1.ApplyCharacterFormat(characterFormat1);
// 通常のコンテンツを追加
paragraph = section.Body.AddParagraph();
textRange2 = paragraph.AppendText("Spire.Docを使用することで、開発者はASP.NET、Webサービス、WinForms、Xamarin、Mono Androidなど、32ビットまたは64ビットの.NETアプリケーションを構築し、Wordドキュメントを作成および処理することができます。");
textRange2.ApplyCharacterFormat(characterFormat2);
// 目次を更新
doc.UpdateTableOfContents();
// ドキュメントを保存
doc.SaveToFile("output/見出しスタイルで目次を作成.docx", FileFormat.Docx2016);
// リソースを解放
doc.Dispose();
}
}
}
Word 文書内でアウトラインレベルスタイルを使用して目次を作成することもできます。ParagraphFormat.OutlineLevel プロパティを使用してアウトラインの段落のレベルスタイルを指定し、これらのアウトラインレベルスタイルを TableOfContent.SetTOCLevelStyle() メソッドを介して目次作成ルールに適用できます。以下に詳細な手順を示します:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// ドキュメントオブジェクトを作成
Document doc = new Document();
Section section = doc.AddSection();
// アウトラインレベル1を定義
ParagraphStyle titleStyle1 = new ParagraphStyle(doc);
titleStyle1.Name = "T1S";
titleStyle1.ParagraphFormat.OutlineLevel = OutlineLevel.Level1;
titleStyle1.CharacterFormat.Bold = true;
titleStyle1.CharacterFormat.FontName = "Yu Gothic UI";
titleStyle1.CharacterFormat.FontSize = 18f;
titleStyle1.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
doc.Styles.Add(titleStyle1);
// アウトラインレベル2を定義
ParagraphStyle titleStyle2 = new ParagraphStyle(doc);
titleStyle2.Name = "T2S";
titleStyle2.ParagraphFormat.OutlineLevel = OutlineLevel.Level2;
titleStyle2.CharacterFormat.Bold = true;
titleStyle2.CharacterFormat.FontName = "Yu Gothic UI";
titleStyle2.CharacterFormat.FontSize = 16f;
titleStyle2.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
doc.Styles.Add(titleStyle2);
// アウトラインレベル3を定義
ParagraphStyle titleStyle3 = new ParagraphStyle(doc);
titleStyle3.Name = "T3S";
titleStyle3.ParagraphFormat.OutlineLevel = OutlineLevel.Level3;
titleStyle3.CharacterFormat.Bold = true;
titleStyle3.CharacterFormat.FontName = "Yu Gothic UI";
titleStyle3.CharacterFormat.FontSize = 14f;
titleStyle3.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
doc.Styles.Add(titleStyle3);
// 段落を追加
Paragraph TOCparagraph = section.AddParagraph();
TableOfContent toc = TOCparagraph.AppendTOC(1, 3);
toc.UseHeadingStyles = false;
toc.UseHyperlinks = true;
toc.UseTableEntryFields = false;
toc.RightAlignPageNumbers = true;
toc.SetTOCLevelStyle(1, titleStyle1.Name);
toc.SetTOCLevelStyle(2, titleStyle2.Name);
toc.SetTOCLevelStyle(3, titleStyle3.Name);
// 文字書式を定義
CharacterFormat characterFormat = new CharacterFormat(doc);
characterFormat.FontName = "Yu Gothic UI";
characterFormat.FontSize = 12;
// 段落を追加し、アウトラインレベルスタイル1を適用
Paragraph paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(titleStyle1.Name);
paragraph.AppendText("概要");
// 段落を追加し、テキストコンテンツを設定
paragraph = section.Body.AddParagraph();
TextRange textRange = paragraph.AppendText("Spire.Doc for .NETは、開発者が.NETプラットフォーム上でWordドキュメントを作成、読み取り、書き込み、変換、比較、および印刷するために特別に設計されたプロフェッショナルなWord .NETライブラリです(対象.NET Framework、.NET Core、.NET Standard、.NET 5.0、.NET 6.0、Xamarin&Mono Android)。高速で高品質なパフォーマンスを提供します。");
textRange.ApplyCharacterFormat(characterFormat);
// 段落を追加し、アウトラインレベルスタイル1を適用
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(titleStyle1.Name);
paragraph.AppendText("主な機能");
// 段落を追加し、アウトラインレベルスタイル2を適用
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(titleStyle2.Name);
paragraph.AppendText("Spire.Docのみ、Microsoft Office Automationなし");
// 段落を追加し、テキストコンテンツを設定
paragraph = section.Body.AddParagraph();
textRange = paragraph.AppendText("Spire.Doc for .NETは、Microsoft Officeがシステムにインストールされていなくても動作する完全に独立した.NET Wordクラスライブラリです。Microsoft Office Automationは不安定で遅く、MS Wordドキュメントを生成するのにスケーラブルではありません。Spire.Doc for .NETはMicrosoft Word Automationよりもはるかに高速で、より優れた安定性とスケーラビリティを持っています。");
textRange.ApplyCharacterFormat(characterFormat);
// 段落を追加し、アウトラインレベルスタイル3を適用
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(titleStyle3.Name);
paragraph.AppendText("Wordバージョン");
// 段落を追加し、テキストコンテンツを設定
paragraph = section.Body.AddParagraph();
textRange = paragraph.AppendText("Word97-03 Word2007 Word2010 Word2013 Word2016 Word2019");
textRange.ApplyCharacterFormat(characterFormat);
// 段落を追加し、アウトラインレベルスタイル2を適用
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(titleStyle2.Name);
paragraph.AppendText("高品質なファイルドキュメントの変換");
// 段落を追加し、テキストコンテンツを設定
paragraph = section.Body.AddParagraph();
textRange = paragraph.AppendText("Spire.Doc for .NETを使用することで、ユーザーはWord Doc/Docxをストリームとして保存したり、Webレスポンスとして保存したり、Word Doc/DocxをXML、Markdown、RTF、EMF、TXT、XPS、EPUB、HTML、SVG、ODTなどに変換することができます。Spire.Doc for .NETは、Word Doc/DocxをPDFやHTMLを画像に変換することもサポートしています。");
textRange.ApplyCharacterFormat(characterFormat);
// 段落を追加し、アウトラインレベルスタイル2を適用
paragraph = section.Body.AddParagraph();
paragraph.ApplyStyle(titleStyle2.Name);
paragraph.AppendText("その他の技術的特徴");
// 段落を追加し、テキストコンテンツを設定
paragraph = section.Body.AddParagraph();
textRange = paragraph.AppendText("Spire.Docを使用することで、開発者はASP.NET、Webサービス、WinForms、Xamarin、Mono Androidなど、32ビットまたは64ビットの.NETアプリケーションを構築し、Wordドキュメントを作成および処理することができます。");
textRange.ApplyCharacterFormat(characterFormat);
// 目次を更新
doc.UpdateTableOfContents();
// ドキュメントを保存
doc.SaveToFile("output/アウトラインレベルスタイルで目次を作成.docx", FileFormat.Docx2016);
// リソースを解放
doc.Dispose();
}
}
}
Spire.Doc ライブラリを使用して、TableOfContent tocForImage = new TableOfContent(Document, " \h \z \c "Image"") メソッドを使用して画像キャプションに基づいた目次を作成できます。以下に詳細な手順を示します:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// ドキュメントオブジェクトを作成
Document doc = new Document();
// ドキュメントにセクションを追加
Section section = doc.AddSection();
// 画像用の目次オブジェクトを作成
TableOfContent tocForImage = new TableOfContent(doc, " \\h \\z \\c \"Picture\"");
// セクションに段落を追加
Paragraph tocParagraph = section.Body.AddParagraph();
// 段落にTOCオブジェクトを追加
tocParagraph.Items.Add(tocForImage);
// フィールドセパレーターを追加
tocParagraph.AppendFieldMark(FieldMarkType.FieldSeparator);
// テキストコンテンツを追加
tocParagraph.AppendText("TOC");
// フィールドエンドマークを追加
tocParagraph.AppendFieldMark(FieldMarkType.FieldEnd);
// セクションに空の段落を追加
section.Body.AddParagraph();
// セクションに段落を追加
Paragraph paragraph = section.Body.AddParagraph();
// 画像を追加
DocPicture docPicture = paragraph.AppendPicture("images/Doc-NET.png");
docPicture.Width = 100;
docPicture.Height = 100;
// 画像のキャプション段落を追加
Paragraph pictureCaptionParagraph = docPicture.AddCaption("Picture", CaptionNumberingFormat.Number, CaptionPosition.BelowItem) as Paragraph;
pictureCaptionParagraph.AppendText(" Spire.Doc for .NET製品");
pictureCaptionParagraph.Format.AfterSpacing = 20;
// セクションに段落を追加し続ける
paragraph = section.Body.AddParagraph();
docPicture = paragraph.AppendPicture("images/PDF-NET.png");
docPicture.Width = 100;
docPicture.Height = 100;
pictureCaptionParagraph = docPicture.AddCaption("Picture", CaptionNumberingFormat.Number, CaptionPosition.BelowItem) as Paragraph;
pictureCaptionParagraph.AppendText(" Spire.PDF for .NET製品");
pictureCaptionParagraph.Format.AfterSpacing = 20;
paragraph = section.Body.AddParagraph();
docPicture = paragraph.AppendPicture("images/XLS-NET.png");
docPicture.Width = 100;
docPicture.Height = 100;
pictureCaptionParagraph = docPicture.AddCaption("Picture", CaptionNumberingFormat.Number, CaptionPosition.BelowItem) as Paragraph;
pictureCaptionParagraph.AppendText(" Spire.XLS for .NET製品");
pictureCaptionParagraph.Format.AfterSpacing = 20;
paragraph = section.Body.AddParagraph();
docPicture = paragraph.AppendPicture("images/PPT-NET.png");
docPicture.Width = 100;
docPicture.Height = 100;
pictureCaptionParagraph = docPicture.AddCaption("Picture", CaptionNumberingFormat.Number, CaptionPosition.BelowItem) as Paragraph;
pictureCaptionParagraph.AppendText(" Spire.Presentation for .NET製品");
// 目次を更新
doc.UpdateTableOfContents(tocForImage);
// ドキュメントをファイルに保存
doc.SaveToFile("output/画像キャプションで目次を作成.docx", FileFormat.Docx2016);
// ドキュメントオブジェクトを破棄
doc.Dispose();
}
}
}
表キャプションを使用して目次を作成するためには、TableOfContent tocForTable = new TableOfContent(Document, " \h \z \c "Table"") メソッドを使用します。以下に詳細な手順を示します:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// 新しいドキュメントを作成
Document doc = new Document();
// ドキュメントにセクションを追加
Section section = doc.AddSection();
// TableOfContentオブジェクトを作成
TableOfContent tocForTable = new TableOfContent(doc, " \\h \\z \\c \"Table\"");
// セクションに段落を追加し、TableOfContentオブジェクトを配置
Paragraph tocParagraph = section.Body.AddParagraph();
tocParagraph.Items.Add(tocForTable);
tocParagraph.AppendFieldMark(FieldMarkType.FieldSeparator);
tocParagraph.AppendText("TOC");
tocParagraph.AppendFieldMark(FieldMarkType.FieldEnd);
// セクションに空の段落を2つ追加
section.Body.AddParagraph();
section.Body.AddParagraph();
// セクションにテーブルを追加
Table table = section.Body.AddTable(true);
table.ResetCells(1, 3);
// テーブルのキャプション段落を追加
Paragraph tableCaptionParagraph = table.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem) as Paragraph;
tableCaptionParagraph.AppendText(" 一行三列");
tableCaptionParagraph.Format.AfterSpacing = 18;
// セクションに新しいテーブルを追加
table = section.Body.AddTable(true);
table.ResetCells(3, 3);
// 2番目のテーブルのキャプション段落を追加
tableCaptionParagraph = table.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem) as Paragraph;
tableCaptionParagraph.AppendText(" 三行三列");
tableCaptionParagraph.Format.AfterSpacing = 18;
// セクションにもう一つ新しいテーブルを追加
table = section.Body.AddTable(true);
table.ResetCells(5, 3);
// 3番目のテーブルのキャプション段落を追加
tableCaptionParagraph = table.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem) as Paragraph;
tableCaptionParagraph.AppendText(" 五行三列");
// 目次を更新
doc.UpdateTableOfContents(tocForTable);
// ドキュメントを指定されたファイルに保存
doc.SaveToFile("output/表キャプションで目次を作成.docx", Spire.Doc.FileFormat.Docx2016);
// リソースを破棄
doc.Dispose();
}
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
フォームでは、テキスト、日付、画像、はい/いいえ形式の質問など、さまざまなタイプの情報のプレースホルダーを作成できます。これにより、読み手はどのような種類の情報を記載すればよいかがわかりやすくなり、また、すべての情報が同じように書式設定されるようにすることができます。Word ドキュメントで記入可能なフォームを作成するためには、以下のツールを使用する必要があります。
Word ドキュメントのコンテンツ コントロールは、ユーザーが構造化されたドキュメントを作成できるようにするためのコンテンツのコンテナです。構造化されたドキュメントでは、ドキュメント内でコンテンツが表示される場所を制御します。Word 2013 では、基本的に10種類のコンテンツ コントロールが利用可能です。この記事では、Spire.Doc for .NET を使用して、以下の7つの一般的なコンテンツ コントロールで構成される Word ドキュメントに記入可能なフォームを作成する方法を説明します。
コンテンツ コントロール | 説明 |
テキスト | テキストフィールドはプレーンテキストに限定されるため、書式を含めることはできません。 |
リッチ テキスト | テキストフィールドで、フォーマットされたテキストや、テーブル、画像、その他のコンテンツコントロールなどのアイテムを含むことができます。 |
画像 | 1枚の画像を含むことができます。 |
ドロップダウン リスト | ドロップダウンリストは、ユーザーが選択できるように、あらかじめ定義されたアイテムのリストを表示します。 |
コンボ ボックス | コンボボックスは、ユーザーがリストの中からあらかじめ定義された値を選択したり、コントロールのテキストボックスに独自の値を入力したりすることができるものです。 |
チェック ボックス | チェックボックスは、ユーザーが「はい(チェック済み)」か「いいえ(チェックなし)」かの二者択一を行うことができるグラフィカルなウィジェットです。 |
日付選択 | ユーザーが日付を選択できるカレンダーコントロールが含まれる。 |
まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.Doc
Spire.Doc for .NET が提供する StructureDocumentTagInline クラスは、段落内の インライン構造(DrawingML のオブジェクトやフィールドなど)に対する構造化ドキュメントタグを作成するために使用されます。このクラスの下にある SDTProperties プロパティと SDTContent プロパティは、現在の構造化ドキュメントタグのプロパティとコンテンツを指定するために使用します。以下は、Word ドキュメントにコンテンツコントロールを持つ記入可能なフォームを作成するための詳細な手順です。
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing;
namespace CreateFormInWord
{
class Program
{
static void Main(string[] args)
{
//Documentのオブジェクトを作成する
Document doc = new Document();
//セクションを追加する
Section section = doc.AddSection();
//表を追加する
Table table = section.AddTable(true);
table.ResetCells(7, 2);
//1列目のセルにテキストを追加する
Paragraph paragraph = table.Rows[0].Cells[0].AddParagraph();
paragraph.GetStyle().CharacterFormat.FontName = "BIZ UDGothic";
paragraph.AppendText("テキスト");
paragraph = table.Rows[1].Cells[0].AddParagraph();
paragraph.AppendText("リッチ テキスト");
paragraph = table.Rows[2].Cells[0].AddParagraph();
paragraph.AppendText("画像");
paragraph = table.Rows[3].Cells[0].AddParagraph();
paragraph.AppendText("ドロップダウン リスト");
paragraph = table.Rows[4].Cells[0].AddParagraph();
paragraph.AppendText("チェック ボックス");
paragraph = table.Rows[5].Cells[0].AddParagraph();
paragraph.AppendText("コンボ ボックス");
paragraph = table.Rows[6].Cells[0].AddParagraph();
paragraph.AppendText("日付選択");
//セル(0,1)にプレーンテキストコンテンツコントロールを追加する
paragraph = table.Rows[0].Cells[1].AddParagraph();
StructureDocumentTagInline sdt = new StructureDocumentTagInline(doc);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.Text;
sdt.SDTProperties.Alias = "テキスト";
sdt.SDTProperties.Tag = "テキスト";
sdt.SDTProperties.IsShowingPlaceHolder = true;
SdtText text = new SdtText(true);
text.IsMultiline = false;
sdt.SDTProperties.ControlProperties = text;
TextRange tr = new TextRange(doc);
tr.Text = "ここをクリックまたはタップしてテキストを入力してください。";
sdt.SDTContent.ChildObjects.Add(tr);
//セル(1,1)にリッチテキストコンテンツコントロールを追加する
paragraph = table.Rows[1].Cells[1].AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.RichText;
sdt.SDTProperties.Alias = "リッチ テキスト";
sdt.SDTProperties.Tag = "リッチ テキスト";
sdt.SDTProperties.IsShowingPlaceHolder = true;
text = new SdtText(true);
text.IsMultiline = false;
sdt.SDTProperties.ControlProperties = text;
tr = new TextRange(doc);
tr.Text = "ここをクリックまたはタップしてテキストを入力してください。";
sdt.SDTContent.ChildObjects.Add(tr);
//セル(2,1)に画像コンテンツコントロールを追加する
paragraph = table.Rows[2].Cells[1].AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.Picture;
sdt.SDTProperties.Alias = "画像";
sdt.SDTProperties.Tag = "画像";
SdtPicture sdtPicture = new SdtPicture();
sdt.SDTProperties.ControlProperties = sdtPicture;
DocPicture pic = new DocPicture(doc);
pic.LoadImage(Image.FromFile("C:\\Users\\Sirion\\Desktop\\画像を選択.png"));
sdt.SDTContent.ChildObjects.Add(pic);
//セル(3,1)にドロップダウンリストコンテンツコントロールを追加する
paragraph = table.Rows[3].Cells[1].AddParagraph();
sdt = new StructureDocumentTagInline(doc);
sdt.SDTProperties.SDTType = SdtType.DropDownList;
sdt.SDTProperties.Alias = "ドロップダウン リスト";
sdt.SDTProperties.Tag = "ドロップダウン リスト";
paragraph.ChildObjects.Add(sdt);
SdtDropDownList sddl = new SdtDropDownList();
sddl.ListItems.Add(new SdtListItem("アイテムを選択してください。", "1"));
sddl.ListItems.Add(new SdtListItem("アイテム 2", "2"));
sddl.ListItems.Add(new SdtListItem("アイテム 3", "3"));
sddl.ListItems.Add(new SdtListItem("アイテム 4", "4"));
sdt.SDTProperties.ControlProperties = sddl;
tr = new TextRange(doc);
tr.Text = sddl.ListItems[0].DisplayText;
sdt.SDTContent.ChildObjects.Add(tr);
//セル(4,1)にチェックボックスコンテンツコントロールを2つ追加する
paragraph = table.Rows[4].Cells[1].AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.CheckBox;
SdtCheckBox scb = new SdtCheckBox();
sdt.SDTProperties.ControlProperties = scb;
tr = new TextRange(doc);
sdt.ChildObjects.Add(tr);
scb.Checked = false;
paragraph.AppendText(" オプション 1");
paragraph = table.Rows[4].Cells[1].AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.CheckBox;
scb = new SdtCheckBox();
sdt.SDTProperties.ControlProperties = scb;
tr = new TextRange(doc);
sdt.ChildObjects.Add(tr);
scb.Checked = false;
paragraph.AppendText(" オプション 2");
//セル(5,1)にコンボボックスコンテンツコントロールを追加する
paragraph = table.Rows[5].Cells[1].AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.ComboBox;
sdt.SDTProperties.Alias = "コンボ ボックス";
sdt.SDTProperties.Tag = "コンボ ボックス";
SdtComboBox cb = new SdtComboBox();
cb.ListItems.Add(new SdtListItem("アイテムを選択してください。"));
cb.ListItems.Add(new SdtListItem("アイテム 2"));
cb.ListItems.Add(new SdtListItem("アイテム 3"));
sdt.SDTProperties.ControlProperties = cb;
tr = new TextRange(doc);
tr.Text = cb.ListItems[0].DisplayText;
sdt.SDTContent.ChildObjects.Add(tr);
//セル(6,1)に日付選択コンテンツコントロールを追加する
paragraph = table.Rows[6].Cells[1].AddParagraph();
sdt = new StructureDocumentTagInline(doc);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.DatePicker;
sdt.SDTProperties.Alias = "日付選択";
sdt.SDTProperties.Tag = "日付選択";
SdtDate date = new SdtDate();
date.CalendarType = CalendarType.Default;
date.DateFormat = "yyyy.MM.dd";
date.FullDate = DateTime.Now;
sdt.SDTProperties.ControlProperties = date;
tr = new TextRange(doc);
tr.Text = "クリックまたはタップして日付を入力してください。";
sdt.SDTContent.ChildObjects.Add(tr);
//ユーザーがフォームフィールドを編集することのみを許可する
doc.Protect(ProtectionType.AllowOnlyFormFields, "permission-psd");
//ドキュメントを保存する
doc.SaveToFile("記入可能なフォーム.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System
Imports System.Drawing
Namespace CreateFormInWord
Class Program
Shared Sub Main(ByVal args() As String)
'Documentのオブジェクトを作成する
Dim doc As Document = New Document()
'セクションを追加する
Dim section As Section = doc.AddSection()
'表を追加する
Dim table As Table = section.AddTable(True)
table.ResetCells(7, 2)
'1列目のセルにテキストを追加する
Dim paragraph As Paragraph = table.Rows(0).Cells(0).AddParagraph()
paragraph.GetStyle().CharacterFormat.FontName = "BIZ UDGothic"
paragraph.AppendText("テキスト")
paragraph = table.Rows(1).Cells(0).AddParagraph()
paragraph.AppendText("リッチ テキスト")
paragraph = table.Rows(2).Cells(0).AddParagraph()
paragraph.AppendText("画像")
paragraph = table.Rows(3).Cells(0).AddParagraph()
paragraph.AppendText("ドロップダウン リスト")
paragraph = table.Rows(4).Cells(0).AddParagraph()
paragraph.AppendText("チェック ボックス")
paragraph = table.Rows(5).Cells(0).AddParagraph()
paragraph.AppendText("コンボ ボックス")
paragraph = table.Rows(6).Cells(0).AddParagraph()
paragraph.AppendText("日付選択")
'セル(0,1)にプレーンテキストコンテンツコントロールを追加する
paragraph = table.Rows(0).Cells(1).AddParagraph()
Dim sdt As StructureDocumentTagInline = New StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.Text
sdt.SDTProperties.Alias = "テキスト"
sdt.SDTProperties.Tag = "テキスト"
sdt.SDTProperties.IsShowingPlaceHolder = True
Dim text As SdtText = New SdtText(True)
text.IsMultiline = False
sdt.SDTProperties.ControlProperties = text
Dim tr As TextRange = New TextRange(doc)
tr.Text = "ここをクリックまたはタップしてテキストを入力してください。"
sdt.SDTContent.ChildObjects.Add(tr)
'セル(1,1)にリッチテキストコンテンツコントロールを追加する
paragraph = table.Rows(1).Cells(1).AddParagraph()
sdt = New StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.RichText
sdt.SDTProperties.Alias = "リッチ テキスト"
sdt.SDTProperties.Tag = "リッチ テキスト"
sdt.SDTProperties.IsShowingPlaceHolder = True
text = New SdtText(True)
text.IsMultiline = False
sdt.SDTProperties.ControlProperties = text
tr = New TextRange(doc)
tr.Text = "ここをクリックまたはタップしてテキストを入力してください。"
sdt.SDTContent.ChildObjects.Add(tr)
'セル(2,1)に画像コンテンツコントロールを追加する
paragraph = table.Rows(2).Cells(1).AddParagraph()
sdt = New StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.Picture
sdt.SDTProperties.Alias = "画像"
sdt.SDTProperties.Tag = "画像"
Dim sdtPicture As SdtPicture = New SdtPicture()
sdt.SDTProperties.ControlProperties = sdtPicture
Dim pic As DocPicture = New DocPicture(doc)
pic.LoadImage(Image.FromFile("C:\\Users\\Sirion\\Desktop\\画像を選択.png"))
sdt.SDTContent.ChildObjects.Add(pic)
'セル(3,1)にドロップダウンリストコンテンツコントロールを追加する
paragraph = table.Rows(3).Cells(1).AddParagraph()
sdt = New StructureDocumentTagInline(doc)
sdt.SDTProperties.SDTType = SdtType.DropDownList
sdt.SDTProperties.Alias = "ドロップダウン リスト"
sdt.SDTProperties.Tag = "ドロップダウン リスト"
paragraph.ChildObjects.Add(sdt)
Dim sddl As SdtDropDownList = New SdtDropDownList()
sddl.ListItems.Add(New SdtListItem("アイテムを選択してください。", "1"))
sddl.ListItems.Add(New SdtListItem("アイテム 2", "2"))
sddl.ListItems.Add(New SdtListItem("アイテム 3", "3"))
sddl.ListItems.Add(New SdtListItem("アイテム 4", "4"))
sdt.SDTProperties.ControlProperties = sddl
tr = New TextRange(doc)
tr.Text = sddl.ListItems(0).DisplayText
sdt.SDTContent.ChildObjects.Add(tr)
'セル(4,1)にチェックボックスコンテンツコントロールを2つ追加する
paragraph = table.Rows(4).Cells(1).AddParagraph()
sdt = New StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.CheckBox
Dim scb As SdtCheckBox = New SdtCheckBox()
sdt.SDTProperties.ControlProperties = scb
tr = New TextRange(doc)
sdt.ChildObjects.Add(tr)
scb.Checked = False
paragraph.AppendText(" オプション 1")
paragraph = table.Rows(4).Cells(1).AddParagraph()
sdt = New StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.CheckBox
scb = New SdtCheckBox()
sdt.SDTProperties.ControlProperties = scb
tr = New TextRange(doc)
sdt.ChildObjects.Add(tr)
scb.Checked = False
paragraph.AppendText(" オプション 2")
'セル(5,1)にコンボボックスコンテンツコントロールを追加する
paragraph = table.Rows(5).Cells(1).AddParagraph()
sdt = New StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.ComboBox
sdt.SDTProperties.Alias = "コンボ ボックス"
sdt.SDTProperties.Tag = "コンボ ボックス"
Dim cb As SdtComboBox = New SdtComboBox()
cb.ListItems.Add(New SdtListItem("アイテムを選択してください。"))
cb.ListItems.Add(New SdtListItem("アイテム 2"))
cb.ListItems.Add(New SdtListItem("アイテム 3"))
sdt.SDTProperties.ControlProperties = cb
tr = New TextRange(doc)
tr.Text = cb.ListItems(0).DisplayText
sdt.SDTContent.ChildObjects.Add(tr)
'セル(6,1)に日付選択コンテンツコントロールを追加する
paragraph = table.Rows(6).Cells(1).AddParagraph()
sdt = New StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.DatePicker
sdt.SDTProperties.Alias = "日付選択"
sdt.SDTProperties.Tag = "日付選択"
Dim date As SdtDate = New SdtDate()
Date.CalendarType = CalendarType.Default
Date.DateFormat = "yyyy.MM.dd"
Date.FullDate = DateTime.Now
sdt.SDTProperties.ControlProperties = Date
tr = New TextRange(doc)
tr.Text = "クリックまたはタップして日付を入力してください。"
sdt.SDTContent.ChildObjects.Add(tr)
'ユーザーがフォームフィールドを編集することのみを許可する
doc.Protect(ProtectionType.AllowOnlyFormFields, "permission-psd")
'ドキュメントを保存する
doc.SaveToFile("記入可能なフォーム.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。