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