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 // __________ Imports __________
25 
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.lang.XComponent;
28 
29 import com.sun.star.beans.PropertyValue;
30 
31 import com.sun.star.document.XExporter;
32 import com.sun.star.document.XFilter;
33 
34 import com.sun.star.drawing.XDrawPage;
35 
36 // __________ Implementation __________
37 
38 /** text demo
39     @author Sven Jacobi
40  */
41 
42 public class GraphicExportDemo
43 {
main( String args[] )44     public static void main( String args[] )
45     {
46 		if ( args.length < 3 )
47 		{
48 			System.out.println( "usage: GraphicExportDemo SourceURL DestinationURL PageIndexToExport" );
49             System.exit(1);
50 		}
51 
52         XComponent xComponent = null;
53         try
54         {
55             // get the remote office context of a running office (a new office
56             // instance is started if necessary)
57 			com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
58 
59             // suppress Presentation Autopilot when opening the document
60             // properties are the same as described for
61             // com.sun.star.document.MediaDescriptor
62             PropertyValue[] pPropValues = new PropertyValue[ 1 ];
63             pPropValues[ 0 ] = new PropertyValue();
64             pPropValues[ 0 ].Name = "Silent";
65             pPropValues[ 0 ].Value = new Boolean( true );
66 
67             java.io.File sourceFile = new java.io.File(args[0]);
68             StringBuffer sUrl = new StringBuffer("file:///");
69             sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
70 
71             xComponent = Helper.createDocument( xOfficeContext,
72                                                 sUrl.toString(), "_blank", 0,
73                                                 pPropValues );
74 
75             Object GraphicExportFilter =
76                 xOfficeContext.getServiceManager().createInstanceWithContext(
77                     "com.sun.star.drawing.GraphicExportFilter", xOfficeContext);
78             XExporter xExporter = (XExporter)
79                 UnoRuntime.queryInterface( XExporter.class, GraphicExportFilter );
80 
81             PropertyValue aProps[] = new PropertyValue[2];
82             aProps[0] = new PropertyValue();
83             aProps[0].Name = "MediaType";
84             aProps[0].Value = "image/gif";
85 
86             /* some graphics e.g. the Windows Metafile does not have a Media Type,
87                for this case
88                aProps[0].Name = "FilterName"; // it is possible to set a FilterName
89                aProps[0].Value = "WMF";
90             */
91             java.io.File destFile = new java.io.File(args[1]);
92             java.net.URL destUrl = destFile.toURL();
93 
94             aProps[1] = new PropertyValue();
95             aProps[1].Name = "URL";
96             aProps[1].Value = destUrl.toString();//args[ 1 ];
97 
98             int nPageIndex = Integer.parseInt( args[ 2 ] );
99             if ( nPageIndex < PageHelper.getDrawPageCount( xComponent ) &&
100                  nPageIndex > 1 )
101 			{
102                 XDrawPage xPage = PageHelper.getDrawPageByIndex( xComponent,
103                                                                  nPageIndex );
104                 XComponent xComp = (XComponent)
105                     UnoRuntime.queryInterface( XComponent.class, xPage );
106                 xExporter.setSourceDocument( xComp );
107                 XFilter xFilter = (XFilter)
108                     UnoRuntime.queryInterface( XFilter.class, xExporter );
109                 xFilter.filter( aProps );
110                 System.out.println( "*** graphics on page \"" + nPageIndex
111                                     + "\" from file \"" + args[0]
112                                     + "\" exported under the name \""
113                                     + args[1] + "\" in the local directory" );
114 			} else
115             {
116                 System.out.println( "page index not in range" );
117             }
118 
119 
120             // close the document
121             com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
122                 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
123                                           xComponent);
124 
125             if (xCloseable != null )
126                 xCloseable.close(false);
127             else
128                 xComponent.dispose();
129 
130             System.out.println("*** document closed!");
131 
132         }
133         catch( Exception ex )
134         {
135             System.out.println( ex );
136         }
137 
138         System.exit( 0 );
139     }
140 }
141 
142