Insert document inline without creating a break

I would like to insert several documents into another document during mail merge.

The documents are merged, but there is a break after each inserted document. How can I remove the break after inserting?

The expected result:

We would like this to be on the same line, with the other text right here and also this text.

But instead I get:

We would like this to be on the same line,
with the other text right here
and also this text.

I have followed the example from: Insert a Document During Mail Merge
Where I use builder.insertDocument(), instead of the insertDocument() method from the example. The insertDocument() method in the example inserts the documents in the wrong order and also creates breaks after each insert.

Can be reproduced with the attached Java project:
AsposeTest.zip (30.3 KB)

@joensen

Thanks for your inquiry. You are facing the expected behavior of Aspose.Words. In your case, we suggest you following solution.

  1. In InsertDocumentAtMailMergeHandler, insert the bookmark before inserting the document.
  2. After mail merger, iterate over inserted bookmarks and merge the paragraphs nodes that contains the bookmark and its next sibling.

Following method shows how to merge two paragraphs.

public static void mergeParagraphs(Paragraph dstPar, Paragraph srcPar)
{
    //move all content from source paragraph into the destination paragraph
    while (srcPar.hasChildNodes())
        dstPar.appendChild(srcPar.getFirstChild());

    //Remove source paragraph
    srcPar.remove();
}

Thank you. That did the trick.

The code to merge bookmarks:

private void mergeBookmarks(Document document) throws Exception {
    DocumentBuilder builder = new DocumentBuilder(document);
    for (Bookmark bookmark : document.getRange().getBookmarks()) {
        builder.moveToBookmark(bookmark.getName());
        Paragraph paragraph = builder.getCurrentParagraph();
        Node nextSibling = paragraph.getNextSibling();
        if (nextSibling instanceof Paragraph) {
            mergeParagraphs(paragraph, (Paragraph) nextSibling);
        }
    }
}

private static void mergeParagraphs(Paragraph dstPar, Paragraph srcPar) {
    while (srcPar.hasChildNodes()) {
        dstPar.appendChild(srcPar.getFirstChild());
    }
    srcPar.remove();
}

@joensen

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.