print printer tray Tobias Krais How to print on different trays? import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.container.XNameAccess; import com.sun.star.container.XNameContainer; import com.sun.star.style.XStyle; import com.sun.star.style.XStyleFamiliesSupplier; import com.sun.star.text.XText; import com.sun.star.text.XTextCursor; import com.sun.star.text.XTextDocument; import com.sun.star.uno.UnoRuntime; import com.sun.star.view.PrintableState; import com.sun.star.view.XPrintJobBroadcaster; import com.sun.star.view.XPrintable; /** * Conditions: XComponent openDocument; * {%internal ../Office/Office.OpenDocumentFromURL.snip} */ /** * This method prints the global defined XComponent in this class. * It takes the name of the printer (the name must have the same value as * shown in the OpenOffice printer dialog). * It also takes the tray of the printer (the name must have the same value * as shown in the OpenOffice printer dialog). * It takes the page numbers you want to print. It must again be the same * value as you use in the OpenOffice printer dialog, e.g "1" for page * number 1 or "2-" for page 2 and following. * * @param printerName * @param printerTray * @param pages */ public void printDocument(String printerName, String printerTray, String pages) { try { // Querying for the interface XPrintable on the loaded document XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, openDocument); XPrintJobBroadcaster selection = (XPrintJobBroadcaster) UnoRuntime.queryInterface(XPrintJobBroadcaster.class, xPrintable); MyXPrintJobListener myXPrintJobListener = new MyXPrintJobListener(); selection.addPrintJobListener(myXPrintJobListener); // Setting the property "Name" for the favoured printer PropertyValue[] printerDesc = new PropertyValue[1]; printerDesc[0] = new PropertyValue(); printerDesc[0].Name = "Name"; printerDesc[0].Value = printerName; // Setting the name of the printer xPrintable.setPrinter(printerDesc); // Setting the property "Pages" so that only the desired pages // will be printed. PropertyValue[] printOpts = new PropertyValue[1]; printOpts[0] = new PropertyValue(); printOpts[0].Name = "Pages"; printOpts[0].Value = pages; XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, openDocument); XText xText = xTextDocument.getText(); /* Manipulate the text .. basically add the text */ // create a text cursor from the cells XText interface XTextCursor xTextCursor = xText.createTextCursor(); // Get the property set of the cell's TextCursor XPropertySet xTextCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); String pageStyleName = xTextCursorProps.getPropertyValue("PageStyleName").toString(); // Get the StyleFamiliesSupplier interface of the document XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDocument); // Use the StyleFamiliesSupplier interface to get the XNameAccess // interface of the actual style families XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xSupplier.getStyleFamilies()); // Access the 'PageStyles' Family XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xFamilies.getByName("PageStyles")); XStyle xStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, xFamily.getByName(pageStyleName)); // Get the property set of the cell's TextCursor XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle); // my PageStyleSetting ... xStyleProps.setPropertyValue("PrinterPaperTray", printerTray); System.out.println("PrinterPaperTray:" + xStyleProps.getPropertyValue("PrinterPaperTray")); xPrintable.print(printOpts); while (myXPrintJobListener.getStatus() == null || myXPrintJobListener.getStatus() == PrintableState.JOB_STARTED) { Thread.sleep(100); } } catch (Exception e) { e.printStackTrace(System.err); } } /** * And here the mentioned MyXPrintJobListener. */ package de.twc.oocom.components; import com.sun.star.view.PrintJobEvent; import com.sun.star.view.PrintableState; import com.sun.star.view.XPrintJobListener; public class MyXPrintJobListener implements XPrintJobListener { private PrintableState status = null; public PrintableState getStatus() { return status; } public void setStatus(PrintableState status) { this.status = status; } /** * The print job event: has to be called when the action is triggered. */ public void printJobEvent(PrintJobEvent printJobEvent) { if(printJobEvent.State == PrintableState.JOB_COMPLETED) { System.out.println("JOB_COMPLETED"); this.setStatus(PrintableState.JOB_COMPLETED); } if(printJobEvent.State == PrintableState.JOB_ABORTED) { System.out.println("JOB_ABORTED"); this.setStatus(PrintableState.JOB_ABORTED); } if(printJobEvent.State == PrintableState.JOB_FAILED) { System.out.println("JOB_FAILED"); this.setStatus(PrintableState.JOB_FAILED); return; } if(printJobEvent.State == PrintableState.JOB_SPOOLED) { System.out.println("JOB_SPOOLED"); this.setStatus(PrintableState.JOB_SPOOLED); } if(printJobEvent.State == PrintableState.JOB_SPOOLING_FAILED) { System.out.println("JOB_SPOOLING_FAILED"); this.setStatus(PrintableState.JOB_SPOOLING_FAILED); return; } if(printJobEvent.State == PrintableState.JOB_STARTED) { System.out.println("JOB_STARTED"); this.setStatus(PrintableState.JOB_STARTED); return; } } /** * Disposing event: ignore. */ public void disposing(com.sun.star.lang.EventObject eventObject) { System.out.println("disposing"); } } Initial version