Hi
Thanks for your inquiry. I think that you are using InsertDocumentAtMergeField method. Right? If so you can try modifying your method as the following.
public static void InsertDocumentAtMergeField(String mergeFieldName, Document dstDoc, Document srcDoc) throws Exception
{
//Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(dstDoc);
//Move cursor to bookmark and insert paragraph break
builder.moveToMergeField(mergeFieldName);
builder.writeln();
//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 = (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();
//Create temporary list
List srcTmpList = null;
List dstTmpList = null;
//Remove empty paragraphs from the end of document
while (!srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes())
{
srcDoc.getLastSection().getBody().getLastParagraph().remove();
}
//Loop through all sections in the source document.
int sectCount = srcDoc.getSections().getCount();
for(int sectIndex=0; sectIndex<sectCount; sectIndex++)
{
Section srcSection = srcDoc.getSections().get(sectIndex);
//Loop through all block level nodes (paragraphs and tables) in the body of the section.
int nodeCount = srcSection.getBody().getChildNodes().getCount();
for(int nodeIndex=0; nodeIndex<nodeCount; nodeIndex++)
{
Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);
//Do not insert node if it is a last empty paragarph in the section.
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
if (para.equals(srcDoc.getFirstSection().getBody().getFirstParagraph()) && insertAfterParagraph.hasChildNodes())
{
int firstParaChildCount = para.getChildNodes().getCount();
for(int childIndex = 0; childIndex<firstParaChildCount; childIndex++)
{
Node node = para.getChildNodes().get(childIndex);
Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
insertAfterParagraph.appendChild(dstNode);
}
//If subdocument contains only one paragraph
//then copy content of insertBeforeParagraph to insertAfterParagraph
//and remove insertBeforeParagraph
if (srcDoc.getFirstSection().getBody().getFirstParagraph().equals(srcDoc.getLastSection().getBody().getLastParagraph()))
{
while (insertBeforeParagraph.hasChildNodes())
{
insertAfterParagraph.appendChild(insertBeforeParagraph.getFirstChild());
}
insertBeforeParagraph.remove();
}
}
//If current paragraph is last paragraph of srcDoc
//then appent its content to insertBeforeParagraph
else if (para.equals(srcDoc.getLastSection().getBody().getLastParagraph()) && insertBeforeParagraph.hasChildNodes())
{
Node previouseNode = null;
int firstParaChildCount = para.getChildNodes().getCount();
for(int childIndex = 0; childIndex<firstParaChildCount; childIndex++)
{
Node node = para.getChildNodes().get(childIndex);
Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
if (previouseNode == null)
{
insertBeforeParagraph.insertBefore(dstNode, insertBeforeParagraph.getFirstChild());
}
else
{
insertBeforeParagraph.insertAfter(dstNode, previouseNode);
}
previouseNode = dstNode;
}
}
else
{
//This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
if(srcNode.getNodeType() == NodeType.PARAGRAPH)
{
Paragraph srcTmpPar = (Paragraph)srcNode;
Paragraph dstTmpPar = (Paragraph)newNode;
if(srcTmpPar.getListFormat().getList()==srcTmpList)
{
dstTmpPar.getListFormat().setList(dstTmpList);
}
else
{
srcTmpList = srcTmpPar.getListFormat().getList();
dstTmpList = dstTmpPar.getListFormat().getList();
}
}
//Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
if(!insertAfterParagraph.hasChildNodes())
insertAfterParagraph.remove();
if(!insertBeforeParagraph.hasChildNodes())
insertBeforeParagraph.remove();
}
}
Hope this helps.
Best regards.
Alexey Noskov
Developer/Technical Support
Aspose Auckland Team