To comment on the following update, log in, then open the issue: http://www.openoffice.org/issues/show_bug.cgi?id=102066 Issue #|102066 Summary|[JAVA] Printing *.odt Component|Word processor Version|OOO310m11 Platform|Macintosh URL| OS/Version|Mac OS X Status|UNCONFIRMED Status whiteboard| Keywords| Resolution| Issue type|DEFECT Priority|P3 Subcomponent|printing Assigned to|writerneedsconfirm Reported by|ict
------- Additional comments from [email protected] Tue May 19 15:58:32 +0000 2009 ------- Hi, I cannot print my odt file using my code (which seems to be correct because it works on Windows replacing the launchSoffiche() function... I can give you the code if you want). I'm using an iMac (10.5.7) and i tried to print my file with OOo 3.0.1 and OOo 3.1.0 without success. I tried to replace "Default" by the name of my printer but it doesn't work. The application seems to freeze during the setPrinter(...). I'm obliged to kill OOo process after the test because it doesn't work anymore. (apologies for my english, if you don't understand a sentence I'll try to reformulate it) Here's my code : [code] import java.io.File; import java.io.IOException; import java.util.Hashtable; import java.util.Vector; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.bridge.XUnoUrlResolver; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XDesktop; import com.sun.star.lang.EventObject; import com.sun.star.lang.XComponent; import com.sun.star.lang.XEventListener; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.uno.Exception; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.view.XPrintable; public class launch { /** Lock permettant de ne pas flooder ooo de demandes de session */ private final static java.util.concurrent.locks.Lock lckOOO = new java.util.concurrent.locks.ReentrantLock(); /** fichiers ouverts */ private static Vector<File> isOpened = new Vector<File>(); /** Hashtable Fichier/XComponent */ private static Hashtable<File, XComponent> listeDocument = new Hashtable<File, XComponent>(); /** Le composant openOffice.org. */ private static XComponent xComponent = null; /** * Ouvre un document OpenOffice */ public static void ouvrir(final File fichier) { try { lckOOO.lock(); if (fichier != null && fichier.getAbsolutePath() != null) { isOpened.add(fichier); try { // Lance OpenOffice launchSoffice(); // Ouvrir un doc existant en arriere plan ou non PropertyValue[] prop = new PropertyValue[1]; prop[0] = new PropertyValue(); // Ouvre la connexion avec OpenOffice XDesktop xDesktop = null; xDesktop = getXDesktop(); XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class, xDesktop); // Blocage ici ? cf. http://www.oooforum.org/forum/viewtopic.phtml?t=33703 xComponent = xComponentLoader.loadComponentFromURL("file:///" + fichier.getPath(), "_blank", 0, prop); } catch (java.lang.Exception e) { e.printStackTrace(); } // Ajoute l'objet XTextDocument à la liste des documents ouverts listeDocument.put(fichier, xComponent); xComponent.addEventListener(new XEventListener(){ public void disposing(EventObject ev) { isOpened.remove(fichier); } }); new Thread("OpenOfficeOuvrir"){ public void run(){ int i=0; while ( isOpened.contains(fichier) ) try{ Thread.sleep(300); if (++i % 1000 == 0) System.out.println("" + i + "-Thread en exécution no" + Thread.currentThread().getName()); } catch (InterruptedException e){ e.printStackTrace(); } try{ // on est obligé d'attendre que le fichier soit écrit par OOo... Thread.sleep(300); } catch (InterruptedException e){ e.printStackTrace(); } } }.start(); } } finally { lckOOO.unlock(); } } /** * Imprime un fichier */ public static void imprimer(File fichier, int nombreCopies) { try { lckOOO.lock(); if (fichier != null) { XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(XPrintable.class, getXComponent(fichier)); // Définie les propriétés de l'imprimante PropertyValue[] printerDesc = new PropertyValue[1]; printerDesc[0] = new PropertyValue(); printerDesc[0].Name = "Name"; printerDesc[0].Value = "Default"; // Définie les options pour l'impression PropertyValue[] printOpts = new PropertyValue[2]; printOpts[0] = new PropertyValue(); printOpts[0].Name = "Wait"; printOpts[0].Value = new Boolean(true); printOpts[1] = new PropertyValue(); printOpts[1].Name = "CopyCount"; printOpts[1].Value =new Short((short) nombreCopies); try { // Imprime xPrintable.setPrinter(printerDesc); xPrintable.print(printOpts); } catch (Exception e) { e.printStackTrace(); } } } finally { lckOOO.unlock(); } } /** * Lance OpenOffice */ protected static void launchSoffice() { Runtime rt = Runtime.getRuntime(); try { // Lance OOo en faisant en sorte qu'il écoute sur le port 8100 rt.exec("/ict/headlessOOo.sh"); // Contenu du script : // #!/bin/sh // `/Applications/OpenOffice.org.app/Contents/MacOS/soffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -headless &` } catch (IOException e){ e.printStackTrace(); } } /** * Retourne le XTextDocument correspondant au fichier passé en paramètre (dans la liste des fichiers ouverts) */ public static XComponent getXComponent(File fichier) { return listeDocument.get(fichier); } /** * Etablit la connexion avec OOo */ private static XDesktop getXDesktop() { XMultiComponentFactory xRemoteServiceManager = null; XComponentContext xRemoteContext = null; XDesktop xDesktop = null; try { xRemoteServiceManager = getRemoteServiceManager(xRemoteContext, "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"); } catch (java.lang.Exception e){ //works from Patch 1 e.printStackTrace(); xRemoteContext = null; } Object desktop = null; try { desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext); } catch (Exception e) { e.printStackTrace(); } xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, desktop); return xDesktop; } /** * Etablit la connexion avec OOo */ private static XMultiComponentFactory getRemoteServiceManager(XComponentContext xRemoteContext, String unoUrl) { if (xRemoteContext == null) { XComponentContext xLocalContext; boolean test = true; int i=0; try { xLocalContext = Bootstrap.createInitialComponentContext(null); XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager(); Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xLocalContext ); XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver); Object initialObject = xUnoUrlResolver.resolve(unoUrl); XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, initialObject); Object context = xPropertySet.getPropertyValue("DefaultContext"); xRemoteContext = (XComponentContext)UnoRuntime.queryInterface(XComponentContext.class, context); test=false; } catch (java.lang.Exception e) { e.printStackTrace(); i++; } while ( test && i<4 && i!=0 ){ try {Thread.sleep(5000);} catch (InterruptedException e1) {e1.printStackTrace();} try { xLocalContext = Bootstrap.createInitialComponentContext(null); XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager(); Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xLocalContext ); XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver); Object initialObject = xUnoUrlResolver.resolve(unoUrl); XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, initialObject); Object context = xPropertySet.getPropertyValue("DefaultContext"); xRemoteContext = (XComponentContext)UnoRuntime.queryInterface(XComponentContext.class, context); test=false; } catch (java.lang.Exception e) { e.printStackTrace(); i++; } } if ( i==5 ) { try {Thread.sleep(10000);} catch (InterruptedException e1) {e1.printStackTrace();} try { xLocalContext = Bootstrap.createInitialComponentContext(null); XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager(); Object urlResolver = xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xLocalContext ); XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver); Object initialObject = xUnoUrlResolver.resolve(unoUrl); XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, initialObject); Object context = xPropertySet.getPropertyValue("DefaultContext"); xRemoteContext = (XComponentContext)UnoRuntime.queryInterface(XComponentContext.class, context); test=false; } catch (java.lang.Exception e) { e.printStackTrace(); } } } return xRemoteContext.getServiceManager(); } public static void main(String[] args) { /** Le fichier à imprimer */ File fichier = new File("/Users/ict/Documents/Test ict.odt"); ouvrir(fichier); imprimer(fichier,1); } }[/code] Ronny. --------------------------------------------------------------------- Please do not reply to this automatically generated notification from Issue Tracker. Please log onto the website and enter your comments. http://qa.openoffice.org/issue_handling/project_issues.html#notification --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
