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 import com.sun.star.uno.UnoRuntime;
36 
37 
38 public class DocumentPrinter {
39     public static void main(String args[]) {
40         if ( args.length < 3 ) {
41             System.out.println("usage: java -jar DocumentLoader.jar " +
42                                "\"<Favoured printer>\" \"<URL|path>\" \"<Pages>\"");
43             System.out.println( "\ne.g.:" );
44             System.out.println("java -jar DocumentLoader.jar \"amadeus\" " +
45                                "\"file:///f:/TestPrint.odt\" \"1-3;7;9\"");
46             System.exit(1);
47       }
48 
49         com.sun.star.uno.XComponentContext xContext = null;
50 
51         try {
52             // get the remote office component context
53             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
54             System.out.println("Connected to a running office ...");
55 
56             // get the remote office service manager
57             com.sun.star.lang.XMultiComponentFactory xMCF =
58                 xContext.getServiceManager();
59 
60             Object oDesktop = xMCF.createInstanceWithContext(
61                 "com.sun.star.frame.Desktop", xContext);
62 
63             com.sun.star.frame.XComponentLoader xCompLoader =
64                 (com.sun.star.frame.XComponentLoader)
65                      UnoRuntime.queryInterface(
66                          com.sun.star.frame.XComponentLoader.class, oDesktop);
67 
68             java.io.File sourceFile = new java.io.File(args[1]);
69             StringBuffer sUrl = new StringBuffer("file:///");
70             sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
71 
72             // Load a Writer document, which will be automaticly displayed
73             com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL(
74                 sUrl.toString(), "_blank", 0,
75                 new com.sun.star.beans.PropertyValue[0] );
76 
77             // Querying for the interface XPrintable on the loaded document
78             com.sun.star.view.XPrintable xPrintable =
79                 (com.sun.star.view.XPrintable)UnoRuntime.queryInterface(
80                     com.sun.star.view.XPrintable.class, xComp);
81 
82             // Setting the property "Name" for the favoured printer (name of
83             // IP address)
84             com.sun.star.beans.PropertyValue propertyValue[] =
85                 new com.sun.star.beans.PropertyValue[1];
86             propertyValue[0] = new com.sun.star.beans.PropertyValue();
87             propertyValue[0].Name = "Name";
88             propertyValue[0].Value = args[ 0 ];
89 
90             // Setting the name of the printer
91             xPrintable.setPrinter( propertyValue );
92 
93             // Setting the property "Pages" so that only the desired pages
94             // will be printed.
95             propertyValue[0] = new com.sun.star.beans.PropertyValue();
96             propertyValue[0].Name = "Pages";
97             propertyValue[0].Value = args[ 2 ];
98 
99             // Printing the loaded document
100             xPrintable.print( propertyValue );
101 
102             System.exit(0);
103         }
104         catch( Exception e ) {
105             e.printStackTrace(System.err);
106             System.exit(1);
107         }
108     }
109 }
110