How to add contentcontrols in bookmarks?

I need to add content controls in the same place as bookmarks. What should I do?

the examples as below:
the first document only has bookmark OnlyBookmark.zip (9.1 KB)
the second document has both bookmark and content control
Bookmark&&Contentcontrol.zip (22.7 KB)

@Anghost

In your case, we suggest you following solution.

  1. Iterate over bookmarks.
  2. Get the text of bookmark using Bookmark.Text property.
  3. Remove the bookmark nodes.
  4. Move the cursor to the bookmark using DocumentBuilder.MoveToBookmark method.
  5. Create StructuredDocumentTag node and insert it in the bookmark using DocumentBuilder.InsertNode method.

Moreover, we suggest you please read the following articles.
Aspose.Words Document Object Model
Use DocumentBuilder to Insert Document Elements
Using DocumentBuilder to Modify a Document Easily

I want to keep both bookmark and content control at the same text in document. Could you Can you write a sample code?

@Anghost

Please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "OnlyBookmark.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    ArrayList nodes = GetRunNodesofBookmark(bookmark, doc);
    StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
    sdt.RemoveAllChildren();
    foreach (Node node in nodes)
    {
        sdt.AppendChild(node);
    }
    builder.MoveToBookmark(bookmark.Name, true, true);
    builder.InsertNode(sdt);
}

doc.Save(MyDir + "output.docx");

public static ArrayList GetRunNodesofBookmark(Bookmark bookmark, Document doc)
{
    ArrayList nodes = new ArrayList();
    ArrayList removenodes = new ArrayList();
    Node currentNode = bookmark.BookmarkStart;

    while (currentNode != bookmark.BookmarkEnd)
    {
        currentNode = currentNode.NextPreOrder(doc);
        if (currentNode.NodeType == NodeType.Run)
        {
            nodes.Add(currentNode.Clone(true));
            removenodes.Add(currentNode); 
        }
    }

    foreach (Node node in removenodes)
    {
        node.Remove();
    }

    return nodes;
}