Forum at OOoForum.orgThe Forum
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Creating a table of content with java

Post new topic   Reply to topic Forum Index -> Macros and API
View previous topic :: View next topic  
Author Message

Joined: 16 Feb 2009
Posts: 2

PostPosted: Mon Feb 16, 2009 1:16 am    Post subject: Creating a table of content with java Reply with quote


I'm new when working with OO and programming, and just got some java code from a colleague that quit. What I want to do is to insert a table of content to my document. At the moment the code does create a title for the table of content, but a get no text. I will insert parts of the code. It would be great if anyone has an opinion of what I should do. And please let me know if you need more code.


     * Sets up a table of content at the beginning of the first page
    private void setTableOfContent(){
        try {
           // New TOC
            Object oContentIndex = mxMultiServiceFactory.createInstance(

            // Set a name for the TOC
            XNamed xNamedIndex = (XNamed) UnoRuntime.queryInterface(
                    XNamed.class, oContentIndex);

            // access the set of properties
            XPropertySet xContentIndexProps = (XPropertySet)
                    UnoRuntime.queryInterface( XPropertySet.class, oContentIndex);

            // "Title" is a "the heading" of the TOC.
            xContentIndexProps.setPropertyValue("Title", "Inneh√•llsf√∂rteckning");
            // "IsProtected" is a property of every BaseIndex
            xContentIndexProps.setPropertyValue("IsProtected", Boolean.TRUE);
            // "CreateFromOutline" is a ContentIndex property
            xContentIndexProps.setPropertyValue("CreateFromOutline", Boolean.FALSE);

            // HOW TO CHANGE THE STYLES?

            //  GETTER METHOD
            //  1. get the name of the style applied to the level we want
            //  2. get that style from the paragraph styles family
            //  3. modify it

            XNameContainer xParaStyleFamily = (XNameContainer)
                    UnoRuntime.queryInterface( XNameContainer.class,
                    mxNameAccess.getByName("ParagraphStyles") );

            // the service BaseIndex has properties to access the styles:
            //      ParaStyleHeading
            //      ParaStyleLevel1, ParaStyleLevel2 ... ParaStyleLevel10

            // change the heading style
            String sParaStyleHeading = AnyConverter.toString(

            if (xParaStyleFamily.hasByName(sParaStyleHeading)) {

                XPropertySet xHeadingStyleProps = (XPropertySet)
                    UnoRuntime.queryInterface( XPropertySet.class,
                    xParaStyleFamily.getByName(sParaStyleHeading) );

                        "ParaAdjust", ParagraphAdjust.CENTER);

                        "CharHeight", new Integer(20));

                        "CharColor", getRGBColor( 0, 0, 125));

                        "CharFontName", "DejaVu Serif;Galatia SIL");

            // change the style for level 1
            String sParaStyleLevel1 = AnyConverter.toString(

            if (xParaStyleFamily.hasByName(sParaStyleLevel1)) {

                XPropertySet xParaStyleLevel1 = (XPropertySet)
                    UnoRuntime.queryInterface( XPropertySet.class,
                    xParaStyleFamily.getByName(sParaStyleLevel1) );

                        "CharPosture", FontSlant.ITALIC);
            // change the style for level 3
            String sParaStyleLevel3 = AnyConverter.toString(

            if (xParaStyleFamily.hasByName(sParaStyleLevel3)) {

                XPropertySet xParaStyleLevel3 = (XPropertySet)
                    UnoRuntime.queryInterface( XPropertySet.class,
                    xParaStyleFamily.getByName(sParaStyleLevel3) );

                        "CharColor", getRGBColor(255, 0, 255));

            // SETTER METHOD
            //  1. create a new custom style
            //  2. set that style at the index level you want

            // create a new style for level 2
            String sMyParaStyleName = "My Custom ContentIndex Style Level 2";

            Object oNewParaStyleLevel2;

            if (xParaStyleFamily.hasByName(sMyParaStyleName)) {
                oNewParaStyleLevel2 = xParaStyleFamily.getByName(sMyParaStyleName);
            } else {
                oNewParaStyleLevel2 = mxMultiServiceFactory.createInstance(
                xParaStyleFamily.insertByName(sMyParaStyleName, oNewParaStyleLevel2);

            XPropertySet xParaStyleLevel2 = (XPropertySet)
                    UnoRuntime.queryInterface( XPropertySet.class,
                    oNewParaStyleLevel2 );

                        "ParaLeftMargin", new Integer(500));
                        "CharWeight", FontWeight.BOLD);

            // SET IT AT THE INDEX LEVEL
            xContentIndexProps.setPropertyValue("ParaStyleLevel2", sMyParaStyleName);


            XDocumentIndex xDocumentIndex = (XDocumentIndex)
                    XDocumentIndex.class, oContentIndex);

            // insert the index at the start of the document
            insertTextContent(mxDocument.getText().getStart(), xDocumentIndex);


        } catch (Exception e) {

private void insertTextContent(XTextRange xTextRange, XTextContent xContent) {
        try {
           mxText.insertControlCharacter(mxTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
           xTextRange.getText().insertTextContent(xTextRange, xContent, false);
        } catch ( ex){

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Forum Index -> Macros and API All times are GMT - 8 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group