How to insert bookmark in some content

I want to insert bookmark in a word doc
insert bookmarkStar at the begin of the content,and insert bookmarkEnd at the end of the content.the content will be found through begin keyword and end keyword,
how to do it ,thanks

@hlgao

Thanks for your inquiry. Please click here to learn about working with bookmark using Aspose.words API. Also please see the exampe code below:

Document doc = new Document("../../data/document.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// ----- Set Bookmark
builder.StartBookmark("AsposeBookmark");
builder.Writeln("Text inside a bookmark.");
builder.EndBookmark("AsposeBookmark");
// ----- Get Bookmark
// By index.
Bookmark bookmark1 = doc.Range.Bookmarks[0];
// By name.
Bookmark bookmark2 = doc.Range.Bookmarks["AsposeBookmark"];
doc.Save("AsposeBookmarks.doc");

Thanks.
but it’s not my result. I want to insert StartBookmark in the begin ,then insert EndBookmark in another place .

Document doc = new Document(dataDir + “02.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph myPara = (Paragraph) doc.getFirstSection().getBody().getChild(NodeType.PARAGRAPH,1,true);
builder.moveTo(myPara);
builder.startBookmark(“mystart”);
builder.moveTo(mypara.getChild(NodeType.RUN, 6, true));
builder.endBookmark(“mystart”);
doc.save(dataDir + “bookmark.docx”);

but the EndBookmark not at the end Node

@hlgao

Thank you for your feedback. You can move the DocumentBuilder to the select Run LastChild. Please have a look at this sample code.

    DocumentBuilder builder = new DocumentBuilder(doc);

    Paragraph myPara = (Paragraph)doc.FirstSection.Body.GetChild(NodeType.Paragraph, 1, true);
    builder.MoveTo(myPara);
    builder.StartBookmark("mystart");

    builder.Writeln("my bookmark here");
    Run run = (Run)myPara.GetChild(NodeType.Run, 6, true);

    //  Clone from selected Run Node
    Run runafter = (Run)run.Clone(true);
    runafter.Text = "";

    run.ParentNode.InsertAfter(runafter, run);

    // Move builder directly to Last Child.
    builder.MoveTo(run.ParentNode.LastChild);

    // OR Move builder to the just inserted empty Run Node.
    builder.MoveTo(runafter);

    builder.EndBookmark("mystart");

Thank you very much. I have solve the problem use the code you provided.

@hlgao

We are glad to hear that it works for you.