Hi team, wondering what might causing this error on load method. for input docx file attached ?
throwing below error.
java.lang.ClassCastException: class com.aspose.words.OfficeMath cannot be cast to class com.aspose.words.Body (com.aspose.words.OfficeMath and com.aspose.words.Body are in unnamed module of loader 'app')
at com.aspose.words.zzWtE.zzXhM(Unknown Source)
at com.aspose.words.zzX1q.zzW07(Unknown Source)
at com.aspose.words.zzZ35.zzYQ4(Unknown Source)
at com.aspose.words.zztN.zzYNd(Unknown Source)
at com.aspose.words.zzZ1R.zzYiv(Unknown Source)
at com.aspose.words.zzWym.zzYIm(Unknown Source)
at com.aspose.words.zzYek.zzKA(Unknown Source)
at com.aspose.words.zzWBo.zzZDc(Unknown Source)
at com.aspose.words.zzXgI.zzW07(Unknown Source)
at com.aspose.words.zzXgI.zzYQ4(Unknown Source)
at com.aspose.words.zzVPq.zzYiv(Unknown Source)
at com.aspose.words.zzWym.zzYIm(Unknown Source)
at com.aspose.words.zzYek.zzKA(Unknown Source)
at com.aspose.words.zzXhQ.zzWyk(Unknown Source)
code used to replicate.
import com.aspose.words.Document;
import java.io.FileInputStream;
public class LoadDocument {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("load-fail.docx")) {
Document doc = new Document(fis);
System.out.println("Document loaded successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
load-fail.docx (27.4 KB)
@ELSSAM_elsevier_com
Summary
You are hitting a known bug in certain versions of Aspose.Words for Java that throws a ClassCastException when the document contains an OfficeMath element. The library tries to treat the OfficeMath node as a Body node while loading the DOCX, which leads to the exception you see.
Why it happens
- The bug is tracked as Issue #4736 in the Aspose.Words issue tracker. It occurs when the parser encounters an OfficeMath object that is placed in a location the loader does not expect (e.g., directly under the document body).
- The problem is present in older releases of the SDK; it was fixed in later versions.
What to do next
- Upgrade the Aspose.Words for Java library to the latest available version. The fix for Issue #4736 is included in the releases after the version you are currently using.
- If upgrading is not immediately possible, you can work around the problem by:
- Loading the document with
LoadOptions that disables the processing of OfficeMath objects, e.g.:
LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(LoadFormat.DOCX);
// No direct flag to ignore OfficeMath, but you can load then remove them after a successful load.
Document doc = new Document("load-fail.docx", loadOptions);
// If the load succeeds, you can iterate and remove OfficeMath nodes if they are not needed.
- Converting the DOCX to a simpler format (like plain text) using a custom converter before loading it with Aspose.Words, as suggested in the forum discussion.
- Verify the SDK version you are using by printing
com.aspose.words.Version.getVersion() and include that information when contacting Aspose support if the issue persists.
- If the problem remains after upgrading, open a support ticket and attach the problematic DOCX file along with the stack trace and the SDK version.
Note: The evidence from the Aspose forum confirms that this is a known issue (Issue #4736) and that upgrading the library or applying a custom conversion workaround are the recommended actions.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
@ELSSAM_elsevier_com
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28848
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.