package cn.com.ikdo.fda.oa.utils;
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.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;
/**
* Docx文件合并
* @author lcg
*
*/
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");
int chunkId = 0;
Iterator<InputStream> it = streams.iterator();
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++);
}
}
}
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[1444];
while ((byteread = fis.read(buffer)) != -1) {
// bytesum += byteread; // 字节数 文件大小
fos.write(buffer, 0, byteread);
}
fis.close();
fos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// public static void main(String[] args) throws Docx4JException, IOException{
// DocxMergeUtil wordUtil=new DocxMergeUtil ();
// String template="F:/docs";
// List<String> list=new ArrayList<String>();
// list.add(template+"/1.docx");
// list.add(template+"/2.docx");
// list.add(template+"/3.docx");
// list.add(template+"/4.docx");
// wordUtil.merge(list, template+"/out.docx");
// }
}