import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import ooo.connector.BootstrapSocketConnector; import com.sun.star.awt.Size; import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertySet; import com.sun.star.container.XNameAccess; import com.sun.star.document.XDocumentInsertable; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.graphic.XGraphicProvider; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.text.XSentenceCursor; import com.sun.star.text.XText; import com.sun.star.text.XTextCursor; import com.sun.star.text.XTextDocument; import com.sun.star.text.XTextGraphicObjectsSupplier; import com.sun.star.uno.Exception; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class Export{ private XComponentContext mxRemoteContext; private XMultiComponentFactory mxRemoteServiceManager; private XTextCursor mxDocCursor; private XText mxDocText; private XTextDocument mxDoc; private XSentenceCursor xSentenceCursor; private XPropertySet propertySet; private Object desktop; private XComponent xEmptyWriterComponent; /** * get the remote service manager * * @return * @throws java.lang.Exception */ private XMultiComponentFactory getRemoteServiceManager() throws java.lang.Exception { if (mxRemoteContext == null && mxRemoteServiceManager == null) { // get the remote office context String oooExeFolder = "D:/Program Files/OpenOffice.org 3/program"; mxRemoteContext = BootstrapSocketConnector.bootstrap(oooExeFolder); System.out.println("Connected to a running office ..."); mxRemoteServiceManager = mxRemoteContext.getServiceManager(); } return mxRemoteServiceManager; } /** * get the interfaces to control the UNO * * @param docType * @return * @throws java.lang.Exception */ private XComponent newDocComponent(String docType) throws java.lang.Exception { String loadUrl = "private:factory/" + docType; mxRemoteServiceManager = this.getRemoteServiceManager(); // get the Desktop service desktop = mxRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", mxRemoteContext); // retrieve the current component and access the controller XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime .queryInterface(XComponentLoader.class, desktop); PropertyValue[] propertyValue = new PropertyValue[1]; propertyValue[0] = new com.sun.star.beans.PropertyValue(); propertyValue[0].Name = "Hidden";// set the OpenOffice not open propertyValue[0].Value = Boolean.TRUE; return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, propertyValue); } /** * editing the export paper * @throws java.lang.Exception */ public void editing() throws java.lang.Exception{ xEmptyWriterComponent = newDocComponent("swriter"); mxDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xEmptyWriterComponent); mxDocText = mxDoc.getText(); // the controller gives us the TextViewCursor mxDocCursor = mxDocText.createTextCursor(); xSentenceCursor = (XSentenceCursor) UnoRuntime.queryInterface( XSentenceCursor.class, mxDocCursor); // query its XPropertySet interface, we want to set character and // paragraph // properties propertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, mxDocCursor); insertHtml(); embedImagesInWriter(xEmptyWriterComponent); String url = "file:///c:/test.doc"; storeDocComponent(xEmptyWriterComponent, url); close(); } private void insertHtml() throws Exception, IOException { String html = "<img src=\"http://img03.taobaocdn.com/tps/i3/T1eG6FXf4kXXcd9aAM-440-135.png\"/><img src=\"http://img03.taobaocdn.com/tps/i3/T1eG6FXf4kXXcd9aAM-440-135.png\"/><img src=\"http://img03.taobaocdn.com/tps/i3/T1eG6FXf4kXXcd9aAM-440-135.png\"/>"; File textFile = createHtmlTempFile(html); //now insert that file as HTML into the location XDocumentInsertable docInsertable = (XDocumentInsertable) UnoRuntime.queryInterface(XDocumentInsertable.class, mxDocCursor); docInsertable.insertDocumentFromURL("file:///c:/temp.html", new PropertyValue[0] ); } private File createHtmlTempFile(String content) throws Exception, IOException { //temp files into working directory File temp = new File("c:/temp.html"); //open file and write HTML content FileOutputStream fs = new FileOutputStream(temp); PrintStream ps = new PrintStream(fs); ps.print( content ); ps.flush(); ps.close(); fs.close(); return temp; } private void storeDocComponent(XComponent xDoc, String storeUrl) throws java.lang.Exception { XStorable xStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, xDoc); PropertyValue[] storeProps = new PropertyValue[2]; storeProps[0] = new PropertyValue(); storeProps[0].Name = "Overwrite"; storeProps[0].Value = Boolean.TRUE; storeProps[1] = new PropertyValue(); storeProps[1].Name = "FilterName"; storeProps[1].Value = "MS Word 97"; System.out.println(storeUrl); xStorable.storeAsURL(storeUrl, storeProps); } private void close() throws Exception { com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable) UnoRuntime .queryInterface(com.sun.star.util.XCloseable.class, mxDoc); if (xCloseable != null) { xCloseable.close(false); } else { com.sun.star.lang.XComponent xComponent = (com.sun.star.lang.XComponent) UnoRuntime .queryInterface(com.sun.star.lang.XComponent.class, mxDoc); xComponent.dispose(); } } private final void embedImagesInWriter(XComponent oDoc) throws Exception { XTextGraphicObjectsSupplier XTxtGraphObjSupplier = (XTextGraphicObjectsSupplier) UnoRuntime.queryInterface(XTextGraphicObjectsSupplier.class, oDoc); XNameAccess XNameAcc; XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); Object oGraphic=null; // XComponentContext xComponentContext = openOfficeConnection.getComponentContext(); XMultiComponentFactory xMCF = mxRemoteContext.getServiceManager(); Object graphicProviderObject = null; graphicProviderObject = xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", mxRemoteContext); XGraphicProvider XGraphProv = (XGraphicProvider) UnoRuntime.queryInterface(XGraphicProvider.class, graphicProviderObject); oGraphic = xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject"); String[] allImages = null; int x = 0; PropertyValue[] aMediaProperties = new PropertyValue[1]; XNameAcc = XTxtGraphObjSupplier.getGraphicObjects(); allImages = XNameAcc.getElementNames(); for (x = 0; x < allImages.length; x++) { oGraphic = XNameAcc.getByName(allImages[x]); XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, oGraphic); aMediaProperties = MakePropertyValue("URL", xPropSet.getPropertyValue("GraphicURL").toString()); xPropSet.getPropertyValue("IsPixelContour"); System.out.println(xPropSet.getPropertyValue("GraphicURL").toString()); System.out.println(((Size)xPropSet.getPropertyValue("ActualSize")).Height); xPropSet.setPropertyValue("Graphic", XGraphProv.queryGraphic(aMediaProperties)); } } private final PropertyValue[] MakePropertyValue(String cName, Object uValue) { PropertyValue[] tempMakePropertyValue = new PropertyValue[1]; tempMakePropertyValue[0] = new PropertyValue(); tempMakePropertyValue[0].Name = cName; tempMakePropertyValue[0].Value= uValue; return tempMakePropertyValue; } public static void main(String[] args) { Export export = new Export(); try { export.editing(); } catch (java.lang.Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }