通过迭代在模板(word)上使用 Aspose

Working with Aspose on a Template (word) with iterations

我正在尝试设置 Word 文档。在本文档中,我想复制我的问卷。所以我需要迭代我所有的类别,然后是我的问题,然后是答案。如果可能的话,我不想在代码中配置所有内容(字体和大小等),因为我有一个模板。

我解决这个问题的方法是复制每个类别的模板标题,并用类别名称替换一些文本。之后,我将以相同的方式处理问题和答案(当然是特定变量)

我的问题是我找不到标记我的模板头部以便复制它的方法。另一个问题是我不知道如何替换复制的占位符,我想在其中设置我的类别名称。

如果有人能帮助我,我将不胜感激。

ps:在 aspose 论坛中,每次 link 都失败了,这令人沮丧。

与其在迭代中复制模板中的某些内容,更好的方法是在模板中创建一个 BuildingBlock 并根据需要插入它。您将作为最终用户创建 BuildingBlock,将其保存在模板中。

在模板中写入一些占位符文本并将其添加为书签,作为插入 BuildingBlock 的目标。

然后您从模板创建一个新文档并使用它,这样模板就不会被更改:

 Dim wdDoc as Word.Document = WordApplication.Documents.Add stringTemplatePath
Dim BookmarkTarget as Word.Bookmark = wdDoc.Bookmarks(BookmarkName)

使用 Insert 方法插入 BuildingBlock:

WordApplication.Templates( pathToTemplateFile) _
    .BuildingBlockEntries(stringBuidlingBlockName).Insert(Where:=BookmarkTarget.Range, RichText:=True)

为了在 BuildingBlock 中标记您想要作为数据输入目标的内容,您有两种选择:Bookmark 或 ContentControl。两者都可以满足要求 "replace the copied placeholder, where I want to set my category name"。在所有条件都相同的情况下,我通常会在使用 Interop 时为此选择一个书签。当您向其中写入文本时,它将自动删除。例如:

 wdDoc.Bookmarks(Bookmark2Name).Range.Text = stringCategoryInfo

在你的帮助下,我已经解决了。我不会post的文件,因为里面主要是德文写的。 我使用了模板文档和输出文档。在我的模板文档中声明书签,以定义我要复制的部分。从模板文档中获取我需要的书签并复制到输出文档中。

要复制书签,我使用了以下代码:

private void AppendBookmarkedText(NodeImporter importer, Bookmark srcBookmark, CompositeNode dstNode)

{

// This is the paragraph that contains the beginning of the bookmark.

Paragraph startPara = srcBookmark.BookmarkStart.ParentNode as Paragraph;

// This is the paragraph that contains the end of the bookmark.

Paragraph endPara = srcBookmark.BookmarkEnd.ParentNode as Paragraph;

if ((startPara == null) || (endPara == null))

throw new InvalidOperationException("Parent of the bookmark start or end is not a paragraph, cannot handle this scenario yet.");

// Limit ourselves to a reasonably simple scenario.

if (startPara.ParentNode != endPara.ParentNode)

throw new InvalidOperationException("Start and end paragraphs have different parents, cannot handle this scenario yet.");

// We want to copy all paragraphs from the start paragraph up to (and including) the end paragraph,

// therefore the node at which we stop is one after the end paragraph.

Node endNode = endPara.NextSibling;

// This is the loop to go through all paragraph-level nodes in the bookmark.

for (Node curNode = startPara; curNode != endNode; curNode = curNode.NextSibling)

{

// This creates a copy of the current node and imports it (makes it valid) in the context

// of the destination document. Importing means adjusting styles and list identifiers correctly.

Node newNode = importer.ImportNode(curNode, true);

// Now we simply append the new node to the destination.

dstNode.AppendChild(newNode);

}

是的,它只是从这个 aspose 网站复制的:https://www.aspose.com/community/forums/post/65476.aspx?Ajax_CallBack=true

在我能够从模板文档中复制内容并将其粘贴到输出文档中之后,使用它就很容易了。 我在每个循环中只做了这几行:

            CompositeNode dstNode = myOutDoc.LastSection.Body;
            Bookmark mySpecialBookmark= myTemplate.Range.Bookmarks["mySpecialBookmark"]; 

            mySpecialBookmark.Text = mySpecialBookmark.Text.Replace("<myChangingWord>", myObj.newWord);
            AppendBookmarkedText(importer, mySpecialBookmark, dstNode);
            mySpecialBookmark.Text = mySpecialBookmark.Text.Replace(myObj.newWord, "<myChangingWord>");

为了解释,我曾经在“<>”中设置我的替换文本,以在我的文档中阐明哪些文本用于占位符,而另一些则不是。

谢谢你的帮助:) (我希望你能理解我写下的内容,我知道我的英语是最差的)