java拼接word文档

#java拼接word文档
说到java操作word 文档,其实网上都已经讲的很多了。我就不再一一赘述了,这里我只记录下使用 docx4j 拼接word文档,使用jacob 调用word 进程生成或者更新docx目录的基本操作。

  1. 引入依赖
<dependency>
    <groupId>org.docx4j</groupId>
	  <artifactId>docx4j-JAXB-Internal</artifactId>
	  <version>8.0.0</version>
</dependency>
<!-- 排除依赖的操作根据自己具体环境选择 -->
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-export-fo</artifactId>
    <version>8.0.0</version>
    <exclusions>
    	<exclusion>
    		<groupId>org.slf4j</groupId>
    		<artifactId>slf4j-log4j12</artifactId>
    	</exclusion>
    </exclusions>
</dependency>
 <!-- https://mvnrepository.com/artifact/net.sf.jacob-project/jacob -->
<dependency>
    <groupId>net.sf.jacob-project</groupId>
    <artifactId>jacob</artifactId>
    <version>1.19</version>
</dependency>

  1. 利用docx4j实现word文档拼接

直接上工具类

/**
 * Copyright (C) 2019 

 * Modification  History
 * Date         Author        Version        Description
 * ------------------------------------------------------
 *
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.org.apache.poi.util.IOUtils;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk;

/**
 * @ClassName: DocxMergeUtil
 * @Description: TODO
 * @author: bohu
 * @date: 2019年8月13日下午4:17:24
 */
public class DocxMergeUtil {
	/**
	 * * DOCX文件合并 * @param list 合并文件全路径(先放入的文件合并到最上方) * @param path 合并后文件输出路径
	 * * @throws Exception
	 */
	public static void merge(List<String> list, String path) throws Exception {
		List<InputStream> inList = new ArrayList<InputStream>();
		for (int i = 0; i < list.size(); i++) {
			inList.add(new FileInputStream(list.get(i)));
		}

		InputStream inputStream = mergeDocx(inList);
		saveTemplate(inputStream, path);
	}

	public static InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException, IOException {

		WordprocessingMLPackage target = null;
		// 创建临时Docx文件
		final File generated = File.createTempFile("generated", ".docx");
		// Inspur1!?
		int chunkId = 0;
		Iterator<InputStream> it = streams.iterator();
		try {
			while (it.hasNext()) {
				InputStream is = it.next();
				if (is != null) {
					if (target == null) {
						// 流读写 第一个文档
						OutputStream os = new FileOutputStream(generated);
						os.write(IOUtils.toByteArray(is));
						os.close();

						// 获取第一个文档
						target = WordprocessingMLPackage.load(generated);
					} else {
						// 插入其他文档
						insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(is), chunkId++);
					}
				}
			}
		} finally {
			streams.forEach((t) -> {
				try {
					if (t != null) {
						t.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			});
		}
		if (target != null) {
			target.save(generated);
			return new FileInputStream(generated);

		} else {
			return null;
		}

	}

	/**
	 * * 插入文档 * @param main * @param bytes * @param chunkId
	 */
	public static void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
		try {
			AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(
					new PartName("/part" + chunkId + ".docx"));
			// afiPart.setContentType(new ContentType(CONTENT_TYPE));
			afiPart.setBinaryData(bytes);
			Relationship altChunkRel = main.addTargetPart(afiPart);

			CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
			chunk.setId(altChunkRel.getId());

			main.addObject(chunk);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * * 输出文件 * @param fis * @param toDocPath
	 */
	public static void saveTemplate(InputStream fis, String toDocPath) {
		FileOutputStream fos;
		// int bytesum = 0;
		int byteread = 0;
		try {
			fos = new FileOutputStream(toDocPath);
			byte[] buffer = new byte[2048];
			while ((byteread = fis.read(buffer)) != -1) {
				// bytesum += byteread; // 字节数 文件大小
				fos.write(buffer, 0, byteread);
			}
			fos.flush();
			fos.close();
			fis.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * docx文档转换为PDF
	 * 
	 * @param docx    docx文档
	 * @param pdfPath PDF文档存储路径
	 * @throws Exception 可能为Docx4JException, FileNotFoundException, IOException等
	 */
	public static void convertDocxToPDF(File docx, String pdfPath) throws Exception {
		OutputStream os = null;
		try {
			WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(docx);
//            Mapper fontMapper = new BestMatchingMapper();   
			Mapper fontMapper = new IdentityPlusMapper();
			// 中文字体转换
			fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
			fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
			fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
			fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
			fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
			fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
			fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
			fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
			fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
			fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
			fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
			fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
			fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
			fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
			fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
			mlPackage.setFontMapper(fontMapper);

			os = new FileOutputStream(pdfPath);
			FOSettings foSettings = Docx4J.createFOSettings();
			foSettings.setWmlPackage(mlPackage);
			Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
		} finally {
			IOUtils.closeQuietly(os);
		}
	}

	public static void main(String[] args) throws Exception {
		DocxMergeUtil wordUtil = new DocxMergeUtil();
		String template = "E:\\parts";
		List<String> list = new ArrayList<String>();
		list.add(template + "\\header.docx");
		list.add(template + "\\linux-1.docx");
		list.add(template + "\\集群.docx");
		list.add(template + "\\文档2.docx");
		list.add(template + "\\文档2-list.docx");
		list.add(template + "\\文档2-table.docx");
		list.add(template + "\\NF8480M5-S.docx");
		list.add(template + "\\CC-AS5500G2.docx");
		list.add(template + "\\JHJ-FS6500.docx");
		list.add(template + "\\fooder.docx");
		// list.add(template + "\\application-1.docx");
		wordUtil.merge(list, template + "\\out.docx");
	}

}

  1. 利用jacob实现word文档目录更新
import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JacobUtils {
	//目录更新
	public static void updateCatalog(String src) {
			log.info("src:{},tag{},titleName{}", src);
			ActiveXComponent app = null;
			Dispatch doc = null;
			try {
				ComThread.InitMTA(true);
				/** 启动word进程 */
				log.debug("==>>启动word 进程  !");
				app = new ActiveXComponent("Word.Application");
				app.setProperty("Visible", new Variant(false));
				Dispatch docs = app.getProperty("Documents").toDispatch();
				log.debug("==>>打开word 文档!");
				/** 打开word文档 */
				doc = Dispatch.call(docs, "Open", src).toDispatch();
				Dispatch activeDocument = app.getProperty("ActiveDocument").toDispatch();
				Dispatch tablesOfContents = Dispatch.get(activeDocument, "TablesOfContents").toDispatch();
				Variant tablesOfContent = Dispatch.call(tablesOfContents, "Item", new Variant(1));
				Dispatch toc = tablesOfContent.toDispatch();
				log.debug("==>>开始更新文档目录!");
				Dispatch.call(toc, "Update");
				//new Variant(12) 表示保存为docx文档,详细内容建议读官方api文档
				Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { src, new Variant(12) }, new int[1]);
			} catch (Exception e) {
				e.printStackTrace();
				log.error("word 更新目录容出错" + e.getMessage());
			} finally {
				if (doc != null) {
					Dispatch.call(doc, "Close", new Variant(false));
				}
				if (app != null) {
					// Dispatch.call(doc, "Close", new Variant(false));
					app.invoke("Quit", new Variant[] {});
				}
				ComThread.Release();
	
			}
	
		}
	}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值