Merge Documents

Aspose.Words - Merge Documents

To append documents using Aspose.Words, simply invoke the appendDocument() method of Document class and specify the second document to append at end.

Java

Document doc1 = new Document(dataDir + "doc1.doc");
Document doc2 = new Document(dataDir + "doc2.doc");
doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);

docx4j - Merge Documents

This sample demonstrates how the MergeDocx utility can be used to merge docx documents.

The MergeDocx utility is a paid extension to docx4j. Purchases of this extension support the docx4j project. 

@see https://www.docx4java.org/blog/2010/11/merging-word-documents/ for more info, or @see https://www.plutext.com.

To run the utility, you simply pass it a list of the docx you want to merge; it returns a new pkg containing the merged documents.

Java

final static String[] sourceDocxNames = { "doc1.docx", "doc2.docx"};
static boolean save = true;
static String outputfilepath = dataDir +"OUT_MergeDocx.docx";
/**
 * @param args
 * @throws Docx4JException
 */
public static void main(String[] args) throws Docx4JException {
	// Create list of docx packages to merge
	List<WordprocessingMLPackage> wmlPkgList=new ArrayList<WordprocessingMLPackage>();
	for (int i=0; i<sourceDocxNames.length; i++){
		String filename = dataDir + sourceDocxNames[i] ;
		System.out.println("Loading " + filename);
		wmlPkgList.add(WordprocessingMLPackage
				.load(new java.io.File(filename)));
	}
	try {
		// Use reflection, so docx4j can be built
		// by users who don't have the `MergeDocx` utility
		Class<?> documentBuilder = Class.forName("com.plutext.merge.DocumentBuilder");
		//Method method = documentBuilder.getMethod("merge", wmlPkgList.getClass());
		Method[] methods = documentBuilder.getMethods();
		Method method = null;
		for (int j=0; j<methods.length; j++) {
			System.out.println(methods[j].getName());
			if (methods[j].getName().equals("merge")) {
				method = methods[j];
				break;
			}
		}
		if (method==null) throw new NoSuchMethodException();
		WordprocessingMLPackage resultPkg = (WordprocessingMLPackage)method.invoke(null, wmlPkgList);
		if (save) {
			SaveToZipFile saver = new SaveToZipFile(resultPkg);
			saver.save(outputfilepath);
			System.out.println("Generated " + outputfilepath);
		} else {
			String result = XmlUtils.marshaltoString(resultPkg.getMainDocumentPart().getJaxbElement(), true, true);
			System.out.println(result);
		}
	} catch (SecurityException e) {
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		extensionMissing(e);
	} catch (IllegalArgumentException e) {
		e.printStackTrace();
	} catch (NoSuchMethodException e) {
		extensionMissing(e);
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	} catch (InvocationTargetException e) {
		e.printStackTrace();
	}
}

Download Running Code

Download Sample Code

For more details, visit Appending Documents.