<?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 = "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</listing> | |
</answer> | |
<versions> | |
<version number="2.0.x" status="tested"/> | |
</versions> | |
<operating-systems> | |
</operating-systems> | |
<changelog> | |
</changelog> | |
</snippet> |