Hi Tilal:
I am attaching two Word .docm files. Both are able to be opened by Aspose.Words 11.1.0 but only one can be opened by Aspose 17.4.0. I have many more examples of each if you need them.
-
Fetch-n-Fill_works_Aspose.docm - works in both Aspose versions
-
Fetch-and-Fill_v12_updated.docm can only be opened by the older version
In our code, we try to open the Word doc with the newer version of Aspose. If it throws an Exception, we call out to en executable JAR that encapsulates the older version of Aspose.
void convertDocumentToHtml(EhddCommand ehddCommand, String pathToDocument, String documentOutputPath) {
setAsposeLicense()
Document document
try {
document = createDocumentFromPath(ehddCommand, pathToDocument)
} catch(Exception e) { //If the initial load of the Word doc fails, use the failover JAR file
triggerFailoverProcess(pathToDocument)
document = createDocumentFromPath(ehddCommand, pathToDocument)
}
document.save("${documentOutputPath}.html", SaveFormat.HTML);
}
private Document createDocumentFromPath(EhddCommand ehddCommand, String pathToDocument) {
Document document = new Document(pathToDocument)
document.acceptAllRevisions()
document.removeMacros()
return document
}
private void triggerFailoverProcess(String pathToDocument) {
GString shellCommand = assembleCommandObject(pathToDocument)
Process process = shellCommand.execute()
StringBuffer output = new StringBuffer()
StringBuffer error = new StringBuffer()
process.waitForProcessOutput(output, error)
if( ! output.toString().startsWith("success")) {
throw new EhddValidationException(error.toString())
}
}
This older version then opens the Word doc, removes macros and revisions, and writes the file back where the main application can finish the processing.
class EhddConverterService {
void convertWordDoc(String pathToDocument) {
log.info(SplunkLogUtils.toSplunkString([appEvent: "convertWordDoc"]))
setAsposeLicense()
Document document = createDocumentFromPath(pathToDocument)
log.info(SplunkLogUtils.toSplunkString([appEvent: "createDocumentFromPath", documentOutputPath: pathToDocument]))
int saveFormat = parseSaveFormatFromExtension(pathToDocument)
document.save(pathToDocument, saveFormat);
log.info(SplunkLogUtils.toSplunkString([appEvent: "save document"]))
}
private Document createDocumentFromPath(String pathToDocument) {
Document document = new Document(pathToDocument)
document.acceptAllRevisions()
document.removeMacros()
return document
}
private int parseSaveFormatFromExtension(String pathToDocument) {
String extension = pathToDocument.drop(pathToDocument.lastIndexOf('.') + 1)
return getSaveFormat(extension)
}
private int getSaveFormat(String extension) {
switch (extension.toLowerCase()) {
case 'docx': return SaveFormat.DOCX
case 'docm': return SaveFormat.DOCM
case 'doc': return SaveFormat.DOC
case 'dot': return SaveFormat.DOT
case 'dotm': return SaveFormat.DOTM
case 'dotx': return SaveFormat.DOTX
case 'rtf': return SaveFormat.RTF
default: return SaveFormat.DOCX
}
}
private void setAsposeLicense() {
def licenseStream = this.class.classLoader.getResourceAsStream('resources/Aspose.Total.Java.lic')
License license = new License()
license.setLicense(licenseStream)
}
}
sample_docs.zip (1.7 MB)