我正在开发一个应用程序,该应用程序需要将用户输入(包括富文本编辑器)中的内容插入 Word 文档。为此,我使用 DocX 库 ( http://docx.codeplex.com/ )。该库提供了一种非常简洁的方式来执行某些任务,并且只要您只需将内容插入到一个空文档中,它就可以很好地工作。
但是,我需要插入的文档是一个已经包含一些内容的模板。我需要的是能够在文档中的此内容之后插入用户输入。像这样:
这里有一些默认内容。
[这是我想要的内容]
这里有一些其他的默认内容。
DocX 有在文档中插入段落和列表的方法:
using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(PathToTestFile))
{
var doc = DocX.Load(stream); //will have two paragraphs
var p = doc.InsertParagraph(); //adds a new, empty paragraph to the end of the document
var list = doc.AddList(listType: ListItemType.Numbered); //adds a list
doc.AddListItem(list, "Test1", listType: ListItemType.Numbered); //adds a listitem to the list
doc.InsertList(list); //adds a list to the end of the document
}
段落也有一种在 og 之后插入某些对象的方法,例如表格或其他段落:
//given a Paragraph p and another Paragraph newP:
p.InsertParagraphAfterSelf(newP);
列表具有相同的方法,但两者都没有对其他列表执行相同操作的选项(即,我不能与上面的列表示例相同)。为此,我需要文档中段落或列表的索引。这将允许我使用接受索引作为参数的插入方法。
DocX 类有这个(从 DocX 源代码中提取:http ://docx.codeplex.com/SourceControl/latest#DocX/DocX.cs ):
// A lookup for the Paragraphs in this document.
internal Dictionary<int, Paragraph> paragraphLookup = new Dictionary<int, Paragraph>();
这本字典是内部的,这意味着我无法访问它。在我的一生中,我无法找到任何其他查找索引的方法,但它必须是一种方法,因为有些方法需要这个索引。有没有人遇到过同样的问题?一个解决方案将非常受欢迎!