Merging DOCX file does not add Section Breaks

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);
	}

@Louis.Coutinho In your case you should use built-in Document.appendDocument method. Also, there is a built-in DocumentBuilder.insertDocument method, which does almost the same as your implementation.
Please see our documentation to learn more.

Thanks. Will try it out.

1 Like

The Document.appendDocument method works. It appends the content of my cover page document to at the end of the main document.

My requirement is to add the content of the cover page document (it’s a single page document) as the first page in the main document.

I tried reversing the logic by adding the document content from the main document to the cover page document (via Document.appendDocument), but the formatting in the result document is quite different than my main document. The number of pages are no longer the same.

Never mind my query. I was able to achieve what I wanted to by using the DocumentBuilder.insertDocument API

@Louis.Coutinho It is perfect that you managed to achieve what you need. Please feel free to ask in case of any issues, we will be glad to help you.