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

Free Support Forum - aspose.com

Removing all content in file

Hi, I need to remove all content in my word document (including images, tables and text).

I had tried using:

InputStream stream = new FileInputStream(“C:\Users\Feng Jun Wendell\Desktop\JavaApplication\Testing.docx”);
Document doc = new Document(stream);
stream.close();
Section section = doc.getSections().get(0);
Body body = section.getBody();
for (Node node = body.getFirstChild(); node != null; node = node.getNextSibling()){
node.remove();
}

But the contents in the document remains intact and nothing was changed.

Thanks in advance to anyone who could help me with my problem.

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your interest in Aspose.Words. Please try using the following code:

Document doc = new Document("C:\\Temp\\in.doc");

doc.removeAllChildren();

doc.save("C:\\Temp\\out.doc");

Best regards,

Hi Tommy,

Just to clarify, the only reason your code wasn't working was because you were removing the node from the Document tree and then making the call to NextSibling, which would return null and cause the iteration to stop right after the first removial. You would need to first store the reference to the NextSibling in a temporary node before removing the original.

Also be aware a document can have many sections and therefore many bodies as well.

Thanks,

Thanks AndreyN and aske012 for your help. It is working fine now. Have a nice day!