溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java怎么實現(xiàn)合并word文檔

發(fā)布時間:2023-05-06 09:32:24 來源:億速云 閱讀:208 作者:iii 欄目:編程語言

今天小編給大家分享一下Java怎么實現(xiàn)合并word文檔的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

說明

在做項目中,遇到了一種情況,需要將一個小word文檔的內容插入到一個大word(主文檔)中。

實現(xiàn)

1.首先定義好主文檔

在主文檔需要插入小word文檔的位置上添加一個書簽,這個書簽名字要記住,后面要用。

Java怎么實現(xiàn)合并word文檔

2.定義需要追加的文檔

Java怎么實現(xiàn)合并word文檔

3. 代碼實現(xiàn)
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");
			//第四個參數(shù)是書簽名,需要和步驟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怎么實現(xiàn)合并word文檔

以上就是“Java怎么實現(xiàn)合并word文檔”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI