1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 35 import com.sun.star.uno.UnoRuntime; 36 37 import java.io.PrintWriter; 38 import java.io.BufferedWriter; 39 import java.io.FileWriter; 40 41 42 public class GraphicsInserter { 43 public static void main(String args[]) { 44 if ( args.length < 1 ) 45 { 46 System.out.println( 47 "usage: java -jar GraphicsInserter.jar \"<Graphic URL|path>\"" ); 48 System.out.println( "\ne.g.:" ); 49 System.out.println( 50 "java -jar GraphicsInserter.jar \"file:///f:/TestGraphics.gif\"" ); 51 System.exit( 1 ); 52 } 53 54 com.sun.star.uno.XComponentContext xContext = null; 55 56 try { 57 58 // bootstrap UNO and get the remote component context. The context can 59 // be used to get the service manager 60 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 61 System.out.println("Connected to a running office ..."); 62 63 // get the remote office service manager 64 com.sun.star.lang.XMultiComponentFactory xMCF = 65 xContext.getServiceManager(); 66 67 /* A desktop environment contains tasks with one or more 68 frames in which components can be loaded. Desktop is the 69 environment for components which can instanciate within 70 frames. */ 71 com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop) 72 UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, 73 xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", 74 xContext ) ); 75 76 com.sun.star.frame.XComponentLoader xCompLoader = 77 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface( 78 com.sun.star.frame.XComponentLoader.class, xDesktop); 79 80 // Load a Writer document, which will be automaticly displayed 81 com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL( 82 "private:factory/swriter", "_blank", 0, 83 new com.sun.star.beans.PropertyValue[0]); 84 85 // Querying for the interface XTextDocument on the xcomponent 86 com.sun.star.text.XTextDocument xTextDoc = 87 (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface( 88 com.sun.star.text.XTextDocument.class, xComp); 89 90 // Querying for the interface XMultiServiceFactory on the xtextdocument 91 com.sun.star.lang.XMultiServiceFactory xMSFDoc = 92 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface( 93 com.sun.star.lang.XMultiServiceFactory.class, xTextDoc); 94 95 // Providing a log file for output 96 PrintWriter printwriterLog = new PrintWriter( 97 new BufferedWriter( new FileWriter("log.txt") ) ); 98 99 Object oGraphic = null; 100 try { 101 // Creating the service GraphicObject 102 oGraphic = 103 xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject"); 104 } 105 catch ( Exception exception ) { 106 System.out.println( "Could not create instance" ); 107 exception.printStackTrace( printwriterLog ); 108 } 109 110 // Getting the text 111 com.sun.star.text.XText xText = xTextDoc.getText(); 112 113 // Getting the cursor on the document 114 com.sun.star.text.XTextCursor xTextCursor = xText.createTextCursor(); 115 116 // Querying for the interface XTextContent on the GraphicObject 117 com.sun.star.text.XTextContent xTextContent = 118 (com.sun.star.text.XTextContent)UnoRuntime.queryInterface( 119 com.sun.star.text.XTextContent.class, oGraphic ); 120 121 // Printing information to the log file 122 printwriterLog.println( "inserting graphic" ); 123 try { 124 // Inserting the content 125 xText.insertTextContent(xTextCursor, xTextContent, true); 126 } catch ( Exception exception ) { 127 System.out.println( "Could not insert Content" ); 128 exception.printStackTrace(System.err); 129 } 130 131 // Printing information to the log file 132 printwriterLog.println( "adding graphic" ); 133 134 // Querying for the interface XPropertySet on GraphicObject 135 com.sun.star.beans.XPropertySet xPropSet = 136 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 137 com.sun.star.beans.XPropertySet.class, oGraphic); 138 try { 139 // Creating a string for the graphic url 140 java.io.File sourceFile = new java.io.File(args[0]); 141 StringBuffer sUrl = new StringBuffer("file:///"); 142 sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/')); 143 System.out.println( "insert graphic \"" + sUrl + "\""); 144 145 // Setting the anchor type 146 xPropSet.setPropertyValue("AnchorType", 147 com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH ); 148 149 // Setting the graphic url 150 xPropSet.setPropertyValue( "GraphicURL", sUrl.toString() ); 151 152 // Setting the horizontal position 153 xPropSet.setPropertyValue( "HoriOrientPosition", 154 new Integer( 5500 ) ); 155 156 // Setting the vertical position 157 xPropSet.setPropertyValue( "VertOrientPosition", 158 new Integer( 4200 ) ); 159 160 // Setting the width 161 xPropSet.setPropertyValue( "Width", new Integer( 4400 ) ); 162 163 // Setting the height 164 xPropSet.setPropertyValue( "Height", new Integer( 4000 ) ); 165 } catch ( Exception exception ) { 166 System.out.println( "Couldn't set property 'GraphicURL'" ); 167 exception.printStackTrace( printwriterLog ); 168 } 169 170 xContext = null; 171 172 System.exit(0); 173 } 174 catch( Exception e ) { 175 e.printStackTrace(System.err); 176 System.exit(1); 177 } 178 } 179 } 180