Thanks for your inquiry. We tested this code with “FiguresAAA-test2.docx”, “FiguresAAA-test3.docx”, and “mssp_test.docx”. We have not faced any exception. Please share the input document for which you are facing this exception.
Hi @tahir.manzoor,
Thanks for your quick reply.
I am using the same code for the following document.
It shows the error:com.aspose.words.Table cannot be cast to com.aspose.words.Paragraph
please ,kindly help me to solve the error in the line: builder.moveToParagraph(paragraphs.indexOf((Paragraph)PreviousPara), -1);
Input 1: Bernardo_et_al_RevisedPaper_aspose.zip (2.9 MB)
Thanks and regards,
Priyanga G
We are investigating this issue and will share the modified code according to your requirement. We apologize for your inconvenience.
Thanks for your patience. In your document, the shapes are inside table and it also contains charts. Please use the following code example to get the desired output.
Document doc = new Document(MyDir + "Bernardo_et_al_RevisedPaper_aspose.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
ArrayList tables = new ArrayList();
int bookmark = 1;
int i = 1;
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs)
{
if(paragraph.toString(SaveFormat.TEXT).trim().startsWith("Fig"))
{
Node PreviousPara = paragraph.getPreviousSibling();
while (PreviousPara != null && PreviousPara.toString(SaveFormat.TEXT).trim().length() == 0
&& ((Paragraph)PreviousPara).getChildNodes(NodeType.SHAPE, true).getCount() == 0)
PreviousPara = PreviousPara.getPreviousSibling();
while (PreviousPara != null
&& PreviousPara.getNodeType() == NodeType.PARAGRAPH
&& (((Paragraph)PreviousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0 || ((Paragraph)PreviousPara).getChildNodes(NodeType.GROUP_SHAPE, true).getCount() > 0)
)
{
PreviousPara = PreviousPara.getPreviousSibling();
if(PreviousPara.toString(SaveFormat.TEXT).trim().length() > 0 &&
(PreviousPara.toString(SaveFormat.TEXT).trim().contains("(a)") ||
PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
PreviousPara.toString(SaveFormat.TEXT).trim().contains("(b)") ||
PreviousPara.toString(SaveFormat.TEXT).trim().contains("(d)")
)
)
continue;
else
break;
}
if(PreviousPara == null)
{
builder.moveToDocumentStart();
builder.insertParagraph();
builder.startBookmark("Bookmark" + bookmark);
builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
builder.endBookmark("Bookmark" + bookmark);
bookmark++;
}
else if(PreviousPara.getNodeType() == NodeType.PARAGRAPH)
{
Node node = ((Paragraph)PreviousPara).getParentNode().insertAfter(new Paragraph(doc), PreviousPara);
builder.moveTo(node);
builder.startBookmark("Bookmark" + bookmark);
builder.moveTo(paragraph);
//builder.writeln();
builder.endBookmark("Bookmark" + bookmark);
bookmark++;
}
else if(PreviousPara.getNodeType() == NodeType.TABLE)
{
if(((Table)PreviousPara).getChildNodes(NodeType.SHAPE, true).getCount() > 0)
tables.add(((Table)PreviousPara));
}
}
}
for (Bookmark bm : doc.getRange().getBookmarks())
{
if(bm.getName().startsWith("Bookmark"))
{
ArrayList nodes = ExtractContents.extractContent(bm.getBookmarkStart(), bm.getBookmarkEnd(), true);
Document dstDoc = ExtractContents.generateDocument(doc, nodes);
PageSetup sourcePageSetup = ((Paragraph)bm.getBookmarkStart().getParentNode()).getParentSection().getPageSetup();
dstDoc.getFirstSection().getPageSetup().setPaperSize(sourcePageSetup.getPaperSize());
dstDoc.getFirstSection().getPageSetup().setLeftMargin(sourcePageSetup.getLeftMargin());
dstDoc.getFirstSection().getPageSetup().setRightMargin(sourcePageSetup.getRightMargin());
if(dstDoc.getLastSection().getBody().getLastParagraph().toString(SaveFormat.TEXT).trim().startsWith("Fig"))
dstDoc.getLastSection().getBody().getLastParagraph().remove();
dstDoc.save(MyDir + "out\\output"+i+".docx");
i++;
}
}
for(Table table : (Iterable<Table>)tables)
{
Document dstDoc = new Document();
NodeImporter importer = new NodeImporter(doc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Node newNode = importer.importNode(table, true);
dstDoc.getFirstSection().getBody().appendChild(newNode);
dstDoc.save(MyDir + "out\\output" + i + ".docx");
i++;
}