how to create and fill a table of contents ? (solved)

LORENZO Vincent vincent.lorenzo at
Mon Feb 11 14:52:09 UTC 2019

thank you. I don't reuse the script, but it gives me the name ("") of the object to instantiate to create Table Of Contents.
I'm not yet fully satisfied of I get, but it is a good start point. Here, I wrote my code for future people who get the same problem.

       private XDocumentIndex tableOfContents = null;
       * Refresh the table of contents
       public void refreshTableOfContents() {
             if (null != this.tableOfContents) {

       * Create the table of contents
       public void writeTableOfContents(final String tocTitle) {

             // TODO improve me
             // we should be able to start of not the table of contents on a new page
             // we should be able to go to the next page at the end of the table of content
             try {

                    XTextCursor mxDocCursor = cursor;
                    XText mxDocText = cursor.getText();
                    XMultiServiceFactory mxDocFactory = odtEditor.getXMultiServiceFactory();

                    // Go to the end of the document
                    // Insert a new paragraph and position the cursor in it
                    mxDocText.insertControlCharacter(mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false);
                    XParagraphCursor xParaCursor = UnoRuntime.queryInterface(XParagraphCursor.class, mxDocCursor);
                    xParaCursor.gotoPreviousParagraph(false);// TODO : it was false before

                                 // Create a ContentIndex and access its XPropertySet interface
                    XPropertySet xIndex = UnoRuntime.queryInterface(XPropertySet.class, mxDocFactory.createInstance(""));

                    // to fill the table of contents according to the existing title
                    xIndex.setPropertyValue("CreateFromOutline", true);

                    xIndex.setPropertyValue("Title", tocTitle);

                    // Access the XTextContent interfaces of both the Index and the
                    // IndexMark
                    XTextContent xIndexContent = UnoRuntime.queryInterface(XTextContent.class, xIndex);
                    // XTextContent xEntryContent = UnoRuntime.queryInterface(XTextContent.class, xEntry);

                    // Insert both in the document
                    // mxDocText.insertTextContent(mxDocCursor, xEntryContent, false);
                    mxDocText.insertTextContent(mxDocCursor, xIndexContent, false);

                    // Get the XDocumentIndex interface of the Index
                    XDocumentIndex xDocIndex = UnoRuntime.queryInterface(XDocumentIndex.class, xIndex);

                    // And call its update method

                    tableOfContents = xDocIndex;

                    // xParaCursor.gotoNextParagraph(true);

                    // XText xText = mxDocCursor.getText();
                    // xText.insertControlCharacter(mxDocCursor, (short)BreakType.PAGE_AFTER_value, false);
                    // mxDocCursor.gotoEnd(false);
                    // WriteUtil.addControlCharacter(xTextCursor, controlCharacter);addControlCharacter

                    XPropertySet cursorProperty = UnoRuntime.queryInterface(XPropertySet.class, xParaCursor);
                    try {
                           // TODO
                           cursorProperty.setPropertyValue("BreakType", BreakType.PAGE_BEFORE); //$NON-NLS-1$ //NONE AND BOTH gave me the same result... -> it is not good
                           // BEFORE Allow that the next content (an image) will be on the next page... it seems me stupid...
                    } catch (Exception excp) {
                           System.err.println("Failed to create page break. Exception: " + excp); //$NON-NLS-1$
             } catch (Exception e) {
                    // TODO Auto-generated catch block

Vincent Lorenzo

De : LibreOffice [mailto:libreoffice-bounces at] De la part de Andrew Douglas Pitonyak
Envoyé : samedi 9 février 2019 14:25
À : libreoffice at
Objet : Re: how to create and fill a table of contents ?

Forgot to add the link:

On 2/8/19 8:42 AM, LORENZO Vincent wrote:
                I'm looking for a code example to create a table of contents and fill it programmatically. I thought find something like an object called XTableOfContents  in the unoil library, but nothing and Google didn't really help me.
So, please if you some pointers, code examples, or advices they will be welcome.

Best Regards,
CEA Saclay Nano-INNOV
Point Courrier n° 174
91 191 Gif sur Yvette CEDEX


LibreOffice mailing list

LibreOffice at<mailto:LibreOffice at>


Andrew Pitonyak

My Macro Document:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the LibreOffice mailing list