blob: 373d74ce64474dd6bd7be7e9b52e53680a8b4be6 [file] [log] [blame]
<?xml version="1.0"?>
<!--
$RCSfile: Writer.TableOfContent.snip,v $
last change: $Revision: 1.1 $ $Author: tomsontom $ $Date: 2008/10/20 11:29:28 $
(c)2008 by the copyright holders listed with the author-tags.
If no explicit copyright holder is mentioned with a certain author,
the author him-/herself is the copyright holder. All rights reserved.
Public Documentation License Notice:
The contents of this Documentation are subject to the
Public Documentation License Version 1.0 (the "License");
you may only use this Documentation if you comply with
the terms of this License. A copy of the License is
available at http://www.openoffice.org/licenses/PDL.html
The Original Documentation can be found in the CVS archives
of openoffice.org at the place specified by RCSfile: in this header.
The Initial Writer(s) of the Original Documentation are listed
with the author-tags below.
The Contributor(s) are listed with the author-tags below
without the marker for being an initial author.
All Rights Reserved.
-->
<snippet language="ooRexx" application="Writer">
<keywords>
<keyword>table of content</keyword>
</keywords>
<authors>
<author id="" initial="false" email="">Nicole Scholz</author>
</authors>
<question heading="table of content">How can I create a table of content with hyperlinks?
</question>
<answer>
<p>In this example a table of content is created and its properties are changed.</p>
<p>Also the hyperlinks within the table of content are activated.</p>
<listing>/*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 = &quot;private:factory/swriter&quot;
xWriterComponent = xComponentLoader~loadComponentFromURL(url, &quot;_blank&quot;, 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(&quot;ParaStyleName&quot;,&quot;Heading 1&quot;)
xText~insertString(xTextCursor,&quot;Headline&quot;,.false)
xDMsf = xTextDocument~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}
-- create table of content
contentInd = xDMsf~createInstance(&quot;{%see com.sun.star.text.ContentIndex}&quot;)
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=&quot;CharacterStyleName&quot;
propsToc[i1,2]~value=&quot;&quot;
end
end
end
-- now set order of the items of the table of content
propsToc[1,1]~name=&quot;TokenType&quot;
propsToc[1,1]~value = &quot;TokenHyperlinkStart&quot; -- start the hyperlink
propsToc[2,1]~name=&quot;TokenType&quot;
propsToc[2,1]~value = &quot;TokenEntryText&quot; -- enter the text
propsToc[3,1]~name=&quot;TokenType&quot;
-- end the hyperlink so only the text is marked as hyperlink
propsToc[3,1]~value = &quot;TokenHyperlinkEnd&quot;
propsToc[4,1]~name=&quot;TokenType&quot;
-- set the tab stop right aligned that the page number is on the right side
propsToc[4,1]~value = &quot;TokenTabStop&quot;
propsToc[4,2]~name = &quot;TabStopRightAligned&quot;
propsToc[4,2]~value = box(&quot;boolean&quot;, .true)
propsToc[5,1]~name=&quot;TokenType&quot;
propsToc[5,1]~value = &quot;TokenPageNumber&quot; -- enter the page number
contentProps~setPropertyValue(&quot;CreateFromOutline&quot;,box(&quot;boolean&quot;, .true))
-- set the title of the table of content
contentProps~setPropertyValue(&quot;Title&quot;,&quot;Table of Content&quot;)
contentProps~setPropertyValue(&quot;IsProtected&quot;,box(&quot;boolean&quot;, .false))
-- add the properties to the table of content
LevelFormat = contentProps~getPropertyValue(&quot;LevelFormat&quot;)
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(&quot;Level&quot;,box(&quot;short&quot;, 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</listing>
</answer>
<versions>
<version number="2.0.x" status="tested"/>
</versions>
<operating-systems>
</operating-systems>
<changelog>
</changelog>
</snippet>