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