Move bookmark to a newly created paragraph

I have a template document with structure like this:
image.png (6.5 KB)

What I want: using C#, to move the bookmark to a paragraph. The reason is that I use the bookmark’s parent node to insert another document at the bookmark, and in this case it parent node happens to be a Body so InsertAfter does not work. This is a template provided by our client and I don’t want to modify the document itself using Word.

What I tried: Go to the bookmark -> Remove it --> Insert Paragraph --> Start then End Bookmark with the same name

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark(“bm”);

bookmark.Remove();
builder.InsertParagraph();
builder.StartBookmark(“bm”);
builder.EndBookmark(“bm”);

But the bookmark keeps appearing the paragraph below, not the inserted one
image.png (9.3 KB)

I figured out int document that InsertParagraph() does not return the inserted paragraph but still the CurrentParagraph

So to summarize, I need to know:

  • Is there any other way to move bookmark to a new paragraph in my case?

  • How do we get the inserted paragraph using InsertParagraph() ?

Thanks in advance!

@viethieule,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified source Word document(s)
  • Aspose.Words for .NET 21.1 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words for .NET.

Bookmark with Docvar.zip (43.1 KB)

@awais.hafeez
Please find these in the zip file:

  • unexpected_result_document.docx: the undesired behavior

  • expected_result_document: the desired output

  • document.docx: the file the will be modified to get the desired output

  • Console app source code, you can see comment for some of my problems

NOTE:

  • Please enable the DOCVARIABLE view, the bookmark is placed right behind a DOCVARIABLE and I think the issue stems from here

  • The bookmark name is “particulars”, you can Go to the bookmark in Work to see

  • ASPOSE version is 20.5 because we use this in production

  • There are two issue I need to understand here:

  1. At line 38, the ParentNode of BookmarkStart is Body so the InsertAfter later failed. In the previous version we used (~17) the ParentNode is Paragraph
  2. Since it failed, I tried to move the bookmark. It works but not my desired output. After line 75, the cursor is still at the CurrentParagraph, not the newly inserted one (see the unexpected output)
    –> How do I get the newly inserted paragraph here to add the bookmark (with same name) ?

Thank you very much!

P/s: Apologize if my clarification is quite complex, just reply if there is anything you dont understand. Thanks again

@viethieule,

The problem occurs because of a Block Level Bookmark. During processing with Aspose.Words, your code makes that Bookmark’s nodes Inline Level children of Paragraph. Can you please check if the following solution is acceptable for you?

Document doc = new Document("C:\\Temp\\Bookmark with Docvar\\document.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Bookmark bookmark = doc.Range.Bookmarks[_bookmarkName];
if (bookmark != null)
{
    Document insertDoc = new Document("C:\\Temp\\Bookmark with Docvar\\document_to_insert.docx");

    if (bookmark.BookmarkStart.ParentNode.NodeType == NodeType.Body && // This tells that it is a Block Level Bookmark
        bookmark.BookmarkStart.NextSibling == bookmark.BookmarkEnd) // This tells that Bookmark is empty and there is no Node between them
    {
        if (bookmark.BookmarkEnd.NextSibling.NodeType == NodeType.Paragraph) // Can be a Table or other type Block Level Node
        {
            Paragraph nextPara = (Paragraph)bookmark.BookmarkEnd.NextSibling; // We will move nodes before this Para after document insertion

            builder.MoveToBookmark(_bookmarkName, true, false);
            builder.InsertDocument(insertDoc, ImportFormatMode.KeepSourceFormatting); // Built-in method to Insert Document into another Word document

            // Restore Bookmark's actual position
            nextPara.ParentNode.InsertBefore(bookmark.BookmarkStart, nextPara);
            nextPara.ParentNode.InsertBefore(bookmark.BookmarkEnd, nextPara);
        }
    }
    else
    {
        builder.MoveToBookmark(_bookmarkName, true, false);
        builder.InsertDocument(insertDoc, ImportFormatMode.KeepSourceFormatting);
    }

    doc.Save(@"C:\Temp\Bookmark with Docvar\\21.1.docx");
}