We are trying to merge an entire DOCX file into the parent DOCX file. When we do this, the Section breaks are not being transferred into the new file. We are using the following API. The code comment mentions that the section format and breaks are ignored. What is that we will need to add to this segment so that the section format and breaks are inserted propertly:
/**
* Inserts content of the external document after the specified node.
* Section breaks and section formatting of the inserted document are
* ignored.
*
* @param insertAfterNode Node in the destination document after which the
* content should be inserted. This node should be a block level node
* (paragraph or table).
* @param srcDoc The document to insert.
*/
public static void insertDocument(Node insertAfterNode, Document srcDoc) throws Exception
{
// Make sure that the node is either a paragraph or table.
if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH)
& (insertAfterNode.getNodeType() != NodeType.TABLE))
{
throw new IllegalArgumentException("The destination node should be either a paragraph or table.");
}
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.getParentNode();
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Loop through all sections in the source document.
for (int g = 0; g < srcDoc.getSections().getCount(); ++g)
{
Section srcSection = srcDoc.getSections().get(g);
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
;
//for (Node srcNode : (Iterable<Node>) srcSection.getBody())
for (int r = 0; r < srcSection.getBody().getChildNodes().getCount(); ++r)
{
// Node srcNode = srcSection.getBody().getChildNodes().get(r).deepClone(true);
Node srcNode = srcSection.getBody().getChildNodes().get(r);
// Let's skip the node if it is a last empty paragraph in a section.
if (srcNode.getNodeType() == (NodeType.PARAGRAPH))
{
Paragraph para = (Paragraph) srcNode;
if (para.isEndOfSection() && !para.hasChildNodes())
{
continue;
}
}
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.importNode(srcNode, true);
// Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
Document oDestDoc = (Document)insertAfterNode.getDocument();
oDestDoc.getLastSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
}