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