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(); } } }
详细解决方案
Openoffice 安插html图片 并保存在文档中
热度:1133 发布时间:2012-09-19 13:43:53.0
相关解决方案
- 访问Tomcat的url的时候如何自动调用index.html
- The requested resource (/webtest/servlet/hello.html) is not availabl 帮忙解决解决方法
- JSP 页面乱码 页面起首已设置 contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
- html js不运行有关问题
- 办公自动化系统——议程管理(用jsp+servlet+js+html+jdbc)怎样实现
- <html:text>property的有关问题
- 关于<html:text>相关的有关问题
- Struts 1.2 html:form的action和form的action区别,还有如果2个都有执行哪个解决方案
- 简单计算器(html+js),该怎么解决
- HTML 小疑点,哪位高手解决 给哪位高手分
- 嵌入JSP中的CSS显示效果和在 HTML 中的不一样.为什么.该怎么解决
- struts1 中<html:checkbox>标签的value值怎么动态赋值
- 新人求问,J2EE方向,html,css,javascript,vml要学到什么程度?解决思路
- :前台和后台开发有啥区别?还有css、html、ajax、js、jquery都有什么区别
- struts <html:file> 怎么让前面的框消失,先谢过了
- =Html.TextAreaFor的文本区域大小如何设置
- 正则表达式 提取 html 标签的内容,该如何解决
- @Html.CkEditor,该怎么处理
- string msg = (string)html.ViewData["Message"];该如何处理
- html password editbox 编码有关问题
- @Html.TextBoxFor(a => a.Title 上边有红线,说异常
- 关于伪静态,怎么配置.html
- MVC中,Html.DropDownListFor怎么绑定数据
- 为何FCKeditor会在自动生成<html>标签呢
- MVC 中得 Html.ActionLink 怎么linkText显示图片标记
- <DOCTYPE html. 这个是有什么用?该如何处理
- asp.net C# 将网页变换为.html 格式
- @Html.ActionLink 基础解决办法
- openoffice 转化 文档至 pdf 实施失败
- 怎么引用一个 Html 控件对象