@purusadh
In your expected output document, the first paragraph has text “RATIONALE” and last paragraph has text “FIGURE 1 - FUSE CONFIGURATION/DIMENSIONS”. You need to insert the bookmarks for these two paragraphs in your input document.
We have inserted the bookmarks into these paragraph in your document using MS Word and attached it along with output document.
Docs.zip (1.1 MB)
Following code example shows how to extract content between two bookmarks and copy header and footer from one document into another. Hope this helps you.
Document doc = new Document(MyDir + "modified AS28938A.doc");
Bookmark bm1 = doc.getRange().getBookmarks().get("extractcontent1");
Bookmark bm2 = doc.getRange().getBookmarks().get("extractcontent2");
ArrayList<Node> extractedNodes = extractContent(bm1.getBookmarkStart(), bm2.getBookmarkStart(), false);
Document dstDoc = generateDocument(doc, extractedNodes);
//Copy header and footer
Section section = (Section) bm1.getBookmarkStart().getAncestor(NodeType.SECTION);
for(HeaderFooter headerFooter : section.getHeadersFooters())
{
HeaderFooter header = dstDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(headerFooter.getHeaderFooterType());
if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(dstDoc, headerFooter.getHeaderFooterType());
dstDoc.getFirstSection().getHeadersFooters().add(header);
}
for (Node srcNode : (Iterable<Node>)headerFooter.getChildNodes())
{
Node dstNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
header.appendChild(dstNode);
}
}
dstDoc.getFirstSection().getPageSetup().setDifferentFirstPageHeaderFooter(section.getPageSetup().getDifferentFirstPageHeaderFooter());
dstDoc.save(MyDir + "output.docx");