Question

163
Views
ponnl Member since 2018 2 posts
PEGA
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.

 try{
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.pega.pegarules.pub.util.PRInputStream(prFile);
  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);
  output.save(os);
  if(tools.getParamAsBoolean(com.pega.pegarules.pub.dictionary.ImmutablePropertyInfo.TYPE_TRUEFALSE,"downloadFileOnRun")){
      tools.sendFile(outPrFile, false, null, true);
  }
}catch(Exception e){
    oLog.error(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