bump product version to 7.2.5.1
[LibreOffice.git] / odk / examples / DevelopersGuide / Drawing / GraphicExportDemo.java
blob2b8ad77dec294af32f91c8ed6970a4b0b7ad47c1
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // __________ Imports __________
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.lang.XComponent;
41 import com.sun.star.beans.PropertyValue;
43 import com.sun.star.document.XExporter;
44 import com.sun.star.document.XFilter;
46 import com.sun.star.drawing.XDrawPage;
48 // __________ Implementation __________
50 // text demo
52 public class GraphicExportDemo
54 public static void main( String args[] )
56 if ( args.length < 3 )
58 System.out.println( "usage: GraphicExportDemo SourceURL DestinationURL PageIndexToExport" );
59 System.exit(1);
62 XComponent xComponent = null;
63 try
65 // get the remote office context of a running office (a new office
66 // instance is started if necessary)
67 com.sun.star.uno.XComponentContext xOfficeContext = Helper.connect();
69 // suppress Presentation Autopilot when opening the document
70 // properties are the same as described for
71 // com.sun.star.document.MediaDescriptor
72 PropertyValue[] pPropValues = new PropertyValue[ 1 ];
73 pPropValues[ 0 ] = new PropertyValue();
74 pPropValues[ 0 ].Name = "Silent";
75 pPropValues[ 0 ].Value = Boolean.TRUE;
77 java.io.File sourceFile = new java.io.File(args[0]);
78 StringBuffer sUrl = new StringBuffer("file:///");
79 sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
81 xComponent = Helper.createDocument( xOfficeContext,
82 sUrl.toString(), "_blank", 0,
83 pPropValues );
85 Object GraphicExportFilter =
86 xOfficeContext.getServiceManager().createInstanceWithContext(
87 "com.sun.star.drawing.GraphicExportFilter", xOfficeContext);
88 XExporter xExporter = UnoRuntime.queryInterface( XExporter.class, GraphicExportFilter );
90 PropertyValue aProps[] = new PropertyValue[2];
91 aProps[0] = new PropertyValue();
92 aProps[0].Name = "MediaType";
93 aProps[0].Value = "image/gif";
95 /* some graphics e.g. the Windows Metafile does not have a Media Type,
96 for this case
97 aProps[0].Name = "FilterName"; // it is possible to set a FilterName
98 aProps[0].Value = "WMF";
100 java.io.File destFile = new java.io.File(args[1]);
101 java.net.URL destUrl = destFile.toURI().toURL();
103 aProps[1] = new PropertyValue();
104 aProps[1].Name = "URL";
105 aProps[1].Value = destUrl.toString();//args[ 1 ];
107 int nPageIndex = Integer.parseInt( args[ 2 ] );
108 if ( nPageIndex < PageHelper.getDrawPageCount( xComponent ) &&
109 nPageIndex > 1 )
111 XDrawPage xPage = PageHelper.getDrawPageByIndex( xComponent,
112 nPageIndex );
113 XComponent xComp = UnoRuntime.queryInterface( XComponent.class, xPage );
114 xExporter.setSourceDocument( xComp );
115 XFilter xFilter = UnoRuntime.queryInterface( XFilter.class, xExporter );
116 xFilter.filter( aProps );
117 System.out.println( "*** graphics on page \"" + nPageIndex
118 + "\" from file \"" + args[0]
119 + "\" exported under the name \""
120 + args[1] + "\" in the local directory" );
121 } else
123 System.out.println( "page index not in range" );
127 // close the document
128 com.sun.star.util.XCloseable xCloseable = UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
129 xComponent);
131 if (xCloseable != null )
132 xCloseable.close(false);
133 else
134 xComponent.dispose();
136 System.out.println("*** document closed!");
139 catch( Exception ex )
141 System.out.println( ex );
144 System.exit( 0 );
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */