📄 openofficeworker.java
字号:
/* A desktop environment contains tasks with one or more frames in which components can be loaded. Desktop is the environment for components which can instanciate within frames. */ Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext); //XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj); XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj); // Preparing properties for loading the document PropertyValue propertyvalue[] = new PropertyValue[ 2 ]; // Setting the flag for hidding the open document propertyvalue[ 0 ] = new PropertyValue(); propertyvalue[ 0 ].Name = "Hidden"; propertyvalue[ 0 ].Value = new Boolean(true); propertyvalue[ 1 ] = new PropertyValue(); propertyvalue[ 1 ].Name = "UpdateDocMode"; propertyvalue[ 1 ].Value = "1"; // Loading the wanted document Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(stringUrl, "_blank", 0, propertyvalue); // Getting an object that will offer a simple way to store a document to a URL. XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore); // Preparing properties for converting the document propertyvalue = new PropertyValue[ 3 ]; // Setting the flag for overwriting propertyvalue[ 0 ] = new PropertyValue(); propertyvalue[ 0 ].Name = "Overwrite"; propertyvalue[ 0 ].Value = new Boolean(true); // Setting the filter name // Preparing properties for converting the document String filterName = getFilterNameFromMimeType(outputMimeType); propertyvalue[ 1 ] = new PropertyValue(); propertyvalue[ 1 ].Name = "FilterName"; propertyvalue[ 1 ].Value = filterName; propertyvalue[2] = new PropertyValue(); propertyvalue[2].Name = "CompressionMode"; propertyvalue[2].Value = "1"; Debug.logInfo("stringConvertedFile: "+stringConvertedFile, module); // Storing and converting the document //File newFile = new File(stringConvertedFile); //newFile.createNewFile(); xstorable.storeToURL(stringConvertedFile, propertyvalue); // Getting the method dispose() for closing the document XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xstorable); // Closing the converted document xcomponent.dispose(); return; } public static OpenOfficeByteArrayOutputStream convertOODocByteStreamToByteStream(XMultiComponentFactory xmulticomponentfactory, OpenOfficeByteArrayInputStream is, String inputMimeType, String outputMimeType) throws Exception { // Query for the XPropertySet interface. XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory); // Get the default context from the office server. Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext"); // Query for the interface XComponentContext. XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext); /* A desktop environment contains tasks with one or more frames in which components can be loaded. Desktop is the environment for components which can instanciate within frames. */ Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext); //XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj); XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj); // Preparing properties for loading the document PropertyValue propertyvalue[] = new PropertyValue[2]; // Setting the flag for hidding the open document propertyvalue[0] = new PropertyValue(); propertyvalue[0].Name = "Hidden"; propertyvalue[0].Value = Boolean.TRUE; // propertyvalue[1] = new PropertyValue(); propertyvalue[1].Name = "InputStream"; propertyvalue[1].Value = is; // Loading the wanted document Object objectDocumentToStore = xcomponentloader.loadComponentFromURL("private:stream", "_blank", 0, propertyvalue); if (objectDocumentToStore == null) { Debug.logError("Could not get objectDocumentToStore object from xcomponentloader.loadComponentFromURL", module); } // Getting an object that will offer a simple way to store a document to a URL. XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore); if (xstorable == null) { Debug.logError("Could not get XStorable object from UnoRuntime.queryInterface", module); } // Preparing properties for converting the document String filterName = getFilterNameFromMimeType(outputMimeType); propertyvalue = new PropertyValue[4]; propertyvalue[0] = new PropertyValue(); propertyvalue[0].Name = "OutputStream"; OpenOfficeByteArrayOutputStream os = new OpenOfficeByteArrayOutputStream(); propertyvalue[0].Value = os; // Setting the filter name propertyvalue[1] = new PropertyValue(); propertyvalue[1].Name = "FilterName"; propertyvalue[1].Value = filterName; // Setting the flag for overwriting propertyvalue[3] = new PropertyValue(); propertyvalue[3].Name = "Overwrite"; propertyvalue[3].Value = Boolean.TRUE; // For PDFs propertyvalue[2] = new PropertyValue(); propertyvalue[2].Name = "CompressionMode"; propertyvalue[2].Value = "1"; xstorable.storeToURL("private:stream", propertyvalue); //xstorable.storeToURL("file:///home/byersa/testdoc1_file.pdf", propertyvalue); // Getting the method dispose() for closing the document XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xstorable); // Closing the converted document xcomponent.dispose(); return os; } public static String getFilterNameFromMimeType(String mimeType) { String filterName = ""; if (UtilValidate.isEmpty(mimeType)) { filterName = "HTML"; } else if (mimeType.equalsIgnoreCase("application/pdf")) { filterName = "writer_pdf_Export"; } else if (mimeType.equalsIgnoreCase("application/msword")) { filterName = "MS Word 97"; } else if (mimeType.equalsIgnoreCase("text/html")) { filterName = "HTML (StarWriter)"; } else { filterName = "HTML"; } return filterName; } public static String getExtensionFromMimeType(String mimeType) { String extension = ""; if (UtilValidate.isEmpty(mimeType)) { extension = "html"; } else if (mimeType.equalsIgnoreCase("application/pdf")) { extension = "pdf"; } else if (mimeType.equalsIgnoreCase("application/msword")) { extension = "doc"; } else if (mimeType.equalsIgnoreCase("text/html")) { extension = "html"; } else { extension = "html"; } return extension; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -