Java怎么实现合并word文档

发布时间:2023-05-06 09:32:24 作者:iii
来源:亿速云 阅读:188

今天小编给大家分享一下Java怎么实现合并word文档的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

说明

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

实现

1.首先定义好主文档

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

Java怎么实现合并word文档

2.定义需要追加的文档

Java怎么实现合并word文档

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文档”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. Java加密、解密Word文档
  2. C# 如何合并、拆分Word文档

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java word

上一篇:Java多线程异步调用性能怎么调优

下一篇:Java对象如何转换为字节序列

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》