Page 1 of 1

Can't Remove Table from document

PostPosted: Mon Nov 20, 2017 6:32 pm
by channab
I am processing a template word document using docx4j and I want to remove a table from the document. I used the following code to get the table and remove the table, but seems like it doesn't remove the table. Can anyone please help me on this? Thanks.

WordprocessingMLPackage tmpl= getTemplate(templatePath);

MainDocumentPart mainDocPart = tmpl.getMainDocumentPart();

List<Object> tblList= getAllElementsForObject(mainDocPart, Tbl.class);
Tbl tmplTbl= (Tbl)tblList.get(0);

mainDocPart.getContent().remove(tmplTbl);

Re: Can't Remove Table from document

PostPosted: Mon Nov 20, 2017 8:08 pm
by jason
What happens?

What does the XML look like before and after? mainDocPart.getXML()

Re: Can't Remove Table from document

PostPosted: Tue Nov 21, 2017 8:49 pm
by channab
Hi All,

I was able to fix this problem by removing parent element of the table.

mainDocPart.getContent().remove(tmplTbl.getParent());