OO-Snippets: Changing background of document

Commons

Keywordspage properties, page background-color, IllegalArgumentException
LanguageJava
ApplicationWriter
AuthorsTom Schindl (initial)
Supported Versions1.1.3  
Supported OS
Question Changing background of swriter-document using Java-UNO produces IllegalArgumentException
Answer

The long value that the idl of the service states is mapped to an integer in Java. So you are only allowed to set the property to integer values. See the Developer's Guide that comes with the SDK on the mapping of idl types to Java, it's chapter 2.5.3. (Steffen.Grund@sun.com)

Code-Snippet-Listing (snippet-source)

private void createDocument() throws Exception {
    // create the swriter-document and retrieve the doc
    XComponentLoader aLoader = (XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class,m_xMSF_.createInstance( "com.sun.star.frame.Desktop" ));
    XComponent xDocument = UNO.queryComponent(aLoader.loadComponentFromURL( "private:factory/swriter", "_blank", 0, new PropertyValue[ 0 ] ) );
    XTextDocument oDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xDocument);

    // create a supplier to get the Style family collection
    XStyleFamiliesSupplier xSupplier = ( XStyleFamiliesSupplier ) UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, oDoc );

    // get the NameAccess interface from the Style family collection
    XNameAccess xNameAccess = xSupplier.getStyleFamilies();

    XNameContainer xPageStyleCollection = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xNameAccess.getByName( "PageStyles" ));

    // create a PropertySet to set the properties for the new Pagestyle
    XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xPageStyleCollection.getByName("Standard") );

    // setBackgroundColor
    xPropertySet.setPropertyValue("BackColor",new Integer( (int)255 ) );
}

Changelog

DateUserModification
2004-06-08tomsontomModified to match codesnippet.dtd v2.0
2004-02-01tomsontomIntial version

and