1 /*************************************************************************
3 * $RCSfile: DocumentPrinter.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:08:51 $
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 import com
.sun
.star
.uno
.UnoRuntime
;
44 public class DocumentPrinter
{
45 public static void main(String args
[]) {
46 if ( args
.length
< 3 ) {
47 System
.out
.println("usage: java -jar DocumentLoader.jar " +
48 "\"<Favoured printer>\" \"<URL|path>\" \"<Pages>\"");
49 System
.out
.println( "\ne.g.:" );
50 System
.out
.println("java -jar DocumentLoader.jar \"amadeus\" " +
51 "\"file:///f:/TestPrint.odt\" \"1-3;7;9\"");
55 com
.sun
.star
.uno
.XComponentContext xContext
= null;
58 // get the remote office component context
59 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
60 System
.out
.println("Connected to a running office ...");
62 // get the remote office service manager
63 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
64 xContext
.getServiceManager();
66 Object oDesktop
= xMCF
.createInstanceWithContext(
67 "com.sun.star.frame.Desktop", xContext
);
69 com
.sun
.star
.frame
.XComponentLoader xCompLoader
=
70 (com
.sun
.star
.frame
.XComponentLoader
)
71 UnoRuntime
.queryInterface(
72 com
.sun
.star
.frame
.XComponentLoader
.class, oDesktop
);
74 java
.io
.File sourceFile
= new java
.io
.File(args
[1]);
75 StringBuffer sUrl
= new StringBuffer("file:///");
76 sUrl
.append(sourceFile
.getCanonicalPath().replace('\\', '/'));
78 // Load a Writer document, which will be automaticly displayed
79 com
.sun
.star
.lang
.XComponent xComp
= xCompLoader
.loadComponentFromURL(
80 sUrl
.toString(), "_blank", 0,
81 new com
.sun
.star
.beans
.PropertyValue
[0] );
83 // Querying for the interface XPrintable on the loaded document
84 com
.sun
.star
.view
.XPrintable xPrintable
=
85 (com
.sun
.star
.view
.XPrintable
)UnoRuntime
.queryInterface(
86 com
.sun
.star
.view
.XPrintable
.class, xComp
);
88 // Setting the property "Name" for the favoured printer (name of
90 com
.sun
.star
.beans
.PropertyValue propertyValue
[] =
91 new com
.sun
.star
.beans
.PropertyValue
[1];
92 propertyValue
[0] = new com
.sun
.star
.beans
.PropertyValue();
93 propertyValue
[0].Name
= "Name";
94 propertyValue
[0].Value
= args
[ 0 ];
96 // Setting the name of the printer
97 xPrintable
.setPrinter( propertyValue
);
99 // Setting the property "Pages" so that only the desired pages
101 propertyValue
[0] = new com
.sun
.star
.beans
.PropertyValue();
102 propertyValue
[0].Name
= "Pages";
103 propertyValue
[0].Value
= args
[ 2 ];
105 // Printing the loaded document
106 xPrintable
.print( propertyValue
);
110 catch( Exception e
) {
111 e
.printStackTrace(System
.err
);