基于java处理.docx格式的word合并

如下实例是将 2.docx和3.docx合并,写到empty.docx中,不适用于.doc格式,

public static void main(String[] args) {
File file1 = new File("D:\\empty.docx");
List<File> targetFile1 = new ArrayList<>();
targetFile1.add(new File("D:\\2.docx"));
targetFile1.add(new File("D:\\3.docx"));
appendDocx(file1, targetFile1);
}

/**
* 把多个docx文件合并成一个
*
* @param outfile 输出文件
* @param targetFile 目标文件
*/
public static void appendDocx(File outfile, List<File> targetFile) {
try {
OutputStream dest = new FileOutputStream(outfile);
ArrayList<XWPFDocument> documentList = new ArrayList<>();
XWPFDocument doc = null;
for (int i = 0; i < targetFile.size(); i++) {
FileInputStream in = new FileInputStream(targetFile.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) {
/* if (i != documentList.size() - 1) {
documentList.get(i).createParagraph().setPageBreak(true);
}*/

appendBody(doc, documentList.get(i));
}
}
//doc.createParagraph().setPageBreak(true);
doc.write(dest);
} 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<>();
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("<"));
//下面这部分可以去掉,我加上的原因是合并的时候,有时候出现打不开的情况,对照document.xml将某些标签去掉就可以正常打开了
addPart = addPart.replaceAll("w14:paraId=\"[A-Za-z0-9]{1,10}\"", "");
addPart = addPart.replaceAll("w14:textId=\"[A-Za-z0-9]{1,10}\"", "");
addPart = addPart.replaceAll("w:rsidP=\"[A-Za-z0-9]{1,10}\"", "");
addPart = addPart.replaceAll("w:rsidRPr=\"[A-Za-z0-9]{1,10}\"", "");
addPart = addPart.replace("<w:headerReference r:id=\"rId8\" w:type=\"default\"/>","");
addPart = addPart.replace("<w:footerReference r:id=\"rId9\" w:type=\"default\"/>","");
addPart = addPart.replace("xsi:nil=\"true\"","");

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);
}
posted @ 2020-01-08 13:28  ding9587  阅读(1046)  评论(0编辑  收藏  举报