Languages
[Edit]
EN

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

10 points
Created by:
RomanaLittle
458

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;
	}
}

 

See also

  1. https://mvnrepository.com/artifact/org.apache.poi/poi 

Alternative titles

  1. Java - join multiple doc / docx documents into one with Apache POI library
  2. Java - combine multiple doc / docx documents into one with Apache POI library
  3. Java - concat multiple doc / docx documents into one with Apache POI library
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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