1 /*************************************************************************
3 * $RCSfile: GraphicExportDemo.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:22:52 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.lang
.XComponent
;
46 import com
.sun
.star
.beans
.PropertyValue
;
48 import com
.sun
.star
.document
.XExporter
;
49 import com
.sun
.star
.document
.XFilter
;
51 import com
.sun
.star
.drawing
.XDrawPage
;
53 // __________ Implementation __________
59 public class GraphicExportDemo
61 public static void main( String args
[] )
63 if ( args
.length
< 3 )
65 System
.out
.println( "usage: GraphicExportDemo SourceURL DestinationURL PageIndexToExport" );
69 XComponent xComponent
= null;
72 // get the remote office context of a running office (a new office
73 // instance is started if necessary)
74 com
.sun
.star
.uno
.XComponentContext xOfficeContext
= Helper
.connect();
76 // suppress Presentation Autopilot when opening the document
77 // properties are the same as described for
78 // com.sun.star.document.MediaDescriptor
79 PropertyValue
[] pPropValues
= new PropertyValue
[ 1 ];
80 pPropValues
[ 0 ] = new PropertyValue();
81 pPropValues
[ 0 ].Name
= "Silent";
82 pPropValues
[ 0 ].Value
= new Boolean( true );
84 java
.io
.File sourceFile
= new java
.io
.File(args
[0]);
85 StringBuffer sUrl
= new StringBuffer("file:///");
86 sUrl
.append(sourceFile
.getCanonicalPath().replace('\\', '/'));
88 xComponent
= Helper
.createDocument( xOfficeContext
,
89 sUrl
.toString(), "_blank", 0,
92 Object GraphicExportFilter
=
93 xOfficeContext
.getServiceManager().createInstanceWithContext(
94 "com.sun.star.drawing.GraphicExportFilter", xOfficeContext
);
95 XExporter xExporter
= (XExporter
)
96 UnoRuntime
.queryInterface( XExporter
.class, GraphicExportFilter
);
98 PropertyValue aProps
[] = new PropertyValue
[2];
99 aProps
[0] = new PropertyValue();
100 aProps
[0].Name
= "MediaType";
101 aProps
[0].Value
= "image/gif";
103 /* some graphics e.g. the Windows Metafile does not have a Media Type,
105 aProps[0].Name = "FilterName"; // it is possible to set a FilterName
106 aProps[0].Value = "WMF";
108 java
.io
.File destFile
= new java
.io
.File(args
[1]);
109 java
.net
.URL destUrl
= destFile
.toURL();
111 aProps
[1] = new PropertyValue();
112 aProps
[1].Name
= "URL";
113 aProps
[1].Value
= destUrl
.toString();//args[ 1 ];
115 int nPageIndex
= Integer
.parseInt( args
[ 2 ] );
116 if ( nPageIndex
< PageHelper
.getDrawPageCount( xComponent
) &&
119 XDrawPage xPage
= PageHelper
.getDrawPageByIndex( xComponent
,
121 XComponent xComp
= (XComponent
)
122 UnoRuntime
.queryInterface( XComponent
.class, xPage
);
123 xExporter
.setSourceDocument( xComp
);
124 XFilter xFilter
= (XFilter
)
125 UnoRuntime
.queryInterface( XFilter
.class, xExporter
);
126 xFilter
.filter( aProps
);
127 System
.out
.println( "*** graphics on page \"" + nPageIndex
128 + "\" from file \"" + args
[0]
129 + "\" exported under the name \""
130 + args
[1] + "\" in the local directory" );
133 System
.out
.println( "page index not in range" );
137 // close the document
138 com
.sun
.star
.util
.XCloseable xCloseable
= (com
.sun
.star
.util
.XCloseable
)
139 UnoRuntime
.queryInterface(com
.sun
.star
.util
.XCloseable
.class,
142 if (xCloseable
!= null )
143 xCloseable
.close(false);
145 xComponent
.dispose();
147 System
.out
.println("*** document closed!");
150 catch( Exception ex
)
152 System
.out
.println( ex
);