ponnl Member since 2018 2 posts
Posted: May 13, 2020
Last activity: June 17, 2020

Merge multiple Word (docx) documents in to one single file

To merge multiple word documents into single file using docx4j use the following snippet of code:

This code will work from 8.X and above. Need to verify if this works with 7.X as well.

Explanation and how to run this java code is available in the attached docx file.

ParameterPage parameterPage = tools.getParameterPage();
List<String> listOfFiles= (java.util.ArrayList)parameterPage.getObject("ListofFiles");
char sep = PRFile.separatorChar;
com.plutext.merge.DocumentBuilderIncremental dbi = new com.plutext.merge.DocumentBuilderIncremental();
for(int i=0;i<listOfFiles.size();i++){
  String exportPath= tools.getProperty("pxProcess.pxServiceExportPath").getStringValue();
  String filenamepath = exportPath+ sep + listOfFiles.get(i);
  PRFile prFile = new PRFile(filenamepath);
  PRInputStream is = new;
  com.plutext.merge.BlockRange block = new com.plutext.merge.BlockRange(org.docx4j.Docx4J.load(is));
  dbi.addBlockRange(block, i==(listOfFiles.size()-1) );
  org.docx4j.openpackaging.packages.WordprocessingMLPackage output = dbi.finish();
  String outFilenamepath = tools.getProperty("pxProcess.pxServiceExportPath").getStringValue()+ sep +    tools.getParamValue("outputFileName");
  PRFile outPrFile = new PRFile(outFilenamepath);
  PROutputStream os = new PROutputStream(outPrFile);;
      tools.sendFile(outPrFile, false, null, true);
}catch(Exception e){



***Edited by Moderator Marissa to update Content Type from Discussion to Question; change Pega Academy to Product***

Pega Platform 8.4 Case Management
Share this page LinkedIn