/*** @Description 向书签后插入文档
*@parammainDoc 主文档
*@paramtobeInserted 拼接的文档
*@parambookmark 书签
* @Return com.aspose.words.Document
* @Author Mr.Walloce
* @Date 2019/7/27 18:33*/
private staticDocument insertDocumentAfterBookMark(Document mainDoc, Document tobeInserted, String bookmark)throwsException {if (mainDoc == null) {return null;
}else if (tobeInserted == null) {returnmainDoc;
}else{//构建新文档
DocumentBuilder mainDocBuilder = newDocumentBuilder(mainDoc);if (bookmark != null && bookmark.length() > 0) {//获取到书签
BookmarkCollection bms =mainDoc.getRange().getBookmarks();
Bookmark bm=bms.get(bookmark);if (bm != null) {
mainDocBuilder.moveToBookmark(bookmark,true, false);
mainDocBuilder.writeln();//获取到插入的位置
Node insertAfterNode =mainDocBuilder.getCurrentParagraph().getPreviousSibling();
insertDocumentAfterNode(insertAfterNode, mainDoc, tobeInserted);
}
}else{
appendDoc(mainDoc, tobeInserted,true);
}returnmainDoc;
}
}/*** @Description TODO
*@paraminsertAfterNode 插入的位置
*@parammainDoc
*@paramsrcDoc
* @Return void
* @Author Mr.Walloce
* @Date 2019/7/27 14:51*/
private static voidinsertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc)throwsException {if (insertAfterNode.getNodeType() != 8 & insertAfterNode.getNodeType() != 5) {throw new Exception("The destination node should be either a paragraph or table.");
}else{
CompositeNode dstStory=insertAfterNode.getParentNode();while (null !=srcDoc.getLastSection().getBody().getLastParagraph()&& !srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) {
srcDoc.getLastSection().getBody().getLastParagraph().remove();
}
NodeImporter importer= new NodeImporter(srcDoc, mainDoc, 1);int sectCount =srcDoc.getSections().getCount();for (int sectIndex = 0; sectIndex < sectCount; ++sectIndex) {
Section srcSection=srcDoc.getSections().get(sectIndex);int nodeCount =srcSection.getBody().getChildNodes().getCount();for (int nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex) {
Node srcNode=srcSection.getBody().getChildNodes().get(nodeIndex);
Node newNode= importer.importNode(srcNode, true);
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode=newNode;
}
}
}
}/*** @Description 文档拼接
*@paramdstDoc
*@paramsrcDoc
*@paramincludeSection
* @Return void
* @Author Mr.Walloce
* @Date 2019/7/27 14:53*/
private static void appendDoc(Document dstDoc, Document srcDoc, boolean includeSection) throwsException {if(includeSection) {
Iterator var3 =srcDoc.getSections().iterator();while(var3.hasNext()) {
Section srcSection=(Section) var3.next();
Node dstNode= dstDoc.importNode(srcSection, true, 0);
dstDoc.appendChild(dstNode);
}
}else{
Node node=dstDoc.getLastSection().getBody().getLastParagraph();if (node == null) {
node= newParagraph(srcDoc);
dstDoc.getLastSection().getBody().appendChild(node);
}if (node.getNodeType() != 8 & node.getNodeType() != 5) {throw new Exception("Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)");
}
insertDocumentAfterNode(node, dstDoc, srcDoc);
}