チュートリアル

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

チュートリアル».NET»Spire.Doc for .NET»フォームフィールド»C#:新規 Word 文書の目次を作成する方法
2024-08-01

C#:新規 Word 文書の目次を作成する方法

新しく作成した Word ドキュメントに目次を作成することは、文書の質を向上させ、読みやすさを改善し、情報を効果的に伝達する重要な手段です。目次は文書のナビゲーションガイドとして機能し、読者が文書の内容を迅速に見つけて移動できるようにします。目次を通じて、読者は文書の構造と内容の整理を迅速に把握でき、読書時間を節約し、作業効率を向上させることができます。本記事では、C# プロジェクト内で Spire.Doc for .NET を使用して新しい Word ドキュメントの目次を作成する方法を説明します。

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

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

PM> Install-Package Spire.Doc

見出しスタイルを使用して目次を作成する方法

見出しスタイルを使用して目次を作成することは、異なるレベルの見出しスタイルを使用して文書内のタイトルやサブタイトルを特定することで、Word 文書内に目次を自動生成する方法です。以下に詳細な手順を示します:

  • Document オブジェクトを作成する。
  • Document.AddSection() メソッドを使用してセクションを追加する。
  • Section.AddParagraph() メソッドを使用して段落を追加する。
  • Paragraph.AppendTOC(int lowerLevel, int upperLevel) メソッドを使用して目次オブジェクトを作成する。
  • CharacterFormat オブジェクトを作成し、フォントを設定する。
  • Paragraph.ApplyStyle(BuiltinStyle.Heading1) メソッドを使用して段落に見出しスタイルを適用する。
  • Paragraph.AppendText() メソッドを使用してテキスト内容を追加する。
  • TextRange.ApplyCharacterFormat() メソッドを使用してテキストに文字書式を適用する。
  • Document.UpdateTableOfContents() メソッドを使用して目次を更新する。
  • Document.SaveToFile() メソッドを使用して文書を保存する。
  • C#
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();
        }
    }
}

C#:新規 Word 文書の目次を作成する方法

アウトラインレベルスタイルを使用して目次を作成する方法

Word 文書内でアウトラインレベルスタイルを使用して目次を作成することもできます。ParagraphFormat.OutlineLevel プロパティを使用してアウトラインの段落のレベルスタイルを指定し、これらのアウトラインレベルスタイルを TableOfContent.SetTOCLevelStyle() メソッドを介して目次作成ルールに適用できます。以下に詳細な手順を示します:

  • Document オブジェクトを作成する。
  • Document.AddSection() メソッドを使用してセクションを追加する。
  • ParagraphStyle オブジェクトを作成し、ParagraphStyle.ParagraphFormat.OutlineLevel = OutlineLevel.Level1 を使用してアウトラインレベルを設定する。
  • 作成した ParagraphStyle オブジェクトを Document.Styles.Add() メソッドを使用して文書に追加する。
  • Section.AddParagraph() メソッドを使用して段落を追加する。
  • Paragraph.AppendTOC(int lowerLevel, int upperLevel) メソッドを使用して目次オブジェクトを作成する。
  • TableOfContent.UseHeadingStyles = false と設定し、見出しスタイルでの目次作成のデフォルト設定を無効にする。
  • TableOfContent.SetTOCLevelStyle(int levelNumber, string styleName) メソッドを使用して目次ルールにアウトラインレベルスタイルを適用する。
  • CharacterFormat オブジェクトを作成し、フォントを設定する。
  • Paragraph.ApplyStyle(ParagraphStyle.Name) メソッドを使用して段落にスタイルを適用する。
  • Paragraph.AppendText() メソッドを使用してテキスト内容を追加する。
  • TextRange.ApplyCharacterFormat() メソッドを使用してテキストに文字書式を適用する。
  • Document.UpdateTableOfContents() メソッドを使用して目次を更新する。
  • Document.SaveToFile() メソッドを使用して文書を保存する。
  • C#
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();
        }
    }
}

C#:新規 Word 文書の目次を作成する方法

画像キャプションを使用して目次を作成する方法

Spire.Doc ライブラリを使用して、TableOfContent tocForImage = new TableOfContent(Document, " \h \z \c "Image"") メソッドを使用して画像キャプションに基づいた目次を作成できます。以下に詳細な手順を示します:

  • Document オブジェクトを作成する。
  • Document.AddSection() メソッドを使用してセクションを追加する。
  • TableOfContent tocForImage = new TableOfContent(Document, " \h \z \c "Image"") を使用して目次オブジェクトを作成し、目次のスタイルを指定する。
  • Section.AddParagraph() メソッドを使用して段落を追加する。
  • Paragraph.Items.Add(tocForImage) メソッドを使用して段落に目次オブジェクトを追加する。
  • Paragraph.AppendFieldMark(FieldMarkType.FieldSeparator) メソッドを使用してフィールド区切り記号を追加する。
  • Paragraph.AppendText("TOC") メソッドを使用してテキスト内容「TOC」を追加する。
  • Paragraph.AppendFieldMark(FieldMarkType.FieldEnd) メソッドを使用してフィールド終了マークを追加する。
  • Paragraph.AppendPicture() メソッドを使用して画像を追加する。
  • DocPicture.AddCaption() メソッドを使用して画像のキャプション段落を追加し、製品情報や書式設定を含める。
  • Document.UpdateTableOfContents(tocForImage) メソッドを使用して文書の変更を反映するように目次を更新する。
  • Document.SaveToFile() メソッドを使用して文書を保存する。
  • C#
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();
        }
    }
}

C#:新規 Word 文書の目次を作成する方法

表キャプションを使用して目次を作成する方法

表キャプションを使用して目次を作成するためには、TableOfContent tocForTable = new TableOfContent(Document, " \h \z \c "Table"") メソッドを使用します。以下に詳細な手順を示します:

  • Document オブジェクトを作成する。
  • Document.AddSection() メソッドを使用してセクションを追加する。
  • TableOfContent tocForTable = new TableOfContent(Document, " \h \z \c "Table"") を使用して目次オブジェクトを作成し、目次のスタイルを指定する。
  • Section.AddParagraph() メソッドを使用して段落を追加する。
  • Paragraph.Items.Add(tocForTable) メソッドを使用して段落に目次オブジェクトを追加する。
  • Paragraph.AppendFieldMark(FieldMarkType.FieldSeparator) メソッドを使用してフィールド区切り記号を追加する。
  • Paragraph.AppendText("TOC") メソッドを使用してテキスト内容「TOC」を追加する。
  • Paragraph.AppendFieldMark(FieldMarkType.FieldEnd) メソッドを使用してフィールド終了マークを追加する。
  • Section.AddTable() メソッドを使用して表を追加し、Table.ResetCells(int rowsNum, int columnsNum) メソッドを使用して行数と列数を設定する。
  • Table.AddCaption() メソッドを使用して表のキャプション段落を追加し、製品情報や書式設定を含める。
  • Document.UpdateTableOfContents(tocForTable) メソッドを使用して文書の変更を反映するように目次を更新する。
  • Document.SaveToFile() メソッドを使用して文書を保存する。
  • C#
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();
        }
    }
}

C#:新規 Word 文書の目次を作成する方法

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

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

Read 147 times