Inserting Bookmark Programmatically

Hi,
I have a document template (Postcard.doc).
The document has got a table and two rows.
I want to insert a bookmark at the top of the table and not inside.
If I use : builder.StartBookmark(“TestBookmark”);, then it is inserting in to the first cell of the table.

What I really want is there in the attached document: Postcard_BM.doc

I did that manually by cutting the table, inserting the bookmark at the top and pasting the table back.

Can I achieve similar functionality with Aspose.Word.

Thanks!

Hi

Thanks for your request. In the document created in MS Word there is a paragraphs before table and bookmarks is inserted into this paragraph. You can achieve the same using Aspose.Words. See the following code:

Document doc = new Document(@"Test001\PostCard.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Check whether the first node in the docuemnt is not Paragraph,
// If so add paragraph before this table.
if (doc.FirstSection.Body.FirstChild.NodeType != NodeType.Paragraph)
{
    Paragraph par = new Paragraph(doc);
    doc.FirstSection.Body.InsertBefore(par, doc.FirstSection.Body.FirstChild);
    // Move DocumentBuilder cursor to the newly created paragraph.
    builder.MoveTo(par);
    // Insert Bookmark.
    string bkName = "MyBookmark";
    builder.StartBookmark(bkName);
    builder.EndBookmark(bkName);
}
// Save output document.
doc.Save(@"Test001\out.doc");

Hope this helps.
Best regards.

Thanks Alexey.
Works great.