在做项目中,经常会遇到一种情况,需要将一个小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实现合并word文档的示例代码
基础教程推荐
猜你喜欢
- JavaWeb 实现验证码功能(demo) 2024-04-14
- Java+mysql实现学籍管理系统 2023-03-16
- 是否适合从javabean类更新数据库? 2023-11-04
- springboot下使用shiro自定义filter的个人经验分享 2024-02-27
- Java中EnvironmentAware 接口的作用 2023-01-23
- Java编写实现窗体程序显示日历 2023-01-02
- 运用El表达式截取字符串/获取list的长度实例 2023-08-01
- 使用Java和WebSocket实现网页聊天室实例代码 2024-02-25
- JSP 动态树的实现 2023-12-17
- 深入理解约瑟夫环的数学优化方法 2024-03-07
