Как создать расширенное оглавление (TOC) в документе Word с использованием C# и .NET


Здесь мы покажем Вам, как создать документ с правильно отформатированным оглавлением. В результате Вы увидите документ DOCX с TOC содержащий два Chapters и SubChapters.

Несколько шагов:

  1. Обратите внимание, что Вы импортируете пространство имен SautinSoft.Document.
    с помощью SautinSoft.Document;
  2. Прежде всего, создайте DocumentCore объект с именем document.
    
    DocumentCore document = new DocumentCore();
    
    DocumentCore является корневым классом, он представляет сам документ.
  3. Создайте и добавьте стили Heading1, Heading2 и TOC.
    
                // Create and add Heading1 style. For "Chapter 1" and "Chapter 2".
                ParagraphStyle Heading1Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading1, dc);
                Heading1Style.ParagraphFormat.LineSpacing = 3;
                Heading1Style.CharacterFormat.Size = 18;
                // #358CCB - blue
                Heading1Style.CharacterFormat.FontColor = new Color("#358CCB");
                dc.Styles.Add(Heading1Style);
    
    
                // Create and add Heading2 style. For "SupChapter 1-1" and "SubChapter 2-1".
                ParagraphStyle Heading2Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading2, dc);
                Heading2Style.ParagraphFormat.LineSpacing = 2;
                Heading2Style.CharacterFormat.Size = 14;
                // #FF9900 - orange
                Heading2Style.CharacterFormat.FontColor = new Color("#FF9900");
                dc.Styles.Add(Heading2Style);
    
    
                // Create and add TOC style. 
                ParagraphStyle TOCStyle = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Subtitle, dc);
                TOCStyle.ParagraphFormat.OutlineLevel = OutlineLevel.BodyText;
                TOCStyle.ParagraphFormat.Alignment = HorizontalAlignment.Center;
                TOCStyle.CharacterFormat.Bold = true;
                // #358CCB - blue
                TOCStyle.CharacterFormat.FontColor = new Color("#358CCB");
                dc.Styles.Add(TOCStyle);
    
    Стили заголовков Word называются Heading 1, Heading 2 и далее до Heading 9. Используйте их для идентификации различных частей документа, но они также используют преимущества других функций Word.
    В Word стиль - это набор инструкций по форматированию. Вы используете стили для форматирования абзацев в вашем документе. Таким образом, вы должны использовать стиль "Title" для своего заголовка, стиль "Body Text" для основного текста, стиль "Caption" для подписей к изображениям и "Heading 1" для основных заголовков.

  4. Мы создали стили.Следующим шагом является применение этих стилей для TOC Header, TOC, и Toc Ending.
    
                // Add TOC Header.
                section.Blocks.Add(
                    new Paragraph(dc, "Table of Contents")
                    {ParagraphFormat = {Style = TOCStyle}});
    
                // Create and add TOC (Table of Contents).
                section.Blocks.Add(new TableOfEntries(dc, FieldType.TOC));
    
                // Add TOC Ending.
                section.Blocks.Add(
                     new Paragraph(dc, "The End")
                     {ParagraphFormat = { Alignment = HorizontalAlignment.Center, BackgroundColor = Color.Gray}});
    
    
    Мы получили такой результат:
  5. Создайте новый параграф для Chapter и SubChapter с помощью Heading1Style и Heading2Style.
    
                /// Add the document content (Chapter 1).
                // Add Chapter 1.
                section.Blocks.Add(
                    new Paragraph(dc, "Chapter 1")
                    {
                        ParagraphFormat =
                        {
                    Style = Heading1Style,
                    PageBreakBefore=true
                        }
                    });
    
                // Add SubChapter 1-1.
                section.Blocks.Add(
                            new Paragraph(dc, String.Format("Subchapter 1-1"))
                            {
                                ParagraphFormat =
                                {
                        Style = Heading2Style
                                }
                            });
    
    Paragraph -это производный от Block элемент, используемый для группировки встроенных элементов, таких как бег, фигура, изображение, поле и т.д.
  6. Когда Вы создаете оглавление таким образом, Вы можете автоматически обновлять его, если вносите изменения в свой документ.
    TOC может быть обновлен только после добавления всего содержимого документа.
    
       var tableofcontents = (TableOfEntries)dc.GetChildElements(true, ElementType.TableOfEntries).FirstOrDefault();
       tableofcontents.Update();
       // Apply the style for the TOC.
                foreach (Paragraph par in tableofcontents.Entries)
                {
                    par.ParagraphFormat.Style = TOCStyle;
                }
    
                // Update TOC's page numbers.
                // Page numbers are automatically updated in that case.
                dc.GetPaginator(new PaginatorOptions() { UpdateFields = true });
    
    Разбиение на страницы используется для разделения документа на отдельные страницы.
  7. Сохраните наш документ в формате DOCX.
    
        // Save DOCX to a file
        document.Save("Extended-Table-Of-Contents.docx");
    
    Загрузите полученный файл Docx: Extended-Table-Of-Contents.docx

Полный код

using System;
using System.Linq;
using SautinSoft.Document;

namespace Example
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            ExtendedTOC();
        }

        /// <summary>
        /// Create extended table of contents in word document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-extended-table-of-contents-in-word-document-net-csharp-vb.php
        /// </remarks> 
        public static void ExtendedTOC()
        {
            string resultFile = "Extended-Table-Of-Contents.docx";
            // First of all, create an instance of DocumentCore.
            DocumentCore dc = new DocumentCore();

            // Create and add Heading1 style. For "Chapter 1" and "Chapter 2".
            ParagraphStyle Heading1Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading1, dc);
            Heading1Style.ParagraphFormat.LineSpacing = 3;
            Heading1Style.CharacterFormat.Size = 18;
            // #358CCB - blue
            Heading1Style.CharacterFormat.FontColor = new Color("#358CCB");
            dc.Styles.Add(Heading1Style);

            // Create and add Heading2 style. For "SupChapter 1-1" and "SubChapter 2-1".
            ParagraphStyle Heading2Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading2, dc);
            Heading2Style.ParagraphFormat.LineSpacing = 2;
            Heading2Style.CharacterFormat.Size = 14;
            // #FF9900 - orange
            Heading2Style.CharacterFormat.FontColor = new Color("#FF9900");
            dc.Styles.Add(Heading2Style);

            // Create and add TOC style.
            ParagraphStyle TOCStyle = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Subtitle, dc);
            TOCStyle.ParagraphFormat.OutlineLevel = OutlineLevel.BodyText;
            TOCStyle.ParagraphFormat.Alignment = HorizontalAlignment.Center;
            TOCStyle.CharacterFormat.Bold = true;
            // #358CCB - blue
            TOCStyle.CharacterFormat.FontColor = new Color("#358CCB");
            dc.Styles.Add(TOCStyle);

            // Add new section.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // Add TOC Header.
            section.Blocks.Add(
                new Paragraph(dc, "Table of Contents")
                { ParagraphFormat = { Style = TOCStyle } });

            // Create and add TOC (Table of Contents).
            section.Blocks.Add(new TableOfEntries(dc, FieldType.TOC));

            // Add TOC Ending.
            section.Blocks.Add(
                 new Paragraph(dc, "The End")
                 { ParagraphFormat = { Alignment = HorizontalAlignment.Center, BackgroundColor = Color.Gray } });

            // Add the document content (Chapter 1).
            // Add Chapter 1.
            section.Blocks.Add(
                new Paragraph(dc, "Chapter 1")
                {
                    ParagraphFormat =
                    {
                Style = Heading1Style,
                PageBreakBefore=true
                    }
                });

            // Add SubChapter 1-1.
            section.Blocks.Add(
                        new Paragraph(dc, String.Format("Subchapter 1-1"))
                        {
                            ParagraphFormat =
                            {
                    Style = Heading2Style
                            }
                        });

            // Add the content of Chapter 1 / Subchapter 1-1.
            section.Blocks.Add(
                        new Paragraph(dc,
                            "�Document.Net� will help you in development of applications which operates with DOCX, RTF, PDF, HTML and Text documents.After adding of the reference to(SautinSoft.Document.dll) - it's 100% C# managed assembly you will be able to create a new document, parse an existing, modify anything what you want.")
                        {
                            ParagraphFormat = new ParagraphFormat
                            {
                                LeftIndentation = 10,
                                RightIndentation = 10,
                                SpecialIndentation = 20,
                                LineSpacing = 20,
                                LineSpacingRule = LineSpacingRule.Exactly,
                                SpaceBefore = 20,
                                SpaceAfter = 20
                            }
                        });

            // Let's add another page break into.
            section.Blocks.Add(
                   new Paragraph(dc,
                   new SpecialCharacter(dc, SpecialCharacterType.PageBreak)));

            // Add the document content (Chapter 2).
            // Add Chapter 2.
            section.Blocks.Add(
                     new Paragraph(dc, "Chapter 2")
                     {
                         ParagraphFormat =
                         {
                Style = Heading1Style
                         }
                     });

            // Add SubChapter 2-1.
            section.Blocks.Add(
                new Paragraph(dc, String.Format("Subchapter 2-1"))
                {
                    ParagraphFormat =
                    {
                    Style = Heading2Style
                    }
                });

            // Add the content of Chapter 2 / Subchapter 2-1.
            section.Blocks.Add(
                        new Paragraph(dc,
                            "Requires only .Net 4.0 or above. Our product is compatible with all .Net languages and supports all Operating Systems where .Net Framework can be used. Note that �Document .Net� is entirely written in managed C#, which makes it absolutely standalone and an independent library. Of course, No dependency on Microsoft Word.")
                        {
                            ParagraphFormat = new ParagraphFormat
                            {
                                LeftIndentation = 10,
                                RightIndentation = 10,
                                SpecialIndentation = 20,
                                LineSpacing = 20,
                                LineSpacingRule = LineSpacingRule.Exactly,
                                SpaceBefore = 20,
                                SpaceAfter = 20
                            }
                        });

            // Update TOC (TOC can be updated only after all document content is added).
            var tableofcontents = (TableOfEntries)dc.GetChildElements(true, ElementType.TableOfEntries).FirstOrDefault();
            tableofcontents.Update();

            // Apply the style for the TOC.
            foreach (Paragraph par in tableofcontents.Entries)
            {
                par.ParagraphFormat.Style = TOCStyle;
            }

            // Update TOC's page numbers.
            // Page numbers are automatically updated in that case.
            dc.GetPaginator(new PaginatorOptions() { UpdateFields = true });

            // Save the document as DOCX file.
            dc.Save(resultFile);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultFile) { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports System
Imports System.Linq
Imports SautinSoft.Document

Module Sample
    Sub Main()
        ExtendedTOC()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Create extended table of contents in word document.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-extended-table-of-contents-in-word-document-net-csharp-vb.php
    ''' </remarks>
    Sub ExtendedTOC()
        Dim resultFile As String = "Extended-Table-Of-Contents.docx"
        ' First of all, create an instance of DocumentCore.
        Dim dc As New DocumentCore()

        ' Create and add Heading1 style. For "Chapter 1" and "Chapter 2".
        Dim Heading1Style As ParagraphStyle = CType(Style.CreateStyle(StyleTemplateType.Heading1, dc), ParagraphStyle)
        Heading1Style.ParagraphFormat.LineSpacing = 3
        Heading1Style.CharacterFormat.Size = 18
        ' #358CCB - blue
        Heading1Style.CharacterFormat.FontColor = New Color("#358CCB")
        dc.Styles.Add(Heading1Style)

        ' Create and add Heading2 style. For "SupChapter 1-1" and "SubChapter 2-1".
        Dim Heading2Style As ParagraphStyle = CType(Style.CreateStyle(StyleTemplateType.Heading2, dc), ParagraphStyle)
        Heading2Style.ParagraphFormat.LineSpacing = 2
        Heading2Style.CharacterFormat.Size = 14
        ' #FF9900 - orange
        Heading2Style.CharacterFormat.FontColor = New Color("#FF9900")
        dc.Styles.Add(Heading2Style)

        ' Create and add TOC style.
        Dim TOCStyle As ParagraphStyle = CType(Style.CreateStyle(StyleTemplateType.Subtitle, dc), ParagraphStyle)
        TOCStyle.ParagraphFormat.OutlineLevel = OutlineLevel.BodyText
        TOCStyle.ParagraphFormat.Alignment = HorizontalAlignment.Center
        TOCStyle.CharacterFormat.Bold = True
        ' #358CCB - blue
        TOCStyle.CharacterFormat.FontColor = New Color("#358CCB")
        dc.Styles.Add(TOCStyle)

        ' Add new section.
        Dim section As New Section(dc)
        dc.Sections.Add(section)

        ' Add TOC Header.
        section.Blocks.Add(New Paragraph(dc, "Table of Contents") With {
            .ParagraphFormat = New ParagraphFormat With {.Style = TOCStyle}
        })

        ' Create and add TOC (Table of Contents).
        section.Blocks.Add(New TableOfEntries(dc, FieldType.TOC))

        ' Add TOC Ending.
        section.Blocks.Add(New Paragraph(dc, "The End") With {
            .ParagraphFormat = New ParagraphFormat With {
                .Alignment = HorizontalAlignment.Center,
                .BackgroundColor = Color.Gray
            }
        })

        ' Add the document content (Chapter 1).
        ' Add Chapter 1.
        section.Blocks.Add(New Paragraph(dc, "Chapter 1") With {
            .ParagraphFormat = New ParagraphFormat With {
                .Style = Heading1Style,
                .PageBreakBefore = True
            }
        })

        ' Add SubChapter 1-1.
        section.Blocks.Add(New Paragraph(dc, String.Format("Subchapter 1-1")) With {
            .ParagraphFormat = New ParagraphFormat With {.Style = Heading2Style}
        })

        ' Add the content of Chapter 1 / Subchapter 1-1.
        section.Blocks.Add(New Paragraph(dc, "�Document.Net� will help you in development of applications which operates with DOCX, RTF, PDF, HTML and Text documents.After adding of the reference to(SautinSoft.Document.dll) - it's 100% C# managed assembly you will be able to create a new document, parse an existing, modify anything what you want.") With {
            .ParagraphFormat = New ParagraphFormat With {
                .LeftIndentation = 10,
                .RightIndentation = 10,
                .SpecialIndentation = 20,
                .LineSpacing = 20,
                .LineSpacingRule = LineSpacingRule.Exactly,
                .SpaceBefore = 20,
                .SpaceAfter = 20
            }
        })

        ' Let's add another page break into.
        section.Blocks.Add(New Paragraph(dc, New SpecialCharacter(dc, SpecialCharacterType.PageBreak)))

        ' Add the document content (Chapter 2).
        ' Add Chapter 2.
        section.Blocks.Add(New Paragraph(dc, "Chapter 2") With {
            .ParagraphFormat = New ParagraphFormat With
            {.Style = Heading1Style}
        })

        ' Add SubChapter 2-1.
        section.Blocks.Add(New Paragraph(dc, String.Format("Subchapter 2-1")) With {
            .ParagraphFormat = New ParagraphFormat With {.Style = Heading2Style}
        })

        ' Add the content of Chapter 2 / Subchapter 2-1.
        section.Blocks.Add(New Paragraph(dc, "Requires only .Net 4.0 or above. Our product is compatible with all .Net languages and supports all Operating Systems where .Net Framework can be used. Note that �Document .Net� is entirely written in managed C#, which makes it absolutely standalone and an independent library. Of course, No dependency on Microsoft Word.") With {
            .ParagraphFormat = New ParagraphFormat With {
                .LeftIndentation = 10,
                .RightIndentation = 10,
                .SpecialIndentation = 20,
                .LineSpacing = 20,
                .LineSpacingRule = LineSpacingRule.Exactly,
                .SpaceBefore = 20,
                .SpaceAfter = 20
            }
        })

        ' Update TOC (TOC can be updated only after all document content is added).
        Dim tableofcontents = CType(dc.GetChildElements(True, ElementType.TableOfEntries).FirstOrDefault(), TableOfEntries)
        tableofcontents.Update()

        ' Apply the style for the TOC.
        For Each par As Paragraph In tableofcontents.Entries
            par.ParagraphFormat.Style = TOCStyle
        Next par

        ' Update TOC's page numbers.
        ' Page numbers are automatically updated in that case.
        dc.GetPaginator(New PaginatorOptions() With {.UpdateFields = True})

        ' Save the document as DOCX file.
        dc.Save(resultFile)

        ' Open the result for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultFile) With {.UseShellExecute = True})
    End Sub

End Module

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.