C#:使用 Office Interop 库在 Word 文档中的书签处插入和缩进项目符号点

C#: Insert and indent bullet points at bookmark in word document using Office Interop libraries

这是我的困境。我被赋予了一项任务,即根据来自 Web 前端的用户输入生成现有 Word 文档的特定部分。系统的后端是用 C# 编写的,其中使用 Microsoft.Office.Interop.Word 命名空间编辑 word 文档的部分。

基本上,他们会从可用指令列表中 select,每个指令都是 string 类型,然后将用于生成文档的指令部分,每个指令都是单独的是列表中的另一个项目符号。这部分工作正常。我的问题是,指令可以包含字符 \,需要将其替换为缩进,或者如果您在 word 中打开文档,则相当于在项目符号中按 TAB 键。到目前为止,我能够让它很好地将项目符号插入列表的中间,它继续按预期对它们进行适当编号。问题是我无法根据需要缩进它们。

我已经尝试了在这里和其他几个网站上找到的几乎所有示例来使它起作用,但没有成功。最新的迭代在下面的代码中,它只是尽可能地缩进整个列表。

var bookmark = "bookMarkName";
var docPath = @"c:\temp\Template.docx";
var app = new Application();
var doc = app.Documents.Open(docPath);
var range = doc.Bookmarks[bookmark].Range;
var listTemplate = range.ListFormat.ListTemplate;

range.ListFormat.ApplyListTemplate(listTemplate);

string[] bulletList = new string[] {
    @"Point A",
    @"\Point B",
    @"\Point C",
    @"\Point D",
    @"Point E"
}

var count = bulletList.Length;

for (var i = 0; i < count; i++)
{
    var listLevel = 0;
    var currentItem = bulletList[i];
    var item = currentItem.Replace(@"\", "");

    if (i < count - 1)
        item = item + "\n";

    listLevel += currentItem.ToList().Where(x => x == '\').Select(x => x).Count();

    for (var x = 0; x < listLevel; x++)
    {
        range.ListFormat.ListIndent();
    }

    range.InsertAfter(item);
}

doc.SaveAs(@"c:\temp\" + DateTime.Now.Ticks + ".docx");
doc.Close();

所以我的代码输出应该是:

这是我第一次真正不得不使用 Office Interop 库,所以我肯定这里缺少一些东西。任何帮助将不胜感激。

请先使用此代码Add DocX DLL

 using (var document = DocX.Create(@"docs\Lists.docx"))
    {
        var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered);
                //Add a numbered list starting at 2
        document.AddListItem(numberedList, "Second List Item.");
        document.AddListItem(numberedList, "Third list item.");
        document.AddListItem(numberedList, "First sub list item", 1);

        document.AddListItem(numberedList, "Nested item.", 2);
        document.AddListItem(numberedList, "Fourth nested item.");

        var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
        document.AddListItem(bulletedList, "Second bullet item");
        document.AddListItem(bulletedList, "Sub bullet item", 1);
        document.AddListItem(bulletedList, "Second sub bullet item", 2);
        document.AddListItem(bulletedList, "Third bullet item");

        document.InsertList(numberedList);
        document.InsertList(bulletedList);
        document.Save();
        Console.WriteLine("\tCreated: docs\Lists.docx");
    }

查找Reference From Here

我的计算机上没有 Office Interop,但您可以尝试使用 DocX 构建列表,将其写入文件,然后将该列表从所述文件插入到您的文档中。

像这样:

using System.Collections.Specialized;
...
...

DocX doc = DocX.Create("bullet-text.docx");

var firstItem = bulletList[0];
var firstItemLevel = firstItem.ToList().Count(c => c == '\');
// Using full Namespace to avoid ambiguous reference error.
Xceed.Words.NET.List list = doc.AddList(firstItem.Replace("\", ""), firstItemLevel, ListItemType.Numbered);

for (var i = 1; i < count; i++)
{
    var currentItem = bulletList[i];
    var item = currentItem.Replace(@"\", "");
    int listLevel = currentItem.ToList().Count(c => c == '\')

    doc.AddListItem(list, item, listLevel, ListItemType.Numbered);

}

doc.InsertList(list);

doc.Save();

// Collapse the range to the end, as to not overwrite it. Unsure if you need this
range.Collapse(WdCollapseDirection.wdCollapseEnd);

// Insert into the selected range
range.InsertFile(Environment.CurrentDirectory + "\bullet-text.docx");

我引用的参考资料:

Nested Bulleted lists in Novacode docx

Collapse

InsertFile