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