Reputation: 103
I'm working on a JAVA web application, I need to merge multiple docx files into one docx file using JAVA.
the method takes a list of File as parameter, the output is a single docx file that contains all data concatenated from Files in input.
I tried this code but it did not work for me:
public File mergeInOneFile(List<File> files) throws IOException, InvalidFormatException, XmlException {
List<CTBody> sourceBody = new ArrayList<>();
for (File file : files) {
OPCPackage srcFile = OPCPackage.open(file);
XWPFDocument srcDocument = new XWPFDocument(srcFile);
CTBody srcBody = srcDocument.getDocument().getBody();
sourceBody.add(srcBody);
}
CTBody source = sourceBody.get(0);
sourceBody.remove(0);
while (sourceBody.size() != 0){
appendBody(source, sourceBody.get(0));
sourceBody.remove(0);
}
return (File) source;
}
private static void appendBody(CTBody src, CTBody append) throws XmlException {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0,srcString.indexOf(">")+1);
String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
String suffix = srcString.substring( srcString.lastIndexOf("<") );
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+suffix);
src.set(makeBody);
}
}
This method did not acheive my purpose. Any other suggestions ?
Upvotes: 0
Views: 3338
Reputation: 103
I have solved the issue of merging a list of docx files in one file, this is my code :
public File merge(final List<File> files) throws IoAppException, OtherAppException {
Path outPath = this.fileStorageService.getPath()
.resolve(UUID.randomUUID().toString() + Directory.DOCX_EXTENSION);
File outFile = outPath.toFile();
try (OutputStream os = new FileOutputStream(outFile);
InputStream is = new FileInputStream(files.get(0));
XWPFDocument doc = new XWPFDocument(is);) {
CTBody ctBody = doc.getDocument().getBody();
for (int i = 1; i < files.size(); i++) {
try (InputStream isI = new FileInputStream(files.get(i)); XWPFDocument docI = new XWPFDocument(isI);) {
CTBody ctBodyI = docI.getDocument().getBody();
appendBody(ctBody, ctBodyI);
} catch (Exception e) {
e.printStackTrace();
throw new OtherAppException("", e);
}
}
doc.write(os);
return outFile;
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new IoAppException("", e);
} catch (IOException e) {
e.printStackTrace();
throw new IoAppException("", e);
}
}
private static void appendBody(CTBody src, CTBody append) throws XmlException {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
String suffix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
CTBody makeBody;
try {
makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + suffix);
} catch (XmlException e) {
e.printStackTrace();
throw new XmlException("", e);
}
src.set(makeBody);
}
Upvotes: 1
Reputation: 457
The error is clearly because of this line: return (File) source. source is a CTBody type and you cast it to File while they are not related.
Upvotes: 1