Inserting a Table at a Cursor Position in a Document

I have a template document that needs to have tables of data inserted at certain points. I can have some kind of tags in the text to indicate where the tables should be inserted. Ho can I position the cursor using a string like <<table 1>> remove that content, and insert a table where the string was?

Hi Curtis,

Thanks for your inquiry. Please use the following code snippet to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
FindTable obj = new FindTable();
doc.Range.Replace(new System.Text.RegularExpressions.Regex(@"<>"), obj, false);

// Remove nodes
foreach (Node node in obj.nodes)
{
    node.Remove();
}
doc.Save(MyDir + "AsposeOut.docx");
public class FindTable : IReplacingCallback
{
    // Store Matched nodes in array list
    public ArrayList nodes = new ArrayList();
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;
        nodes.Add(currentNode);
        DocumentBuilder builder = new DocumentBuilder((Document)currentNode.Document);
        builder.MoveTo(currentNode);
        // Code to insert table.
        // Your Code....
        return ReplaceAction.Skip;
    }
}

I suggest you to read following documentation links for your kind reference.
https://docs.aspose.com/words/java/find-and-replace/
https://docs.aspose.com/words/net/find-and-replace/

Hope this helps you. Please let us know if you have any more queries.

Thanks for the help, that works great. I have another question. I need to have portions of a template document that need to be extracted, have wildcard strings replaced, and be inserted back into the original document. How would I do that? An example would be:
Original document:
This is my original document.

This paragraph is like a sub template, my monthly expense on food for <> was <>.

What a great document it is.
Resulting document:
This is my original document.
This paragraph is like a sub template, my monthly expense on food for November was $100.00.
This paragraph is like a sub template, my monthly expense on food for December was $250.
What a great document it is.

Hi Curtis,

Thanks for your inquiry.

dowings:
I need to have portions of a template document that need to be extracted, have wildcard strings replaced, and be inserted back into the original document. How would I do that? An example would be:

You can replace <> and <> with your desired values by using the code shared in my last post. Do you only want to replace the contents between** and** ?

You can extract contents from MS Word document and save those contents to another document by using the approach shared here:
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/

After extracting contents, you may use the code shared here to replace <> and <> with specific values.

The requirement is to extract the content bounded by the and tags, deleting it from the source document, then inserting it back at the same position with the internal string replacements one or more times based on driving data. I saw the replacing content between paragraphs code but couldn’t see how to put it together to meet the requirements. I was hoping you could help with an example.
Thanks -
Curtis

Hi Curtis,

Thanks for sharing the details. Please use the following code snippet to achieve your requirement. Hope this helps you. I have attached the ReplaceEval class and InsertDocument method with this post.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Step 1 - Get the start and end paragraphs ( and )
Paragraph startPara = null;
Paragraph endPara = null;
NodeCollection startTags = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in startTags)
{
    if (para.ToString(SaveFormat.Text).Contains(""))
    {
        builder.MoveTo(para);
        // Start Bookmark, this will help to remove contents
        builder.StartBookmark("MyBookmark");
        startPara = para;
        break;
    }
}
foreach (Paragraph para in startTags)
{
    if (para.ToString(SaveFormat.Text).Contains(""))
    {
        endPara = para;
        builder.MoveTo(para);
        // End Bookmark, this will help to remove contents
        builder.EndBookmark("MyBookmark");
        break;
    }
}
// Step2 - Extract contents for tags and 
ArrayList extractedNodes = ExtractContent(startPara, endPara, true);
// Generate document from extracted nodes
Document dstDoc = GenerateDocument(doc, extractedNodes);
// Step3 - Replace contents
// Replace month value to 'November'
dstDoc = ReplaceWithEvaluator(dstDoc, "month", "November");
// Step4 - Remove old contents
// Set the bookmark text to empty to remove contents of tag
doc.Range.Bookmarks["MyBookmark"].Text = "";
// Step5
// Insert the replaced contents between and to original document.
InsertDocument(startPara, dstDoc);
// Remove bookmark
doc.Range.Bookmarks["MyBookmark"].Remove();
doc.Save(MyDir + "AsposeOut.docx");
public Document ReplaceWithEvaluator(Aspose.Words.Document wordDoc, string OldValue, string NewValue)
{
    ReplaceEval.ReplaceText = NewValue.Replace("\n", "");
    ReplaceEval.OldValue = OldValue;
    wordDoc.Range.Replace(new System.Text.RegularExpressions.Regex(@"\<<" + OldValue + @"\>>"), new ReplaceEval(), false);
    return wordDoc;
}

If you still face problem, please create sample input and output documents by using MS Word and share those documents with us. We will share the code according to shared documents.