Update ooo320-m1
[ooovba.git] / odk / examples / java / Text / GraphicsInserter.java
blobd47b405517899b8483f12ded2c8f7cd50a072a21
1 /*************************************************************************
3 * $RCSfile: GraphicsInserter.java,v $
5 * $Revision: 1.6 $
7 * last change: $Author: kz $ $Date: 2005-10-05 14:43:07 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
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
17 * are met:
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 import com.sun.star.uno.UnoRuntime;
43 import java.io.PrintWriter;
44 import java.io.BufferedWriter;
45 import java.io.FileWriter;
48 public class GraphicsInserter {
49 public static void main(String args[]) {
50 if ( args.length < 1 )
52 System.out.println(
53 "usage: java -jar GraphicsInserter.jar \"<Graphic URL|path>\"" );
54 System.out.println( "\ne.g.:" );
55 System.out.println(
56 "java -jar GraphicsInserter.jar \"file:///f:/TestGraphics.gif\"" );
57 System.exit( 1 );
60 com.sun.star.uno.XComponentContext xContext = null;
62 try {
64 // bootstrap UNO and get the remote component context. The context can
65 // be used to get the service manager
66 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
67 System.out.println("Connected to a running office ...");
69 // get the remote office service manager
70 com.sun.star.lang.XMultiComponentFactory xMCF =
71 xContext.getServiceManager();
73 /* A desktop environment contains tasks with one or more
74 frames in which components can be loaded. Desktop is the
75 environment for components which can instanciate within
76 frames. */
77 com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop)
78 UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class,
79 xMCF.createInstanceWithContext("com.sun.star.frame.Desktop",
80 xContext ) );
82 com.sun.star.frame.XComponentLoader xCompLoader =
83 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface(
84 com.sun.star.frame.XComponentLoader.class, xDesktop);
86 // Load a Writer document, which will be automaticly displayed
87 com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL(
88 "private:factory/swriter", "_blank", 0,
89 new com.sun.star.beans.PropertyValue[0]);
91 // Querying for the interface XTextDocument on the xcomponent
92 com.sun.star.text.XTextDocument xTextDoc =
93 (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface(
94 com.sun.star.text.XTextDocument.class, xComp);
96 // Querying for the interface XMultiServiceFactory on the xtextdocument
97 com.sun.star.lang.XMultiServiceFactory xMSFDoc =
98 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
99 com.sun.star.lang.XMultiServiceFactory.class, xTextDoc);
101 // Providing a log file for output
102 PrintWriter printwriterLog = new PrintWriter(
103 new BufferedWriter( new FileWriter("log.txt") ) );
105 Object oGraphic = null;
106 try {
107 // Creating the service GraphicObject
108 oGraphic =
109 xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject");
111 catch ( Exception exception ) {
112 System.out.println( "Could not create instance" );
113 exception.printStackTrace( printwriterLog );
116 // Getting the text
117 com.sun.star.text.XText xText = xTextDoc.getText();
119 // Getting the cursor on the document
120 com.sun.star.text.XTextCursor xTextCursor = xText.createTextCursor();
122 // Querying for the interface XTextContent on the GraphicObject
123 com.sun.star.text.XTextContent xTextContent =
124 (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
125 com.sun.star.text.XTextContent.class, oGraphic );
127 // Printing information to the log file
128 printwriterLog.println( "inserting graphic" );
129 try {
130 // Inserting the content
131 xText.insertTextContent(xTextCursor, xTextContent, true);
132 } catch ( Exception exception ) {
133 System.out.println( "Could not insert Content" );
134 exception.printStackTrace(System.err);
137 // Printing information to the log file
138 printwriterLog.println( "adding graphic" );
140 // Querying for the interface XPropertySet on GraphicObject
141 com.sun.star.beans.XPropertySet xPropSet =
142 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
143 com.sun.star.beans.XPropertySet.class, oGraphic);
144 try {
145 // Creating a string for the graphic url
146 java.io.File sourceFile = new java.io.File(args[0]);
147 StringBuffer sUrl = new StringBuffer("file:///");
148 sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
149 System.out.println( "insert graphic \"" + sUrl + "\"");
151 // Setting the anchor type
152 xPropSet.setPropertyValue("AnchorType",
153 com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH );
155 // Setting the graphic url
156 xPropSet.setPropertyValue( "GraphicURL", sUrl.toString() );
158 // Setting the horizontal position
159 xPropSet.setPropertyValue( "HoriOrientPosition",
160 new Integer( 5500 ) );
162 // Setting the vertical position
163 xPropSet.setPropertyValue( "VertOrientPosition",
164 new Integer( 4200 ) );
166 // Setting the width
167 xPropSet.setPropertyValue( "Width", new Integer( 4400 ) );
169 // Setting the height
170 xPropSet.setPropertyValue( "Height", new Integer( 4000 ) );
171 } catch ( Exception exception ) {
172 System.out.println( "Couldn't set property 'GraphicURL'" );
173 exception.printStackTrace( printwriterLog );
176 xContext = null;
178 System.exit(0);
180 catch( Exception e ) {
181 e.printStackTrace(System.err);
182 System.exit(1);