Page 1 of 1

Add breakline in table of contents entries

PostPosted: Fri May 26, 2023 5:11 am
by quimperval
Hello.

I'm setting the property setDirty as true, to force MS Word to update the table of contents and add the page numbers in the entries of the table of contents.

It is working, however because of the indentation and the length of the titles, for some entries the text Is being placed very close to the page number or moving randomly to the next line. Is there a way to force the text in the table of contents to add a breakline when the text is longer than some predefinido length?

For example:

1. Title 1 normal ..........2
2. This title is longer
than the previous.........4

Instead of

1. Title 1 normal ..........2
2. This titles Is longer than
the previous..................4

Thanks for your help or coments.

Re: Add breakline in table of contents entries

PostPosted: Sun May 28, 2023 6:54 pm
by jason
There doesn't seem to be a TOC field setting which could help with this: http://webapp.docx4java.org/OnlineDemo/ ... L/TOC.html

So its a question of whether you can set the relevant TOC style to do what you want. This is a general Word question, not specific to docx4j.

Failing that, perhaps a macro?

Re: Add breakline in table of contents entries

PostPosted: Tue May 30, 2023 12:34 am
by quimperval
I already tried to iterate over each item in the StdBlock object, and for items that are hyperlink I replaced the run that has a long text with as many as runs are necessary, each one with text content that fits some length. And between two text runs I'm adding a run with a break (Br).
However that is not working, Word is ignoring the breakline.

Re: Add breakline in table of contents entries

PostPosted: Tue May 30, 2023 1:33 am
by quimperval
The approach of adding breaklines in the runs works, but not in MS Word. I opened the file in LibreOffice and the breaklines are there (the format is horrible, but they are preserved).

Re: Add breakline in table of contents entries

PostPosted: Tue May 30, 2023 2:12 am
by quimperval
Just for the record, the packages I'm using are:
docx4j-JAXB-ReferenceImpl: 8.2.9
docx4j-ImportXHTML: 8.0.0

The issue could be related to the specification version implemented by docx4j. Because when I Open the file in MS Word and add a breakline in an entry of the tablet of contents MSWord and I want to save, Word informs that some features are not supported by earlier versions of Word.
And then when I reopen the saved file the breakline that I added manually is there.

Re: Add breakline in table of contents entries

PostPosted: Tue May 30, 2023 3:27 pm
by jason
quimperval wrote:I replaced the run that has a long text with as many as runs are necessary, each one with text content that fits some length. And between two text runs I'm adding a run with a break (Br).
However that is not working, Word is ignoring the breakline.


But you are using w:updateFields, to force MS Word to update the table of contents: you'd expect Word to overwrite your contents, since there is no option to update page numbers only.

Re: Add breakline in table of contents entries

PostPosted: Wed May 31, 2023 2:05 am
by quimperval
Hello Jason, thanks for your response, that makes it clear :D.

Re: Add breakline in table of contents entries

PostPosted: Wed Jun 14, 2023 2:26 pm
by quimperval
Finally I created a macro to format the table of contents and make it to run automatically when the user opens the word document. And the macro is added in the file, with docx4j.