1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir import java.io.BufferedWriter;
28cdf0e10cSrcweir import java.io.FileWriter;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 
31cdf0e10cSrcweir public class GraphicsInserter {
main(String args[])32cdf0e10cSrcweir     public static void main(String args[]) {
33cdf0e10cSrcweir         if ( args.length < 1 )
34cdf0e10cSrcweir         {
35cdf0e10cSrcweir             System.out.println(
36cdf0e10cSrcweir                 "usage: java -jar GraphicsInserter.jar \"<Graphic URL|path>\"" );
37cdf0e10cSrcweir             System.out.println( "\ne.g.:" );
38cdf0e10cSrcweir             System.out.println(
39cdf0e10cSrcweir                 "java -jar GraphicsInserter.jar \"file:///f:/TestGraphics.gif\"" );
40cdf0e10cSrcweir             System.exit( 1 );
41cdf0e10cSrcweir         }
42cdf0e10cSrcweir 
43cdf0e10cSrcweir         com.sun.star.uno.XComponentContext xContext = null;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir         try {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir             // bootstrap UNO and get the remote component context. The context can
48cdf0e10cSrcweir             // be used to get the service manager
49cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
50cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
51cdf0e10cSrcweir 
52cdf0e10cSrcweir             // get the remote office service manager
53cdf0e10cSrcweir             com.sun.star.lang.XMultiComponentFactory xMCF =
54cdf0e10cSrcweir                 xContext.getServiceManager();
55cdf0e10cSrcweir 
56cdf0e10cSrcweir             /* A desktop environment contains tasks with one or more
57cdf0e10cSrcweir                frames in which components can be loaded. Desktop is the
58cdf0e10cSrcweir                environment for components which can instanciate within
59cdf0e10cSrcweir                frames. */
60cdf0e10cSrcweir             com.sun.star.frame.XDesktop xDesktop = (com.sun.star.frame.XDesktop)
61cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class,
62cdf0e10cSrcweir                     xMCF.createInstanceWithContext("com.sun.star.frame.Desktop",
63cdf0e10cSrcweir                                                    xContext ) );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir             com.sun.star.frame.XComponentLoader xCompLoader =
66cdf0e10cSrcweir                 (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface(
67cdf0e10cSrcweir                     com.sun.star.frame.XComponentLoader.class, xDesktop);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             // Load a Writer document, which will be automaticly displayed
70cdf0e10cSrcweir             com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL(
71cdf0e10cSrcweir                 "private:factory/swriter", "_blank", 0,
72cdf0e10cSrcweir                 new com.sun.star.beans.PropertyValue[0]);
73cdf0e10cSrcweir 
74cdf0e10cSrcweir             // Querying for the interface XTextDocument on the xcomponent
75cdf0e10cSrcweir             com.sun.star.text.XTextDocument xTextDoc =
76cdf0e10cSrcweir                 (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface(
77cdf0e10cSrcweir                     com.sun.star.text.XTextDocument.class, xComp);
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             // Querying for the interface XMultiServiceFactory on the xtextdocument
80cdf0e10cSrcweir             com.sun.star.lang.XMultiServiceFactory xMSFDoc =
81cdf0e10cSrcweir                 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
82cdf0e10cSrcweir                     com.sun.star.lang.XMultiServiceFactory.class, xTextDoc);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             // Providing a log file for output
85cdf0e10cSrcweir             PrintWriter printwriterLog = new PrintWriter(
86cdf0e10cSrcweir                 new BufferedWriter( new FileWriter("log.txt") ) );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir             Object oGraphic = null;
89cdf0e10cSrcweir             try {
90cdf0e10cSrcweir                 // Creating the service GraphicObject
91cdf0e10cSrcweir                 oGraphic =
92cdf0e10cSrcweir                     xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject");
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir             catch ( Exception exception ) {
95cdf0e10cSrcweir                 System.out.println( "Could not create instance" );
96cdf0e10cSrcweir                 exception.printStackTrace( printwriterLog );
97cdf0e10cSrcweir             }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir             // Getting the text
100cdf0e10cSrcweir             com.sun.star.text.XText xText = xTextDoc.getText();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir             // Getting the cursor on the document
103cdf0e10cSrcweir             com.sun.star.text.XTextCursor xTextCursor = xText.createTextCursor();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir             // Querying for the interface XTextContent on the GraphicObject
106cdf0e10cSrcweir             com.sun.star.text.XTextContent xTextContent =
107cdf0e10cSrcweir                 (com.sun.star.text.XTextContent)UnoRuntime.queryInterface(
108cdf0e10cSrcweir                     com.sun.star.text.XTextContent.class, oGraphic );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             // Printing information to the log file
111cdf0e10cSrcweir             printwriterLog.println( "inserting graphic" );
112cdf0e10cSrcweir             try {
113cdf0e10cSrcweir                 // Inserting the content
114cdf0e10cSrcweir                 xText.insertTextContent(xTextCursor, xTextContent, true);
115cdf0e10cSrcweir             } catch ( Exception exception ) {
116cdf0e10cSrcweir                 System.out.println( "Could not insert Content" );
117cdf0e10cSrcweir                 exception.printStackTrace(System.err);
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             // Printing information to the log file
121cdf0e10cSrcweir             printwriterLog.println( "adding graphic" );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir             // Querying for the interface XPropertySet on GraphicObject
124cdf0e10cSrcweir             com.sun.star.beans.XPropertySet xPropSet =
125cdf0e10cSrcweir                 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
126cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, oGraphic);
127cdf0e10cSrcweir             try {
128cdf0e10cSrcweir                 // Creating a string for the graphic url
129cdf0e10cSrcweir                 java.io.File sourceFile = new java.io.File(args[0]);
130cdf0e10cSrcweir                 StringBuffer sUrl = new StringBuffer("file:///");
131cdf0e10cSrcweir                 sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
132cdf0e10cSrcweir                 System.out.println( "insert graphic \"" + sUrl + "\"");
133cdf0e10cSrcweir 
134cdf0e10cSrcweir                 // Setting the anchor type
135cdf0e10cSrcweir                 xPropSet.setPropertyValue("AnchorType",
136cdf0e10cSrcweir                            com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir                 // Setting the graphic url
139cdf0e10cSrcweir                 xPropSet.setPropertyValue( "GraphicURL", sUrl.toString() );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir                 // Setting the horizontal position
142cdf0e10cSrcweir                 xPropSet.setPropertyValue( "HoriOrientPosition",
143cdf0e10cSrcweir                                            new Integer( 5500 ) );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir                 // Setting the vertical position
146cdf0e10cSrcweir                 xPropSet.setPropertyValue( "VertOrientPosition",
147cdf0e10cSrcweir                                            new Integer( 4200 ) );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir                 // Setting the width
150cdf0e10cSrcweir                 xPropSet.setPropertyValue( "Width", new Integer( 4400 ) );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                 // Setting the height
153cdf0e10cSrcweir                 xPropSet.setPropertyValue( "Height", new Integer( 4000 ) );
154cdf0e10cSrcweir             } catch ( Exception exception ) {
155cdf0e10cSrcweir                 System.out.println( "Couldn't set property 'GraphicURL'" );
156cdf0e10cSrcweir                 exception.printStackTrace( printwriterLog );
157cdf0e10cSrcweir             }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir             xContext = null;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             System.exit(0);
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         catch( Exception e ) {
164cdf0e10cSrcweir             e.printStackTrace(System.err);
165cdf0e10cSrcweir             System.exit(1);
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir }
169