Java实现合并word文档的示例代码

 更新时间:2022年08月10日 14:49:30   作者:江西-吴彦祖  
在做项目中,经常会遇到一种情况,需要将一个小word文档的内容插入到一个大word(主文档)中。本文就为大家准备了Java实现合并word文档的方法,需要的可以参考一下

说明

在做项目中,遇到了一种情况,需要将一个小word文档的内容插入到一个大word(主文档)中。

实现

1.首先定义好主文档

在主文档需要插入小word文档的位置上添加一个书签,这个书签名字要记住,后面要用。

2.定义需要追加的文档

3. 代码实现

package com.test.word;

import com.aspose.words.Body;
import com.aspose.words.Bookmark;
import com.aspose.words.BookmarkCollection;
import com.aspose.words.CompositeNode;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.Node;
import com.aspose.words.NodeImporter;
import com.aspose.words.Orientation;
import com.aspose.words.PaperSize;
import com.aspose.words.Section;

public class Test1 
{
	public static void main(String[] args) 
	{
		try
		{
			//主文档
			Document mainDocument = new Document("F:\\test\\main.docx");
			//需要进行追加的文档
			Document addDocument = new Document("F:\\test\\add.docx");
			//第四个参数是书签名,需要和步骤1在大word文档中定义的书签名对上
			appendDocument(mainDocument, addDocument, true, "shuqian1");
			System.out.println("成功!");
			//将最终合并完成后的文档对象保存到文件中
			mainDocument.save("F:\\test\\result.docx");
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description 文档拼接
	 * @param mainDoc 主文档
	 * @param addDoc 要拼接的文档
	 * @param isPortrait 是否横向拼接
	 * @param bookmark 书签名称,将add文档拼接到主文档哪个位置
	 */
	public static void appendDocument(Document mainDoc, Document addDoc, boolean isPortrait, String bookmark)
	{
		DocumentBuilder builder = null;
		try
		{
			builder = new DocumentBuilder(mainDoc);
			BookmarkCollection bms = mainDoc.getRange().getBookmarks();
			Bookmark bm = bms.get(bookmark);
			if (bm != null)
			{
				builder.moveToBookmark(bookmark, true, false);
				builder.writeln();
				builder.getPageSetup().setPaperSize(PaperSize.A4);
				if (isPortrait)
				{
					builder.getPageSetup().setOrientation(Orientation.PORTRAIT);
				}
				else
				{
					builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
				}
				Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling();
				insertDocumentAfterNode(insertAfterNode, mainDoc, addDoc);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description
	 * @param insertAfterNode 插入的位置
	 * @param mainDoc 主文档
	 * @param srcDoc 要拼接进去的文档
	 * @Return void
	 */
	@SuppressWarnings("rawtypes")
	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();
			Body body = srcDoc.getLastSection().getBody();
			while (null != body.getLastParagraph() && !body.getLastParagraph().hasChildNodes())
			{
				srcDoc.getLastSection().getBody().getLastParagraph().remove();
			}

			NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
			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;
				}
			}
		}
	}
}

4. 成果展示

到此这篇关于Java实现合并word文档的示例代码的文章就介绍到这了,更多相关Java合并word文档内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java安全之CC1利用链详解

    Java安全之CC1利用链详解

    这篇文章主要介绍了Java安全之CC1利用链的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-05-05
  • Java中 shuffle 算法的使用

    Java中 shuffle 算法的使用

    本篇文章,小编将为大家介绍,在Java中 shuffle 算法的使用,有需要的朋友可以参考一下
    2013-04-04
  • Java调用SQL脚本执行常用的方法示例

    Java调用SQL脚本执行常用的方法示例

    这篇文章主要给大家介绍了关于Java调用SQL脚本执行常用的方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • 基于swagger参数与实体中参数不一致的原因分析

    基于swagger参数与实体中参数不一致的原因分析

    这篇文章主要介绍了基于swagger参数与实体中参数不一致的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Mybatis insert方法主键回填和自定义操作

    Mybatis insert方法主键回填和自定义操作

    这篇文章主要介绍了Mybatis insert方法主键回填和自定义操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • SpringBoot启动yaml报错的解决

    SpringBoot启动yaml报错的解决

    这篇文章主要介绍了SpringBoot启动yaml报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 基于Java编写一个数据库比较工具类

    基于Java编写一个数据库比较工具类

    这篇文章主要为大家详细介绍了如何基于Java编写一个数据库比较工具类,其中比较结果会以现数据库的视角说明,感兴趣的小伙伴可以了解一下
    2023-07-07
  • 在springboot中使用AOP进行全局日志记录

    在springboot中使用AOP进行全局日志记录

    这篇文章主要介绍就在springboot中使用AOP进行全局日志记录,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java实现EasyCaptcha图形验证码的具体使用

    Java实现EasyCaptcha图形验证码的具体使用

    Java图形验证码,支持gif、中文、算术等类型,可用于Java Web、JavaSE等项目,下面就跟随小编一起来了解一下
    2021-08-08
  • Spring Boot CORS 配置方法允许跨域请求的最佳实践方案

    Spring Boot CORS 配置方法允许跨域请求的最佳实践方案

    跨域请求在现代Web开发中非常重要,特别是在涉及多个前端和后端服务时,本文详细介绍了跨域请求的背景、重要性以及如何解决跨域问题,通过SpringBoot框架的CORS配置,可以有效地处理跨域请求,确保数据传输的安全性和用户体验,感兴趣的朋友跟随小编一起看看吧
    2024-11-11

最新评论