In this example a table of content is created and its properties are changed.
Also the hyperlinks within the table of content are activated.
/*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~{%see com.sun.star.frame.XDesktop%XDesktop}~{%see com.sun.star.frame.XComponentLoader%XComponentLoader} -- get componentLoader interface
/* open the blank file */
url = "private:factory/swriter"
xWriterComponent = xComponentLoader~loadComponentFromURL(url, "_blank", 0, .UNO~noProps)
xDocumentFactory = xWriterComponent~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}
xTextDocument = xWriterComponent~{%see com.sun.star.text.XTextDocument%XTextDocument}
xText = xTextDocument~getText()
xTextCursor = xText~createTextCursor()
xPropertySet = xTextCursor~{%see com.sun.star.beans.XPropertySet%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~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}
-- create table of content
contentInd = xDMsf~createInstance("{%see com.sun.star.text.ContentIndex}")
contentProps = contentInd~{%see com.sun.star.beans.XPropertySet%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~{%see com.sun.star.container.XIndexAccess%xIndexAccess}~{%see com.sun.star.container.XIndexReplace%xIndexReplace}~replaceByIndex(1, propsToc)
-- set text content to the table of content and format it
contentTextContent = contentInd~{%see com.sun.star.text.XTextContent%xTextContent}()
contentProps~setPropertyValue("Level",box("short", 2))
-- insert table of content
xText~insertTextContent(xText, contentTextContent, .false )
-- update table of content
contentTextContent~{%see com.sun.star.text.XDocumentIndex%XDocumentIndex}~update()
::requires UNO.cls