Thanks for you response. I used to the described technique in the mentioned Post, but had some problems (weird output). After searching around in the forum I found a bunch of similar techniques (mainly posted by the User: alexey.noskov) describing the exact same problem as I posted. I tried various techniques and ended up with the following solution (which seems to be the most recent on I’ve found):
This solutions seems to work fine for us, for the documents we tested it against. The only concerns I have is, that the technique looks very complex and it’s hard to understand fully. And since it is a pretty common usage, it might by worth it to let aspose.word providing this technique either within the wiki, or as a function within the code. Within each post I found the technique, something has been added or changed in order to fix a bug within the algorithm or the enhance it. Thus I’d really appreciate if this algorithm is maintained somewhere in aspose (wiki or within the code). From a customer point of view, I’d be able to get the most recent algorithm in every release (if it is directly in the code) or as soon as I’m having a problem within that algorithm I’d be able to look the most recent one up within the wiki.
However thanks again for your code.
private void insertBuildingBlockAtBookmark(String bookmarkName, Document mainDocument, BuildingBlock buildingBlock) throws Exception {
NodeImporter importer = new NodeImporter(buildingBlock.getDocument(), mainDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(mainDocument);
// Move cursor to bookmark and insert paragraph break
builder.moveToBookmark(bookmarkName);
builder.writeln();
// If current paragraph is a list item, we should clear its formating.
if (builder.getCurrentParagraph().isListItem()) {
builder.getListFormat().removeNumbers();
}
// Content of srcdoc will be inserted after this node
Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling();
// Content of first paragraph of srcDoc will be apended to this parafraph
Paragraph insertAfterParagraph = null;
if (insertAfterNode.getNodeType() == NodeType.PARAGRAPH) {
insertAfterParagraph = (Paragraph) insertAfterNode;
}
// Content of last paragraph of srcDoc will be apended to this parafraph
Paragraph insertBeforeParagraph = builder.getCurrentParagraph();
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.getParentNode();
// Remove empty paragraphs from the end of document
while (!((CompositeNode) buildingBlock.getLastSection().getBody().getLastChild()).hasChildNodes()) {
buildingBlock.getLastSection().getBody().getLastParagraph().remove();
if (buildingBlock.getLastSection().getBody().getLastChild() == null) {
break;
}
}
// Loop through all sections in the source document.
for (Section srcSection : buildingBlock.getSections()) {
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
for (int nodeIdx = 0; nodeIdx < srcSection.getBody().getChildNodes().getCount(); nodeIdx++) {
Node srcNode = srcSection.getBody().getChildNodes().get(nodeIdx);
// Do not insert node if it is a last empty paragarph in the section.
Paragraph para = null;
if (srcNode.getNodeType() == NodeType.PARAGRAPH) {
para = (Paragraph) srcNode;
}
if ((para != null) && para.isEndOfSection() && (!para.hasChildNodes())) {
break;
}
// If current paragraph is first paragraph of srcDoc
// then appent its content to insertAfterParagraph
boolean nodeInserted = false;
if (para != null && para.equals(buildingBlock.getFirstSection().getBody().getFirstChild())) {
nodeInserted = true; // set this flag to know that we already processed this node.
// !String.IsNullOrEmpty(insertAfterParagraph.toTxt().trim()
if (!(insertAfterParagraph.toTxt() == null || insertAfterParagraph.toTxt().trim().equals(""))) {
for (int i = 0; i < para.getChildNodes().getCount(); i++) {
Node node = para.getChildNodes().get(i);
Node dstNode = importer.importNode(node, true);
insertAfterParagraph.appendChild(dstNode);
}
} else {
Paragraph dstNode = (Paragraph) mainDocument.importNode(para, true);
insertAfterParagraph.getParentNode().insertAfter(dstNode, insertAfterParagraph);
while (insertAfterParagraph.hasChildNodes()) {
dstNode.prependChild(insertAfterParagraph.getLastChild());
}
insertAfterParagraph.remove();
insertAfterParagraph = dstNode;
insertAfterNode = dstNode;
}
// If subdocument contains only one paragraph
// then copy content of insertBeforeParagraph to insertAfterParagraph
// and remove insertBeforeParagraph
if (buildingBlock.getFirstSection().getBody().getFirstParagraph()
.equals(buildingBlock.getLastSection().getBody().getLastChild())) {
while (insertBeforeParagraph.hasChildNodes()) {
insertAfterParagraph.appendChild(insertBeforeParagraph.getFirstChild());
}
insertBeforeParagraph.remove();
}
}
// Fix: Avoid if the current paragraph is the first and last paragraph
else
// If current paragraph is last paragraph of srcDoc
// then appent its content to insertBeforeParagraph
if (para != null && para.equals(buildingBlock.getLastSection().getBody().getLastChild())) {
nodeInserted = true; // set this flag to know that we already processed this node.
Paragraph dstNode = (Paragraph) mainDocument.importNode(para, true);
insertBeforeParagraph.getParentNode().insertBefore(dstNode, insertBeforeParagraph);
while (insertBeforeParagraph.hasChildNodes()) {
dstNode.appendChild(insertBeforeParagraph.getFirstChild());
}
insertBeforeParagraph.remove();
insertBeforeParagraph = dstNode;
}
if (!nodeInserted) {
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = mainDocument.importNode(srcNode, true);
// Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}