Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Text / GraphicsInserter.java
blob82a263866e5788857a0aaf3ada57c892d234d490
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 import com.sun.star.uno.UnoRuntime;
37 import java.io.PrintWriter;
38 import java.io.BufferedWriter;
39 import java.io.FileWriter;
42 public class GraphicsInserter {
43 public static void main(String args[]) {
44 if ( args.length < 1 )
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 );
54 com.sun.star.uno.XComponentContext xContext = null;
56 try {
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 ...");
63 // get the remote office service manager
64 com.sun.star.lang.XMultiComponentFactory xMCF =
65 xContext.getServiceManager();
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 = UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class,
72 xMCF.createInstanceWithContext("com.sun.star.frame.Desktop",
73 xContext ) );
75 com.sun.star.frame.XComponentLoader xCompLoader =
76 UnoRuntime.queryInterface(
77 com.sun.star.frame.XComponentLoader.class, xDesktop);
79 // Load a Writer document, which will be automatically displayed
80 com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL(
81 "private:factory/swriter", "_blank", 0,
82 new com.sun.star.beans.PropertyValue[0]);
84 // Querying for the interface XTextDocument on the xcomponent
85 com.sun.star.text.XTextDocument xTextDoc =
86 UnoRuntime.queryInterface(
87 com.sun.star.text.XTextDocument.class, xComp);
89 // Querying for the interface XMultiServiceFactory on the xtextdocument
90 com.sun.star.lang.XMultiServiceFactory xMSFDoc =
91 UnoRuntime.queryInterface(
92 com.sun.star.lang.XMultiServiceFactory.class, xTextDoc);
94 // Providing a log file for output
95 PrintWriter printwriterLog = new PrintWriter(
96 new BufferedWriter( new FileWriter("log.txt") ) );
98 Object oGraphic = null;
99 try {
100 // Creating the service GraphicObject
101 oGraphic =
102 xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject");
104 catch ( Exception exception ) {
105 System.out.println( "Could not create instance" );
106 exception.printStackTrace( printwriterLog );
109 // Getting the text
110 com.sun.star.text.XText xText = xTextDoc.getText();
112 // Getting the cursor on the document
113 com.sun.star.text.XTextCursor xTextCursor = xText.createTextCursor();
115 // Querying for the interface XTextContent on the GraphicObject
116 com.sun.star.text.XTextContent xTextContent =
117 UnoRuntime.queryInterface(
118 com.sun.star.text.XTextContent.class, oGraphic );
120 // Printing information to the log file
121 printwriterLog.println( "inserting graphic" );
122 try {
123 // Inserting the content
124 xText.insertTextContent(xTextCursor, xTextContent, true);
125 } catch ( Exception exception ) {
126 System.out.println( "Could not insert Content" );
127 exception.printStackTrace(System.err);
130 // Printing information to the log file
131 printwriterLog.println( "adding graphic" );
133 // Querying for the interface XPropertySet on GraphicObject
134 com.sun.star.beans.XPropertySet xPropSet =
135 UnoRuntime.queryInterface(
136 com.sun.star.beans.XPropertySet.class, oGraphic);
137 try {
138 // Creating a string for the graphic url
139 java.io.File sourceFile = new java.io.File(args[0]);
140 StringBuffer sUrl = new StringBuffer("file:///");
141 sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
142 System.out.println( "insert graphic \"" + sUrl + "\"");
144 // Setting the anchor type
145 xPropSet.setPropertyValue("AnchorType",
146 com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH );
148 // Setting the graphic url
149 xPropSet.setPropertyValue( "GraphicURL", sUrl.toString() );
151 // Setting the horizontal position
152 xPropSet.setPropertyValue( "HoriOrientPosition",
153 Integer.valueOf( 5500 ) );
155 // Setting the vertical position
156 xPropSet.setPropertyValue( "VertOrientPosition",
157 Integer.valueOf( 4200 ) );
159 // Setting the width
160 xPropSet.setPropertyValue( "Width", Integer.valueOf( 4400 ) );
162 // Setting the height
163 xPropSet.setPropertyValue( "Height", Integer.valueOf( 4000 ) );
164 } catch ( Exception exception ) {
165 System.out.println( "Couldn't set property 'GraphicURL'" );
166 exception.printStackTrace( printwriterLog );
169 xContext = null;
171 System.exit(0);
173 catch( Exception e ) {
174 e.printStackTrace(System.err);
175 System.exit(1);