【问题标题】:create table of content docx library创建目录 docx 库
【发布时间】:2017-12-13 17:53:12
【问题描述】:

我正在使用来自 c# 的 novacode docx 库来生成文档,我想知道如何在文档中添加标题并将它们链接到目录中。

【问题讨论】:

    标签: heading tableofcontents novacode-docx


    【解决方案1】:

    就我个人而言,我正在使用带有一些标题定义文本和一些标签的模板文档:

    1. [标题 1]

    然后,我正在使用这样的东西:

    using (document = DocX.Load(TEMPLATE_LOCATION))
    {
        #region Static data
    
        //Get datas from the ressource files and translate tag
        ResourceSet resourceSet = StaticLabels.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        foreach (DictionaryEntry entry in resourceSet)
        {
    
            string resourceKey = entry.Key.ToString();
            string resource = (string)entry.Value;
            document.ReplaceText(resourceKey, resource);
        }
    
        #endregion //Static Data
    
        #region Add Table of content
    
        document.InsertDefaultTableOfContents();
    
        #endregion //Table of content
    
    }
    

    资源文件包含 [TITLE 1] 和一些替换它的文本

    您也可以简单地使用:

    document.ReplaceText("[TITLE]", "My Title");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 2017-02-18
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多