1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.uno.UnoRuntime;
25 
26 
27 public class DocumentPrinter {
main(String args[])28     public static void main(String args[]) {
29         if ( args.length < 3 ) {
30             System.out.println("usage: java -jar DocumentLoader.jar " +
31                                "\"<Favoured printer>\" \"<URL|path>\" \"<Pages>\"");
32             System.out.println( "\ne.g.:" );
33             System.out.println("java -jar DocumentLoader.jar \"amadeus\" " +
34                                "\"file:///f:/TestPrint.odt\" \"1-3;7;9\"");
35             System.exit(1);
36       }
37 
38         com.sun.star.uno.XComponentContext xContext = null;
39 
40         try {
41             // get the remote office component context
42             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
43             System.out.println("Connected to a running office ...");
44 
45             // get the remote office service manager
46             com.sun.star.lang.XMultiComponentFactory xMCF =
47                 xContext.getServiceManager();
48 
49             Object oDesktop = xMCF.createInstanceWithContext(
50                 "com.sun.star.frame.Desktop", xContext);
51 
52             com.sun.star.frame.XComponentLoader xCompLoader =
53                 (com.sun.star.frame.XComponentLoader)
54                      UnoRuntime.queryInterface(
55                          com.sun.star.frame.XComponentLoader.class, oDesktop);
56 
57             java.io.File sourceFile = new java.io.File(args[1]);
58             StringBuffer sUrl = new StringBuffer("file:///");
59             sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
60 
61             // Load a Writer document, which will be automatically displayed
62             com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL(
63                 sUrl.toString(), "_blank", 0,
64                 new com.sun.star.beans.PropertyValue[0] );
65 
66             // Querying for the interface XPrintable on the loaded document
67             com.sun.star.view.XPrintable xPrintable =
68                 (com.sun.star.view.XPrintable)UnoRuntime.queryInterface(
69                     com.sun.star.view.XPrintable.class, xComp);
70 
71             // Setting the property "Name" for the favoured printer (name of
72             // IP address)
73             com.sun.star.beans.PropertyValue propertyValue[] =
74                 new com.sun.star.beans.PropertyValue[1];
75             propertyValue[0] = new com.sun.star.beans.PropertyValue();
76             propertyValue[0].Name = "Name";
77             propertyValue[0].Value = args[ 0 ];
78 
79             // Setting the name of the printer
80             xPrintable.setPrinter( propertyValue );
81 
82             // Setting the property "Pages" so that only the desired pages
83             // will be printed.
84             propertyValue[0] = new com.sun.star.beans.PropertyValue();
85             propertyValue[0].Name = "Pages";
86             propertyValue[0].Value = args[ 2 ];
87 
88             // Printing the loaded document
89             xPrintable.print( propertyValue );
90 
91             System.exit(0);
92         }
93         catch( Exception e ) {
94             e.printStackTrace(System.err);
95             System.exit(1);
96         }
97     }
98 }
99