OO-Snippets: table of content

Commons

Keywordstable of content
LanguageooRexx
ApplicationWriter
AuthorsNicole Scholz
Supported Versions2.0.x  
Supported OS
QuestionHow can I create a table of content with hyperlinks?
Answer

In this example a table of content is created and its properties are changed.

Also the hyperlinks within the table of content are activated.

Code-Snippet-Listing (snippet-source)

/*table of content*/

/* Retrieve the Desktop object, we need its XComponentLoader interface to load a new document*/
oDesktop         = UNO.createDesktop()    -- get the UNO Desktop service object
xComponentLoader = oDesktop~XDesktop~XComponentLoader  -- get componentLoader interface
/* open the blank file */
url = "private:factory/swriter"
xWriterComponent = xComponentLoader~loadComponentFromURL(url, "_blank", 0, .UNO~noProps)

xDocumentFactory = xWriterComponent~XMultiServiceFactory
xTextDocument = xWriterComponent~XTextDocument
xText = xTextDocument~getText()
xTextCursor = xText~createTextCursor()
xPropertySet = xTextCursor~xPropertySet

 -- add some text with Heading 1 style so the text is shown in the TOC
xPropertySet~setPropertyValue("ParaStyleName","Heading 1")
xText~insertString(xTextCursor,"Headline",.false)
xDMsf = xTextDocument~XMultiServiceFactory

 -- create table of content
contentInd = xDMsf~createInstance("com.sun.star.text.ContentIndex")
contentProps = contentInd~XPropertySet

 -- create a array for the TOC properties
m1=5   /* three PropertyValue pairs */
m2=2   /* two PropertyValues        */
 -- create two-dimensional array of type PropertyValue
propsToc = bsf.createArray(.UNO~PropertyValue, m1, m2)
do i1=1 to m1
   do i2=1 to m2
      propsToc[i1,i2]=.uno~propertyValue~new --create an assign PropertyValue object
      if i2=2 then  -- set companion PropertyValue to default value
      do
         propsToc[i1,2]~name="CharacterStyleName"
         propsToc[i1,2]~value=""
      end
   end
end

 -- now set order of the items of the table of content
propsToc[1,1]~name="TokenType"
propsToc[1,1]~value = "TokenHyperlinkStart" -- start the hyperlink
propsToc[2,1]~name="TokenType"
propsToc[2,1]~value = "TokenEntryText" -- enter the text
propsToc[3,1]~name="TokenType"
-- end the hyperlink so only the text is marked as hyperlink
propsToc[3,1]~value = "TokenHyperlinkEnd"
propsToc[4,1]~name="TokenType"
 -- set the tab stop right aligned that the page number is on the right side
propsToc[4,1]~value = "TokenTabStop"
propsToc[4,2]~name = "TabStopRightAligned"
propsToc[4,2]~value = box("boolean", .true)
propsToc[5,1]~name="TokenType"
propsToc[5,1]~value = "TokenPageNumber" -- enter the page number

contentProps~setPropertyValue("CreateFromOutline",box("boolean", .true))
 -- set the title of the table of content
contentProps~setPropertyValue("Title","Table of Content")
contentProps~setPropertyValue("IsProtected",box("boolean", .false))

 -- add the properties to the table of content
LevelFormat = contentProps~getPropertyValue("LevelFormat")
LevelFormat~xIndexAccess~xIndexReplace~replaceByIndex(1, propsToc)

 -- set text content to the table of content and format it
contentTextContent = contentInd~xTextContent()
contentProps~setPropertyValue("Level",box("short", 2))

 -- insert table of content
xText~insertTextContent(xText, contentTextContent, .false )
 -- update table of content
contentTextContent~XDocumentIndex~update()

::requires UNO.cls

Changelog

DateUserModification

and