Page 1 of 1

Error No ToC content control found after manipulating docx

PostPosted: Tue May 07, 2019 3:24 am
by steveEP
When I update the TOC before manipulating the document it works correctly. This is my code:

Code: Select all
         WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(myTemplate));

         TocGenerator tocGenerator = new TocGenerator(wordMLPackage);
         tocGenerator.updateToc(false);


The problem is I need to call updateToc at the end of method after I insert data into the document. When calling updateToc (the same way I call it originally) after inserting data I get the error:

No ToC content control found

Here is the docx that is created:
tocTest.docx
(199.5 KiB) Downloaded 276 times


Thanks!

Re: Error No ToC content control found after manipulating do

PostPosted: Wed May 08, 2019 10:29 am
by jason
As per the error message, your docx does not contain a ToC content control.

A ToC content control (w:sdt), if present, is a container around your ToC.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
       <w:sdt>
            <w:sdtPr>
                <w:id w:val="-1123310647"/>
                <w:docPartObj>
                    <w:docPartGallery w:val="Table of Contents"/>
                    <w:docPartUnique/>
                </w:docPartObj>
            </w:sdtPr>
            <w:sdtContent>
                <w:p w:rsidP="00F04C35" w:rsidR="00F04C35" w:rsidRDefault="00F04C35">
                    <w:pPr>
                        <w:pStyle w:val="TOCHeading"/>
                    </w:pPr>
                    <w:r>
                        <w:t>Contents</w:t>
                    </w:r>
                </w:p>
                <w:p w:rsidP="00F04C35" w:rsidR="00F04C35" w:rsidRDefault="00F04C35">
                    <w:pPr>
                        <w:pStyle w:val="TOC1"/>
                        <w:tabs>
                            <w:tab w:leader="dot" w:pos="9016" w:val="right"/>
                        </w:tabs>
                        <w:rPr>
                            <w:noProof/>
                        </w:rPr>
                    </w:pPr>
                    <w:r>
                        <w:fldChar w:fldCharType="begin"/>
                    </w:r>
                    <w:r>
                        <w:instrText xml:space="preserve"> TOC \o "1-3" \h \z \u </w:instrText>
                    </w:r>
:
:
            </w:sdtContent>
        </w:sdt>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Could it be that your code is removing this w:sdt (but keeping its contents)?

If you save your docx at various points and manually inspect the contents, you'll be able to pin down where it is getting removed. One place docx4j may be doing that is in OpenDoPE's RemovalHandler. Are you doing OpenDoPE content control data binding?

Re: Error No ToC content control found after manipulating do

PostPosted: Thu May 09, 2019 2:08 am
by steveEP
Hi Jason, thanks for the advice.

No I am not doing OpenDoPe content data binding.

I've located where it is removing the content control by placing the updateToc() calls throughout the code. It seems the content control is getting removed after I call VariablePrepare as such:

Code: Select all
VariablePrepare.prepare(wordMLPackage);


I need to call VariablePrepare to replace my placeholders with data. What are my options?

Thanks again

Re: Error No ToC content control found after manipulating do

PostPosted: Fri May 10, 2019 6:39 am
by jason
The problem is https://github.com/plutext/docx4j/blob/ ... e.java#L77

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
filterSettings.setRemoveContentControls(true);
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


For now the quickest approach would be to copy/rename the class, then remove that line.

Could you please add an issue to our issue tracker on GitHub asking for granular control of these settings / an option to keep the ToC content control?

Re: Error No ToC content control found after manipulating do

PostPosted: Sun May 26, 2019 5:46 pm
by jason