C#:使用Office Interop库在Word文档中的书签处插入并缩进项目符号点 [英] C#: Insert and indent bullet points at bookmark in word document using Office Interop libraries

查看:316
本文介绍了C#:使用Office Interop库在Word文档中的书签处插入并缩进项目符号点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's my dilemma. I've been given a task to generate a specific section of an existing word document based on user input from a web front end. The back end of the system is being written in C# with the part that edits the word document using the Microsoft.Office.Interop.Word namespace.

基本上,他们将从可用指令的列表中进行选择,每个指令均属于string类型,然后将用于生成文档的指令部分,每个单独的指令均是列表中的另一个项目符号.这部分工作正常.我的问题是,指令中可能包含字符\,需要将其替换为缩进,或者如果您用文字打开了文档,则在项目符号中击中TAB等效.到目前为止,我已经能够将项目符号插入列表的中间,它会继续按预期对它们进行适当的编号.更重要的是,我无法根据需要缩进它们.

Basically, they would select from a list of available instructions, each being of the type string, which will then be used to generate the instructions part of the document, with each separate instruction being another bullet in the list. This part works fine. My problem is, the instructions can contain the character \, which need to be replaced with an indent, or the equivalent of hitting TAB while in the bullet if you had the document opened in word. So far I'm able to get it to insert the bullets into the middle of the list just fine, it continues numbering them appropriately as expected. The kicker is I can't get it to indent them as needed.

我已经在这里和其他一些站点上找到了几乎所有可以使用的示例,但都没有成功.最新的迭代在下面的代码中,该代码仅缩进整个列表.

I've tried pretty much all of the examples I was able to find here and on a few other sites to get this to work but with no success. The latest iteration is in the code below, which just indents the entire list as far as it can go.

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();

所以我的代码输出应为:

So the output of my code should be:

  • 1点A
    • 1.1点B
    • 1.2点C
      • 1.2.1 D点
      • 1 Point A
        • 1.1 Point B
        • 1.2 Point C
          • 1.2.1 Point D

          这是我第一次真正需要使用Office Interop库,因此我很肯定这里缺少某些东西.任何帮助将不胜感激.

          This is the first time I've ever really had to work with the Office Interop libraries so I'm positive there's something I'm missing here. Any help would be greatly appreciated.

          推荐答案

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

          I don't have Office Interop on my computer, but you could try building the list with DocX, writing it to a file, then inserting that list into your document, from said file.

          类似这样的东西:

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

          我引用过的参考文献:

          Novacode docx中的嵌套项目符号列表

          如何将文件嵌入到word docx中?

          折叠

          插入文件

          这篇关于C#:使用Office Interop库在Word文档中的书签处插入并缩进项目符号点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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