Page 1 of 1

remove Row, Tr/Tc/Sdt content controls

PostPosted: Thu Aug 13, 2015 8:21 pm
by fatcore
Hi everyone, it's my first question on the forum.
I learnt to use docx4j by myself, for now I didn't have to ask a lot, but now I'm really stuck and I don't know what to do so I ask it here :)

Ok so my problem is that I have a table already done and full of content controls. Each cell has one content control.
The thing I want to do is : if (content control X has a tag = Y) then {erase the whole row}.

I don't want to use binding, and I'm quite sure it will not help here. I also don't want to use Xpath.

What I was using was :

if (CAliasVal.equals("RULE[\"TauxRestauration\"]")){
Object o2 = ((org.docx4j.wml.SdtPr) pr).getParent();
Object o3 = ((org.docx4j.wml.SdtRun) o2).getParent();
Object oi = XmlUtils.unwrap(o3);
Object o4 = ((org.docx4j.wml.SdtRun) oi).getParent();
Object o5 = XmlUtils.unwrap(o4);
Object o6 = ((org.docx4j.wml.SdtRun) o5).getParent();
Object o7 = XmlUtils.unwrap(o6);

System.err.println(o7.getClass());
}

What I want, is to get the parent of SdtRun so P in my XML, and after what get Tc and finally get Tr to remove the row. But What ever I do, I'm stuck in SdtRun. And If I get its parent I still get SdtRun. What should I do ?

PS : I put ((org.docx4j.wml.SdtRun) o5) every time because I know it's what I get :(
PSS : I tryed to remove my content control then remove all Tc that were empty to eventually remove all Tr that are empty too but. Removing the content control doesn't make the cell empty :(

Please help me, there is nothing on the web. Even just a hint will help, I've tryed everything I know, but sadly i'm no expert.

Re: remove Row, Tr/Tc/Sdt content controls

PostPosted: Fri Aug 14, 2015 8:55 am
by jason
If you've manually added stuff, parent won't be set. Not until you've saved/reloaded the docx, that is.

And even then, JAXB (Sun/Oracle's implementation) might get it wrong.

Your solution is to use TraversalUtil.

Its implementation of walkJAXBElements sets parent; for SdtBlock, it sets it to ((SdtBlock)parent).getSdtContent()

So after TraversalUtil has traversed your table, you can use getParent.

Alternatively (and better), as you traverse the table, why don't you get a reference to the table row of interest, so you can delete it later.

Re: remove Row, Tr/Tc/Sdt content controls

PostPosted: Sat Aug 15, 2015 2:07 am
by fatcore
Hi jason,
Thank you for your reply.

Actually I didn't do the word, it is done by someone else with word (the programme). I only get the template to insert info in it. And if a line is empty then I have to delete it.
Sorry, if I wasn't clear.

Actually in traversutil I search for all content controls of the document. And provide a list of these to another class who is in charge of finding the right content control to insert info. If the info is null then I need to remove the whole line. (where the content control is).

Maybe I should get all Tr in a list and for each of them get to all the content controls in it to test them and remove the lines if needed ?

Thanx again for your reply,

Igor