如何在word文档中添加内容表? [英] How to add table of content in word document?

查看:70
本文介绍了如何在word文档中添加内容表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 docx库在c#中创建word文档。现在我想创建索引页面或者想要为页面编号添加该文件的内容表。请帮忙。

提前致谢。



我的尝试:



我尝试使用InsertDefaultTableOfContents方法,但它没有创建任何TOC。

I am creating word document in c# using docx library. Now i want to create index page or want to add table of content for that file with page number. Please help.
Thanks in advance.

What I have tried:

I have tried with InsertDefaultTableOfContents method but it's not creating any TOC.

推荐答案

引用:

我尝试过使用InsertDefaultTableOfContents方法,但它没有创建任何TOC。

I have tried with InsertDefaultTableOfContents method but it's not creating any TOC.



它工作正常-word之前我在wps office中检查过生成的空白ToC。


It is working fine in ms-word previously i checked in wps office which was generated blank ToC.

InsertDefaultTableOfContents 

方法为你的word文件添加ToC,用于所有文本样式为heading1,heading2 ...

method add's ToC for your word file for all text which is style as heading1, heading2...


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

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            TOC();
        }

        public static void TOC()
        {

            // Let's create a simple document.
            DocumentCore dc = new DocumentCore();

            // Create and add Heading 1 style for TOC.
            ParagraphStyle Heading1Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading1, dc);
            dc.Styles.Add(Heading1Style);

            // Create and add Heading 2 style for TOC.
            ParagraphStyle Heading2Style = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Heading2, dc);
            dc.Styles.Add(Heading2Style);

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

            // Add TOC title in the DOCX document.
            section.Blocks.Add(new Paragraph(dc, "Table of Contents"));

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

            section.Blocks.Add(new Paragraph(dc, "The end."));

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

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

            // 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 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."));

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

            // Add document content.
            // 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."));

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

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

            // Save DOCX to a file
            dc.Save("Table-Of-Contents.docx");

            // Open the result for demonstation purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Table-Of-Contents.docx") { UseShellExecute = true });
        }
    }
}


这篇关于如何在word文档中添加内容表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆