Java如何实现合并word文档
作者:若安舟已远
时间:2023-04-25
来源:互联网
浏览:0
说明在做项目中,遇到了一种情况,需要将一个小word文档的内容插入到一个大word(主文档)中。实现1.首先定义好主文档在主文档需要插入小word文档的位置上添加一个书签,这个书签名字要记住,后面要用。2.定义需要追加的文档3.代码实现packagecom.test.word;importcom.aspose.words.Body;importcom.aspose.words.Bookmark;importcom.aspose.words.BookmarkCollection;importcom.aspo
说明
在做项目中,遇到了一种情况,需要将一个小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. 成果展示

作者最新文章
Word格式转换成PDF?Word转PDF的具体步骤是什么?
2026-09-02 19:13
长安猎手K50 2026款上市,14.19万元起售
2026-08-25 16:08
价格大跳水,网友惊呼买早了!两大巨头正面交锋,最高直降2500元,几乎所有品类“你降我也降”
2026-08-25 16:02
全新奥迪Q3 L申报信息揭晓,车身尺寸升级,动力配置保持强劲
2026-08-25 15:44
iPhone15ProMax屏幕常亮怎么设置 iPhone15ProMax屏幕常亮设置方法
2026-08-25 15:31
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















