Languages
[Edit]
EN

Java - merge multiple doc / docx documents into one with Apache POI library

10 points
Created by:
Romana-Little
248

In this short article we would like to show how in Java merge multiple Microsoft Word (doc / docx) documents into 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;
	}
}

See also

  1. https://mvnrepository.com/artifact/org.apache.poi/poi 
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join