1

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

4

1 回答 1

0

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

  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");
于 2017-09-29T14:07:51.047 回答