package com.core.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import java.io.*;
public class DocumentMergeUtil {
public static void main (String[] args) throws Exception {
File newFile = new File("D:/C.docx");
List<File> srcfile = new ArrayList<>();
pdf2png("","");
File file1 = new File("D:/1.docx");
File file2 = new File("D:/2.docx");
srcfile.add(file1);
srcfile.add(file2);
try {
OutputStream dest = new FileOutputStream(newFile);
ArrayList<XWPFDocument> documentList = new ArrayList<>();
XWPFDocument doc = null;
for (int i = 0; i < srcfile.size(); i++) {
FileInputStream in = new FileInputStream(srcfile.get(i).getPath());
OPCPackage open = OPCPackage.open(in);
XWPFDocument document = new XWPFDocument(open);
documentList.add(document);
}
for (int i = 0; i < documentList.size(); i++) {
doc = documentList.get(0);
if(i == 0){//首页直接分页,不再插入首页文档内容
documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
// appendBody(doc,documentList.get(i));
}else if(i == documentList.size()-1){//尾页不再分页,直接插入最后文档内容
appendBody(doc,documentList.get(i));
}else{
documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
appendBody(doc,documentList.get(i));
}
}
doc.write(dest);
System.out.println("*****合成成功********");
//Runtime.getRuntime().exec("cmd /c start winword C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/Paticulars.docx");//直接调用cmd打开合成文档
} catch (Exception e) {
e.printStackTrace();
}
}
public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
CTBody src1Body = src.getDocument().getBody();
CTBody src2Body = append.getDocument().getBody();
List<XWPFPictureData> allPictures = append.getAllPictures();
// 记录图片合并前及合并后的ID
Map<String,String> map = new HashMap<String,String>();
for (XWPFPictureData picture : allPictures) {
String before = append.getRelationId(picture);
//将原文档中的图片加入到目标文档中
String after = src.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG);
map.put(before, after);
}
appendBody(src1Body, src2Body,map);
}
private static void appendBody(CTBody src, CTBody append,Map<String,String> map) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0,srcString.indexOf(">")+1);
String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
String sufix = srcString.substring( srcString.lastIndexOf("<") );
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
if (map != null && !map.isEmpty()) {
//对xml字符串中图片ID进行替换
for (Map.Entry<String, String> set : map.entrySet()) {
addPart = addPart.replace(set.getKey(), set.getValue());
}
}
//将两个文档的xml内容进行拼接
CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);
src.set(makeBody);
}
/**
* Word文件转PDF
* @param wordPath 需要被转换的word全路径带文件名
* @param pdfPath 转换之后pdf的全路径带文件名
public static void doc2pdf(String wordPath, String pdfPath) {
try {
long old = System.currentTimeMillis();
File file = new File(pdfPath); //新建一个pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); //Address是将要被转化的word文档
doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
} catch (Exception e) {
e.printStackTrace();
}
}*/
/**
* 转换PDF为图片
* @Author yang
* @Date 2021/01/04 16:39
**/
public static void pdf2png(String filepath,String filename) throws IOException {
try {
// 将pdf装图片 并且自定义图片得格式大小
File file = new File("D://asx.pdf");
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
//BufferedImage image = renderer.renderImageWithDPI(0, 72); // Windows native DPI
BufferedImage image = renderer.renderImage(0, 1);
ImageIO.write(image, "png", new File("D://abc.png"));
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
记录合成word,pdf转图片两种 亲测可用
最新推荐文章于 2023-08-03 16:29:23 发布