/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /************************************************************************* * * The Contents of this file are made available subject to the terms of * the BSD license. * * Copyright 2000, 2010 Oracle and/or its affiliates. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*************************************************************************/
// comment: Step 1: bootstrap UNO and get the remote component context // Step 2: open an empty text document // Step 3: enter some text // Step 4: insert a text table // Step 5: insert colored text // Step 6: insert a text frame
import com.sun.star.uno.UnoRuntime;
publicclass SWriter {
publicstaticvoid main(String args[]) {
//oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo // bootstrap UNO and get the remote component context. The context can // be used to get the service manager
try { // get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); if( xContext != null )
System.out.println("Connected to a running office ...");
} catch( Exception e) {
e.printStackTrace(System.err);
System.exit(1);
}
//oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo // open an empty document. In this case it's a writer document. // For this purpose an instance of com.sun.star.frame.Desktop // is created. It's interface XDesktop provides the XComponentLoader, // which is used to open the document via loadComponentFromURL
//Open Writer document
System.out.println("Opening an empty Writer document");
com.sun.star.text.XTextDocument myDoc = openWriter(xContext);
//oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo // insert some text. // For this purpose get the Text-Object of the document and create the // cursor. Now it is possible to insert a text at the cursor-position // via insertString
//getting the text object
com.sun.star.text.XText xText = myDoc.getText();
//create a cursor object
com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
//inserting some Text
xText.insertString( xTCursor, "The first line in the newly created text document.\n", false );
//inserting a second line
xText.insertString( xTCursor, "Now we're in the second line\n", false );
//oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooo // insert a text table. // For this purpose get MultiServiceFactory of the document, create an // instance of com.sun.star.text.TextTable and initialize it. Now it can // be inserted at the cursor position via insertTextContent. // After that some properties are changed and some data is inserted.
//inserting a text table
System.out.println("Inserting a text table");
//getting MSF of the document
com.sun.star.lang.XMultiServiceFactory xDocMSF =
UnoRuntime.queryInterface(
com.sun.star.lang.XMultiServiceFactory.class, myDoc);
//create instance of a text table
com.sun.star.text.XTextTable xTT = null;
//oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooo // insert a colored text. // Get the propertySet of the cursor, change the CharColor and add a // shadow. Then insert the Text via InsertString at the cursor position.
// get the property set of the cursor
com.sun.star.beans.XPropertySet xTCPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
xTCursor);
// Change the CharColor and add a Shadow try {
xTCPS.setPropertyValue("CharColor",Integer.valueOf(255));
xTCPS.setPropertyValue("CharShadowed", Boolean.TRUE);
} catch (Exception e) {
System.err.println("Couldn't change the color " + e);
e.printStackTrace(System.err);
}
//create a paragraph break try {
xText.insertControlCharacter(xTCursor,
com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
//oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooo // insert a text frame. // create an instance of com.sun.star.text.TextFrame using the MSF of the // document. Change some properties an insert it. // Now get the text-Object of the frame and the corresponding cursor. // Insert some text via insertString.
// get the property set of the text frame
com.sun.star.beans.XPropertySet xTFPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTF);
// Change the AnchorType try {
xTFPS.setPropertyValue("AnchorType",
com.sun.star.text.TextContentAnchorType.AS_CHARACTER);
} catch (Exception e) {
System.err.println("Couldn't change the color " + e);
e.printStackTrace(System.err);
}
//insert the frame
System.out.println("Insert the text frame");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.