Search code examples
.netms-wordaspose

Working with Aspose on a Template (word) with iterations


I'm trying to set up a Word document. In this document i want to replicate my questionnaires. So i need to iterate for all my categories, then for my questions and then for the answers. If it's possible I don't want to configure everything in my code (font and size and so on) because I have a template.

My way to solve this would be to copy the template head for each category and replace some text with the category name. After it I would work the same way for the questions and the answers (specific variable of course)

My problem is i don't find a way to mark my template head so I can copy it. The other problem is I don't know how to replace the copied placeholder, where I want to set my category name.

If someone can help me I would really appreciate it.

ps: in the aspose forum is like every link down and this is frustrating.


Solution

  • I have solved it, with your help. I will not post the documents because there are mainly written in German. I used a template doc and an output doc. Declared bookmarks in my template Doc, to define wich part i want to copy. Get the bookmarks i needed from the template doc and copied into the output doc.

    To copy the bookmarks i used following code:

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

    yep, it's just copied from this aspose website: https://www.aspose.com/community/forums/post/65476.aspx?Ajax_CallBack=true

    After i was able to copy content from template doc and past it in the output doc, it was easy to work with it. I only did these few lines in every loop:

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

    For explain I used to set my replaced text in '<>' to clarify in my document which Text is for Placeholding something an wich is not.

    Thanks for your help anyways :) (and i hope you understand what i have written down, i know my English is the worst)