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:
import java.util.stream.Stream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
public class WordUtils {
public static XWPFDocument mergeDocuments(Stream<XWPFDocument> documents) {
XWPFDocument mergedDocuments = new XWPFDocument();
CTDocument1 mergedCTDocument = mergedDocuments.getDocument();
mergedCTDocument.unsetBody(); // to remove blank first page in merged document
documents.forEach(srcDocument -> {
CTDocument1 srcCTDocument= srcDocument.getDocument();
if (srcCTDocument != null) {
CTBody srcCTBody = srcCTDocument.getBody();
if (srcCTBody != null) {
CTBody mergedCTBody = mergedCTDocument.addNewBody();
mergedCTBody.set(srcCTBody);
}
}
});
return mergedDocuments;
}
}