blob: 33ad12e44882d5d04aab2f5c8b7d903e69c0de25 [file] [log] [blame]
<?xml version="1.0"?>
<!--
$RCSfile: Writer.AutomaticallyCreateAParagraphStyleNamedCode.snip,v $
last change: $Revision: 1.1 $ $Author: tomsontom $ $Date: 2008/10/20 11:29:27 $
(c)2003 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>create</keyword>
<keyword>paragraph</keyword>
<keyword>style</keyword>
<keyword>code</keyword>
</keywords>
<authors>
<author id="1" initial="true" email="" copyright="Josef Frysak">Josef Frysak</author>
</authors>
<question heading="Automatically create a paragraph style named code">How to automatically create a paragraph style with name &quot;code&quot;?
</question>
<answer>
<p>First get a list of all paragraph styles of the current document.</p>
<p>(&quot;XStyleFamiliesSupplier&quot;). Then search it for the name &quot;code&quot;.</p>
<p>If no paragraph style named &quot;code&quot; exists, create a new one using the</p>
<p>documents &quot;XMultiServiceFactory&quot; interface. Next configurate the newly</p>
<p>created paragrah style object by changing its properties. Finally</p>
<p>add this object to the list of paragraph styles.</p>
<p>For further details see http://wi.wu-wien.ac.at/rgf/diplomarbeiten/BakkStuff/2008/200809_Frysak/200809_Frysak_Automating_OOo_ooRexx_Nutshells.pdf.</p>
<listing>-- try to get a script context, will be .nil, if script was not invoked by OOo
x_ScriptContext = uno.getScriptContext()
if (x_ScriptContext &lt;&gt; .nil) then
do
-- invoked by OOo as a macro
-- get context
x_ComponentContext = x_ScriptContext~getComponentContext
-- get desktop (an XDesktop)
x_Desktop = x_ScriptContext~getDesktop
-- get current document
x_Document = x_ScriptContext~getDocument
end
else
do
-- called from outside of OOo, create a connection
-- connect to Open Office and get component context
x_ComponentContext = UNO.connect()
-- create a desktop service and its interface
service = &quot;{%see com.sun.star.frame.Desktop}&quot;
s_Desktop = x_ComponentContext~getServiceManager~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}~createInstance(service)
x_Desktop = s_Desktop~{%see com.sun.star.frame.XDesktop%XDesktop}
-- get the last active document
x_Document = x_Desktop~getCurrentComponent()
end
-- first we search all paragraph styles if there is allready one
-- with name &quot;code&quot;
x_StyleFamiliesSupplier = x_Document~{%see com.sun.star.style.XStyleFamiliesSupplier%XStyleFamiliesSupplier}
x_StyleFamilies = x_StyleFamiliesSupplier~getStyleFamilies()
s_StyleFamily = x_StyleFamilies~getByName(&quot;ParagraphStyles&quot;)
x_NameAccess = s_StyleFamily~{%see com.sun.star.container.XNameAccess%XNameAccess}
if x_NameAccess~hasByName(&quot;code&quot;) then
do
-- if &quot;code&quot; allready exists show error message
.bsf.dialog~messageBox(&apos;PageStyle &quot;code&quot; allready exists!&apos;, &quot;ERROR&quot;, &quot;error&quot;)
end
else
do
-- if &quot;code&quot; does not exist:
-- create a factory to create a new paragraph style object
x_MultiServiceFactory = x_Document~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}
s_ParagraphStyle = x_MultiServiceFactory~createInstance(&quot;{%see com.sun.star.style.ParagraphStyle}&quot;)
-- now set its properties
s_ParagraphProperties = s_ParagraphStyle~{%see com.sun.star.beans.XPropertySet%XPropertySet}
-- WARNING! Java.Integer = UNO_LONG !!!!!!!
-- its background color
paracolor = .bsf~new(&quot;java.lang.Integer&quot;, X2D(&quot;FFFFCC&quot;))
s_ParagraphProperties~setPropertyValue(&quot;ParaBackColor&quot;, paracolor)
-- left and right margin
pramargin = .bsf~new(&quot;java.lang.Integer&quot;, 500)
s_ParagraphProperties~setPropertyValue(&quot;ParaLeftMargin&quot;, pramargin)
s_ParagraphProperties~setPropertyValue(&quot;ParaRightMargin&quot;, pramargin)
-- left, right, top and bottom border
o_Border = .bsf~new(&quot;{%see com.sun.star.table.BorderLine}&quot;)
o_Border~bsf.setFieldValue(&quot;Color&quot;, 0)
o_Border~bsf.setFieldValue(&quot;InnerLineWidth&quot;, 0)
o_Border~bsf.setFieldValue(&quot;OuterLineWidth&quot;, 1)
o_Border~bsf.setFieldValue(&quot;LineDistance&quot;, 0)
s_ParagraphProperties~setPropertyValue(&quot;LeftBorder&quot;, o_Border)
s_ParagraphProperties~setPropertyValue(&quot;RightBorder&quot;, o_Border)
s_ParagraphProperties~setPropertyValue(&quot;TopBorder&quot;, o_Border)
s_ParagraphProperties~setPropertyValue(&quot;BottomBorder&quot;, o_Border)
borderdist = .bsf~new(&quot;java.lang.Integer&quot;, 50)
s_ParagraphProperties~setPropertyValue(&quot;LeftBorderDistance&quot;, borderdist)
s_ParagraphProperties~setPropertyValue(&quot;RightBorderDistance&quot;, borderdist)
s_ParagraphProperties~setPropertyValue(&quot;TopBorderDistance&quot;, borderdist)
s_ParagraphProperties~setPropertyValue(&quot;BottomBorderDistance&quot;, borderdist)
-- and the fontname
s_ParagraphProperties~setPropertyValue(&quot;CharFontName&quot;, &quot;Arial&quot;)
-- finally insert the new paragraph style to the list of
-- paragraph styles of this document
x_NameAccess~insertByName(&quot;code&quot;, s_ParagraphStyle)
end
::requires UNO.CLS</listing>
</answer>
<versions>
<version number="2.4.1" status="tested"/>
</versions>
<operating-systems>
<operating-system name="All"/>
</operating-systems>
<changelog>
<change author-id="1" date="2008-10-14">Initial version</change>
</changelog>
</snippet>