1 /*************************************************************************
3 * $RCSfile: SWriter.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:17:30 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: bootstrap UNO and get the remote component context
43 // Step 2: open an empty text document
44 // Step 3: enter some text
45 // Step 4: insert a text table
46 // Step 5: insert colored text
47 // Step 6: insert a text frame
48 //***************************************************************************
50 import com
.sun
.star
.uno
.UnoRuntime
;
52 public class SWriter
{
54 public static void main(String args
[]) {
57 //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
58 // bootstrap UNO and get the remote component context. The context can
59 // be used to get the service manager
60 //*************************************************************************
61 com
.sun
.star
.uno
.XComponentContext xContext
= null;
64 // get the remote office component context
65 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
66 if( xContext
!= null )
67 System
.out
.println("Connected to a running office ...");
70 e
.printStackTrace(System
.err
);
74 //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
75 // open an empty document. In this case it's a writer document.
76 // For this purpose an instance of com.sun.star.frame.Desktop
77 // is created. It's interface XDesktop provides the XComponentLoader,
78 // which is used to open the document via loadComponentFromURL
79 //*************************************************************************
81 //Open Writer document
83 System
.out
.println("Opening an empty Writer document");
84 com
.sun
.star
.text
.XTextDocument myDoc
= openWriter(xContext
);
86 //*************************************************************************
89 //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
91 // For this purpose get the Text-Object of the document an create the
92 // cursor. Now it is possible to insert a text at the cursor-position
94 //*************************************************************************
97 //getting the text object
98 com
.sun
.star
.text
.XText xText
= myDoc
.getText();
100 //create a cursor object
101 com
.sun
.star
.text
.XTextCursor xTCursor
= xText
.createTextCursor();
103 //inserting some Text
104 xText
.insertString( xTCursor
, "The first line in the newly created text document.\n", false );
106 //inserting a second line
107 xText
.insertString( xTCursor
, "Now we're in the second line\n", false );
109 //*************************************************************************
112 //oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooo
113 // insert a text table.
114 // For this purpose get MultiServiceFactory of the document, create an
115 // instance of com.sun.star.text.TextTable and initialize it. Now it can
116 // be inserted at the cursor position via insertTextContent.
117 // After that some properties are changed and some data is inserted.
118 //*************************************************************************
120 //inserting a text table
121 System
.out
.println("Inserting a text table");
123 //getting MSF of the document
124 com
.sun
.star
.lang
.XMultiServiceFactory xDocMSF
=
125 (com
.sun
.star
.lang
.XMultiServiceFactory
) UnoRuntime
.queryInterface(
126 com
.sun
.star
.lang
.XMultiServiceFactory
.class, myDoc
);
128 //create instance of a text table
129 com
.sun
.star
.text
.XTextTable xTT
= null;
132 Object oInt
= xDocMSF
.createInstance("com.sun.star.text.TextTable");
133 xTT
= (com
.sun
.star
.text
.XTextTable
)
134 UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextTable
.class,oInt
);
135 } catch (Exception e
) {
136 System
.err
.println("Couldn't create instance "+ e
);
137 e
.printStackTrace(System
.err
);
140 //initialize the text table with 4 columns an 4 rows
143 com
.sun
.star
.beans
.XPropertySet xTTRowPS
= null;
147 xText
.insertTextContent(xTCursor
, xTT
, false);
149 com
.sun
.star
.container
.XIndexAccess xTTRows
= xTT
.getRows();
150 xTTRowPS
= (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
151 com
.sun
.star
.beans
.XPropertySet
.class, xTTRows
.getByIndex(0));
153 } catch (Exception e
) {
154 System
.err
.println("Couldn't insert the table " + e
);
155 e
.printStackTrace(System
.err
);
159 // get the property set of the text table
161 com
.sun
.star
.beans
.XPropertySet xTTPS
= (com
.sun
.star
.beans
.XPropertySet
)
162 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class, xTT
);
164 // Change the BackColor
166 xTTPS
.setPropertyValue("BackTransparent", new Boolean(false));
167 xTTPS
.setPropertyValue("BackColor",new Integer(13421823));
168 xTTRowPS
.setPropertyValue("BackTransparent", new Boolean(false));
169 xTTRowPS
.setPropertyValue("BackColor",new Integer(6710932));
171 } catch (Exception e
) {
172 System
.err
.println("Couldn't change the color " + e
);
173 e
.printStackTrace(System
.err
);
176 // write Text in the Table headers
177 System
.out
.println("Write text in the table headers");
179 insertIntoCell("A1","FirstColumn", xTT
);
180 insertIntoCell("B1","SecondColumn", xTT
) ;
181 insertIntoCell("C1","ThirdColumn", xTT
) ;
182 insertIntoCell("D1","SUM", xTT
) ;
185 //Insert Something in the text table
186 System
.out
.println("Insert something in the text table");
188 (xTT
.getCellByName("A2")).setValue(22.5);
189 (xTT
.getCellByName("B2")).setValue(5615.3);
190 (xTT
.getCellByName("C2")).setValue(-2315.7);
191 (xTT
.getCellByName("D2")).setFormula("sum <A2:C2>");
193 (xTT
.getCellByName("A3")).setValue(21.5);
194 (xTT
.getCellByName("B3")).setValue(615.3);
195 (xTT
.getCellByName("C3")).setValue(-315.7);
196 (xTT
.getCellByName("D3")).setFormula("sum <A3:C3>");
198 (xTT
.getCellByName("A4")).setValue(121.5);
199 (xTT
.getCellByName("B4")).setValue(-615.3);
200 (xTT
.getCellByName("C4")).setValue(415.7);
201 (xTT
.getCellByName("D4")).setFormula("sum <A4:C4>");
204 //oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooo
205 // insert a colored text.
206 // Get the propertySet of the cursor, change the CharColor and add a
207 // shadow. Then insert the Text via InsertString at the cursor position.
208 //*************************************************************************
210 // get the property set of the cursor
211 com
.sun
.star
.beans
.XPropertySet xTCPS
= (com
.sun
.star
.beans
.XPropertySet
)
212 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class,
215 Object oldValue
= null;
217 // Change the CharColor and add a Shadow
219 xTCPS
.setPropertyValue("CharColor",new Integer(255));
220 xTCPS
.setPropertyValue("CharShadowed", new Boolean(true));
221 } catch (Exception e
) {
222 System
.err
.println("Couldn't change the color " + e
);
223 e
.printStackTrace(System
.err
);
226 //create a paragraph break
228 xText
.insertControlCharacter(xTCursor
,
229 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false);
231 } catch (Exception e
) {
232 System
.err
.println("Couldn't insert break "+ e
);
233 e
.printStackTrace(System
.err
);
236 //inserting colored Text
237 System
.out
.println("Inserting colored Text");
239 xText
.insertString(xTCursor
, " This is a colored Text - blue with shadow\n",
242 //create a paragraph break
244 xText
.insertControlCharacter(xTCursor
,
245 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false);
247 } catch (Exception e
) {
248 System
.err
.println("Couldn't insert break "+ e
);
249 e
.printStackTrace(System
.err
);
252 //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooo
253 // insert a text frame.
254 // create an instance of com.sun.star.text.TextFrame using the MSF of the
255 // document. Change some properties an insert it.
256 // Now get the text-Object of the frame an the corresponding cursor.
257 // Insert some text via insertString.
258 //*************************************************************************
260 // Create a TextFrame
261 com
.sun
.star
.text
.XTextFrame xTF
= null;
262 com
.sun
.star
.drawing
.XShape xTFS
= null;
265 Object oInt
= xDocMSF
.createInstance("com.sun.star.text.TextFrame");
266 xTF
= (com
.sun
.star
.text
.XTextFrame
) UnoRuntime
.queryInterface(
267 com
.sun
.star
.text
.XTextFrame
.class,oInt
);
268 xTFS
= (com
.sun
.star
.drawing
.XShape
) UnoRuntime
.queryInterface(
269 com
.sun
.star
.drawing
.XShape
.class,oInt
);
271 com
.sun
.star
.awt
.Size aSize
= new com
.sun
.star
.awt
.Size();
276 } catch (Exception e
) {
277 System
.err
.println("Couldn't create instance "+ e
);
278 e
.printStackTrace(System
.err
);
281 // get the property set of the text frame
282 com
.sun
.star
.beans
.XPropertySet xTFPS
= (com
.sun
.star
.beans
.XPropertySet
)
283 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class, xTF
);
285 // Change the AnchorType
287 xTFPS
.setPropertyValue("AnchorType",
288 com
.sun
.star
.text
.TextContentAnchorType
.AS_CHARACTER
);
289 } catch (Exception e
) {
290 System
.err
.println("Couldn't change the color " + e
);
291 e
.printStackTrace(System
.err
);
295 System
.out
.println("Insert the text frame");
298 xText
.insertTextContent(xTCursor
, xTF
, false);
299 } catch (Exception e
) {
300 System
.err
.println("Couldn't insert the frame " + e
);
301 e
.printStackTrace(System
.err
);
304 //getting the text object of Frame
305 com
.sun
.star
.text
.XText xTextF
= xTF
.getText();
307 //create a cursor object
308 com
.sun
.star
.text
.XTextCursor xTCF
= xTextF
.createTextCursor();
310 //inserting some Text
311 xTextF
.insertString(xTCF
,
312 "The first line in the newly created text frame.", false);
315 xTextF
.insertString(xTCF
,
316 "\nWith this second line the height of the frame raises.", false);
318 //insert a paragraph break
320 xText
.insertControlCharacter(xTCursor
,
321 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false );
323 } catch (Exception e
) {
324 System
.err
.println("Couldn't insert break "+ e
);
325 e
.printStackTrace(System
.err
);
328 // Change the CharColor and add a Shadow
330 xTCPS
.setPropertyValue("CharColor",new Integer(65536));
331 xTCPS
.setPropertyValue("CharShadowed", new Boolean(false));
332 } catch (Exception e
) {
333 System
.err
.println("Couldn't change the color " + e
);
334 e
.printStackTrace(System
.err
);
337 xText
.insertString(xTCursor
, " That's all for now !!", false );
339 System
.out
.println("done");
345 public static com
.sun
.star
.text
.XTextDocument
openWriter(
346 com
.sun
.star
.uno
.XComponentContext xContext
)
349 com
.sun
.star
.frame
.XComponentLoader xCLoader
;
350 com
.sun
.star
.text
.XTextDocument xDoc
= null;
351 com
.sun
.star
.lang
.XComponent xComp
= null;
354 // get the remote office service manager
355 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
356 xContext
.getServiceManager();
358 Object oDesktop
= xMCF
.createInstanceWithContext(
359 "com.sun.star.frame.Desktop", xContext
);
361 xCLoader
= (com
.sun
.star
.frame
.XComponentLoader
)
362 UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XComponentLoader
.class,
364 com
.sun
.star
.beans
.PropertyValue
[] szEmptyArgs
=
365 new com
.sun
.star
.beans
.PropertyValue
[0];
366 String strDoc
= "private:factory/swriter";
367 xComp
= xCLoader
.loadComponentFromURL(strDoc
, "_blank", 0, szEmptyArgs
);
368 xDoc
= (com
.sun
.star
.text
.XTextDocument
)
369 UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextDocument
.class,
372 } catch(Exception e
){
373 System
.err
.println(" Exception " + e
);
374 e
.printStackTrace(System
.err
);
379 public static void insertIntoCell(String CellName
, String theText
,
380 com
.sun
.star
.text
.XTextTable xTTbl
) {
382 com
.sun
.star
.text
.XText xTableText
= (com
.sun
.star
.text
.XText
)
383 UnoRuntime
.queryInterface(com
.sun
.star
.text
.XText
.class,
384 xTTbl
.getCellByName(CellName
));
386 //create a cursor object
387 com
.sun
.star
.text
.XTextCursor xTC
= xTableText
.createTextCursor();
389 com
.sun
.star
.beans
.XPropertySet xTPS
= (com
.sun
.star
.beans
.XPropertySet
)
390 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class, xTC
);
393 xTPS
.setPropertyValue("CharColor",new Integer(16777215));
394 } catch (Exception e
) {
395 System
.err
.println(" Exception " + e
);
396 e
.printStackTrace(System
.err
);
399 //inserting some Text
400 xTableText
.setString( theText
);