java通过aspose.words将两个word文档合并

参考文章:https://www.cnblogs.com/yin1361866686/p/9561006.html

 

1:提供一个jar包的下载链接

链接:https://pan.baidu.com/s/1P58L_9EdjhcOPWlenI1xHQ 
提取码:q9cx

2:代码如下:

//调用如下
public static void main(String[] args) {
        //sohuyepath:第一个word文件路径
        //xuyepath:第二个word文件路径
		
	Document word=new Document(sohuyepath);
	Document word1=new Document(xuyepath);
	appendDoc(word, word1, true);
				
	word.save(sohuyepath);
}

private static void appendDoc(Document dstDoc, Document srcDoc, boolean includeSection) throws Exception {
       if (includeSection) {
           Iterator<Section> 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 = new Paragraph(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);
       }   



   private static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc)
           throws Exception {
       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;
               }
           }

       }
   }

  
  
       }

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值