Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 4.67 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. LibreOffice UNO Java API: how to open a document, execute a macro and close it?
  2. soffice --accept=...
  3.        
  4. try {
  5.         XComponentContext xLocalContext = Bootstrap.createInitialComponentContext(null);
  6.         System.out.println("xLocalContext");
  7.  
  8.         XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
  9.         System.out.println("xLocalServiceManager");
  10.  
  11.         Object urlResolver = xLocalServiceManager.createInstanceWithContext(
  12.                 "com.sun.star.bridge.UnoUrlResolver", xLocalContext);
  13.         System.out.println("urlResolver");
  14.  
  15.         XUnoUrlResolver xUrlResolver =
  16.             (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver);            
  17.         System.out.println("xUrlResolve");
  18.  
  19.         try {
  20.             String uno = "uno:" + unoMode + ",host=" + unoHost + ",port=" + unoPort + ";" + unoProtocol + ";" + unoObjectName;
  21.             Object rInitialObject = xUrlResolver.resolve(uno);
  22.             System.out.println("rInitialObject");
  23.  
  24.             if (null != rInitialObject) {
  25.                 XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(
  26.                     XMultiComponentFactory.class, rInitialObject);
  27.                 System.out.println("xOfficeFactory");
  28.  
  29.                 Object desktop = xOfficeFactory.createInstanceWithContext("com.sun.star.frame.Desktop", xLocalContext);
  30.                 System.out.println("desktop");
  31.  
  32.                 XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
  33.                     XComponentLoader.class, desktop);
  34.                 System.out.println("xComponentLoader");
  35.  
  36.                 PropertyValue[] loadProps = new PropertyValue[3];
  37.  
  38.                 loadProps[0] = new PropertyValue();
  39.                 loadProps[0].Name = "Hidden";
  40.                 loadProps[0].Value = Boolean.TRUE;
  41.  
  42.                 loadProps[1] = new PropertyValue();
  43.                 loadProps[1].Name = "ReadOnly";
  44.                 loadProps[1].Value = Boolean.FALSE;
  45.  
  46.                 loadProps[2] = new PropertyValue();
  47.                 loadProps[2].Name = "MacroExecutionMode";
  48.                 loadProps[2].Value = new Short(com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE_NO_WARN);
  49.  
  50.                 try {
  51.                     XComponent xComponent = xComponentLoader.loadComponentFromURL("file:///" + inputFile, "_blank", 0, loadProps);              
  52.                     System.out.println("xComponent from " + inputFile);
  53.  
  54.                     String macroName = "Standard.Module1.MYMACRONAME?language=Basic&location=application";
  55.                     Object[] aParams = null;
  56.  
  57.                     XScriptProviderSupplier xScriptPS = (XScriptProviderSupplier) UnoRuntime.queryInterface(XScriptProviderSupplier.class, xComponent);
  58.                     XScriptProvider xScriptProvider = xScriptPS.getScriptProvider();
  59.                     XScript xScript = xScriptProvider.getScript("vnd.sun.star.script:"+macroName);
  60.  
  61.                     short[][] aOutParamIndex = new short[1][1];
  62.                     Object[][] aOutParam = new Object[1][1];
  63.  
  64.                     @SuppressWarnings("unused")
  65.                     Object result = xScript.invoke(aParams, aOutParamIndex, aOutParam);
  66.                     System.out.println("xScript invoke macro" + macroName);
  67.  
  68.                     XStorable xStore = (XStorable)UnoRuntime.queryInterface(XStorable.class, xComponent);
  69.                     System.out.println("xStore");
  70.  
  71.                     if (outputFileType.equalsIgnoreCase("pdf")) {
  72.                         System.out.println("writer_pdf_Export");
  73.                         loadProps[0].Name = "FilterName";
  74.                         loadProps[0].Value = "writer_pdf_Export";
  75.                     }
  76.                     xStore.storeToURL("file:///" + outputFile, loadProps);
  77.                     System.out.println("storeToURL to file " + outputFile);
  78.  
  79.                     xComponent.dispose();
  80.  
  81.                     xComponentLoader = null;
  82.                     rInitialObject = null;
  83.  
  84.                     System.out.println("done.");
  85.  
  86.                     System.exit(0);
  87.  
  88.                 } catch(IllegalArgumentException e) {
  89.                     System.err.println("Error: Can't load component from url " + inputFile);
  90.                 }
  91.             } else {
  92.                 System.err.println("Error: Unknown initial object name at server side");
  93.             }          
  94.         } catch(NoConnectException e) {
  95.             System.err.println("Error: Server Connection refused: check server is listening...");           }
  96.     } catch(java.lang.Exception e) {
  97.         System.err.println("Error: Java exception:");
  98.         e.printStackTrace();
  99.     }
  100.        
  101. loadProps[1].Name = "Hidden";
  102. loadProps[1].Value = Boolean.FALSE;