hi,
We are using AsposeWords Java with the latest jar. I have word document, for which I am trying to insert Table Of Contents. then I am getting the ClassCastException.
the Entire stack trace is as follows.
Exception in thread “main” java.lang.ClassCastException: com.aspose.words.asz
at com.aspose.words.ath.zd(SpanText.java:66)
at com.aspose.words.yb.o(LineReflower.java:708)
at com.aspose.words.yb.Wl(LineReflower.java:248)
at com.aspose.words.yb.Wj(LineReflower.java:178)
at com.aspose.words.yb.d(LineReflower.java:43)
at com.aspose.words.xv.e(LineBandReflower.java:32)
at com.aspose.words.xw.f(LineBlockReflower.java:110)
at com.aspose.words.xw.a(LineBlockReflower.java:35)
at com.aspose.words.df.a(ContentPartReflower.java:28)
at com.aspose.words.asd.arm(ShellReflower.java:321)
at com.aspose.words.asd.a(ShellReflower.java:65)
at com.aspose.words.ce.Bf(ColumnReflower.java:194)
at com.aspose.words.ce.c(ColumnReflower.java:39)
at com.aspose.words.ajj.p(PageReflower.java:109)
at com.aspose.words.ajj.h(PageReflower.java:52)
at com.aspose.words.ajy.w(PartReflower.java:31)
at com.aspose.words.atn.fe(StoryLayout.java:90)
at com.aspose.words.ff.aD(DocumentLayoutBuilder.java:169)
at com.aspose.words.fe.Fx(DocumentLayout.java:174)
at com.aspose.words.wp.a(LayoutDocument.java:52)
at com.aspose.words.Document.updatePageLayout(Document.java:1467)
at com.aspose.words.ld.perform(ExternalActionUpdateLayout.java:25)
at com.aspose.words.pf.execute(FieldUpdateExecutionItem.java:79)
at com.aspose.words.pi.ir(FieldUpdater.java:97)
at com.aspose.words.pi.updateFields(FieldUpdater.java:60)
at com.aspose.words.Range.updateFields(Range.java:172)
at com.aspose.words.Document.updateFields(Document.java:1053)
at myTests.TesInsertToc.main(TesInsertToc.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120).
Code that I am using is,
Document sectionDocument = AsposeUtil.getDocument(“D:\AsposeTests\Test.doc”);
//create a builder
DocumentBuilder builder = new DocumentBuilder(sectionDocument);
// delete the content of the bookmark before inserting.
Bookmark bm = sectionDocument.getRange().getBookmarks().get(“TOC”);
if (bm != null) {
bm.setText("");
}
// move to new page before inserting TOC, thus starting TOC from a new page
// Move builder cursor to bookmark. cursor will be placed right before bookmark.
boolean moveResult = builder.moveToBookmark(“TOC”, true, false);
if (moveResult) {
// Insert PageBreak.
builder.insertBreak(BreakType.PAGE_BREAK);
}else{
//if bookmark not found move to begining of document
builder.moveToDocumentStart();
}
//add heading here
builder.getFont().setBold(true);
builder.writeln(“Table of Contents”);
//reset the font
builder.setBold(false);
//now add the TOC
Field toc = builder.insertTableOfContents("\o “1-3” \h \z \u");
//try to update all the fields
sectionDocument.updateFields();
Word document is attached for your reference.
Please let me know where am I going wrong.