EN
Java - merge multiple doc / docx documents into one with Apache POI library
10 points
In this short article, we would like to show how in Java, merge multiple Microsoft Word (doc / docx) documents into a single one using Apache POI library.
Quick solution:
xxxxxxxxxx
1
import java.util.stream.Stream;
2
import org.apache.poi.xwpf.usermodel.*;
3
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
4
5
public class WordUtils {
6
7
public static XWPFDocument mergeDocuments(Stream<XWPFDocument> documents) {
8
XWPFDocument mergedDocuments = new XWPFDocument();
9
CTDocument1 mergedCTDocument = mergedDocuments.getDocument();
10
mergedCTDocument.unsetBody(); // to remove blank first page in merged document
11
documents.forEach(srcDocument -> {
12
CTDocument1 srcCTDocument= srcDocument.getDocument();
13
if (srcCTDocument != null) {
14
CTBody srcCTBody = srcCTDocument.getBody();
15
if (srcCTBody != null) {
16
CTBody mergedCTBody = mergedCTDocument.addNewBody();
17
mergedCTBody.set(srcCTBody);
18
}
19
}
20
});
21
return mergedDocuments;
22
}
23
}