We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Extract content from protected document thorws ClassCastException using Java

Hi,
I am extracting the content between the bookmark of the protected document. The extracting content is belongs to editable region. If the content is only paragraph everything is working fine. But if the content contains any picture,chart, etc then giving this error

java.lang.ClassCastException: com.aspose.words.EditableRangeEnd cannot be cast to com.aspose.words.CompositeNode
at Main.extractContent(Main.java:159)

I am uploading my sample code and input file
sample.zip (369.5 KB)

Please check this issue and provide any solution for this.

@Gptrnt

You are saving document’s content to HTML. So, you can remove EditableRangeEnd nodes from the document as shown below to avoid the exception.

Document outputDocument = new Document();
Document document = new Document(MyDir + "input.docx");
document.getChildNodes(NodeType.EDITABLE_RANGE_END, true).clear();
Document pdfDocument = new Document();
DocumentBuilder builder = new DocumentBuilder(pdfDocument);
//  Get Paragraph Collection
NodeCollection<Paragraph> paragraphColl = document.getChildNodes(NodeType.PARAGRAPH, true);
List<Paragraph> headings = getHeadingFromParagraph(paragraphColl);
ArrayList<Node>  extractedNodes = new ArrayList();
List<String> html = new ArrayList<>();
for (int i=0;i<headings.size();i++){
    Paragraph startNode = headings.get(i);
    if(headings.size()> (i + 1)){
        Paragraph endNode = headings.get(i + 1);
        System.out.println(startNode.getText());
        //Extract_contents.extractContent(startNode,endNode,false);
        html.add(convertNodeToHtmlStr(Extract_contents.extractContent(startNode,endNode,false)));
    }else{

    }
}
outputDocument = generateDocument(html);
outputDocument.save(MyDir + "out.docx",SaveFormat.DOCX);