How to insert file content after specific bookmark that is surrounded by further bookmarks

Hello Aspose team,

i would like to insert content of another document right after a specific bookmark of my document.
My problem: This bookmark is surrounded by further bookmarks, and i do not know how to insert after this bookmark.

Here are my example files:

  • DocObjectShape.docx (18.6 KB)
    This is document to be inserted in WordTestShape.docx
  • WordTestShape.docx (17.6 KB)
    My target dokument that contains several bookmarks (BM1,BM2,BM3).
    Document DocObjectShape.docx should be inserted after bookmark BM2.

Do you have any ideas to solve my problem?

Best regards
Matthias

@curmas You can use the following code to achieve this:

Document dst = new Document("C:\\Temp\\WordTestShape.docx");
Document src = new Document("C:\\Temp\\DocObjectShape.docx");
    
DocumentBuilder builder = new DocumentBuilder(dst);
// Move to the bookmark.
builder.moveToBookmark("BM1", false, true);
// Insert source document.
builder.insertDocument(src, ImportFormatMode.USE_DESTINATION_STYLES);
    
dst.save("C:\\Temp\\out.docx");

Hello @alexey.noskov,

perfect, that works very well. Thank you for this tip. It really helps me a lot.

Best regards
Matthias

1 Like