1 package org.lucane.extensions.pdfbox; 2 3 import java.io.IOException ; 4 import java.net.URL ; 5 6 import com.sun.star.beans.PropertyValue; 7 import com.sun.star.bridge.XUnoUrlResolver; 8 import com.sun.star.comp.helper.Bootstrap; 9 import com.sun.star.frame.XComponentLoader; 10 import com.sun.star.frame.XStorable; 11 import com.sun.star.lang.XComponent; 12 import com.sun.star.lang.XMultiComponentFactory; 13 import com.sun.star.uno.UnoRuntime; 14 import com.sun.star.uno.XComponentContext; 15 16 19 public class OooToPdf 20 { 21 private static final String [] PDF_FILTERS = { 23 "writer_pdf_Export", 24 "writer_web_pdf_Export", 25 "writer_globaldocument_pdf_Export", 26 "calc_pdf_Export", 27 "impress_pdf_Export", 28 "draw_pdf_Export", 29 "math_pdf_Export"}; 30 31 private static OooToPdf instance = null; 34 private static OooProcess process = null; 35 public static void initInstance(String oooCommand) 36 throws Exception 37 { 38 if(OooToPdf.instance == null) 39 { 40 if(OooToPdf.process == null) 42 { 43 OooToPdf.process = new OooProcess(oooCommand); 44 new Thread (OooToPdf.process).start(); 45 } 46 } 47 } 48 49 public synchronized static OooToPdf getInstance() 50 throws Exception 51 { 52 if(OooToPdf.instance != null) 54 { 55 try { 56 XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface( 57 XComponentLoader.class, 58 OooToPdf.instance.desktop); 59 } catch(Exception e) { 60 OooToPdf.instance = null; 61 } 62 } 63 64 65 if(OooToPdf.instance == null) 66 { 67 OooToPdf.instance = new OooToPdf(); 69 OooToPdf.instance.initComponentContext(); 70 OooToPdf.instance.initDesktop(); 71 } 72 73 return OooToPdf.instance; 74 } 75 76 public static void shutdown() 77 { 78 OooToPdf.process.shutdown(); 79 } 80 81 private XComponentContext context; 83 private Object desktop; 84 85 87 90 private void initComponentContext() 91 throws Exception 92 { 93 XComponentContext localContext = Bootstrap.createInitialComponentContext(null); 94 XMultiComponentFactory localServiceManager = localContext.getServiceManager(); 95 Object urlResolver = localServiceManager.createInstanceWithContext( 96 "com.sun.star.bridge.UnoUrlResolver", 97 localContext); 98 XUnoUrlResolver unoUrlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface( 99 XUnoUrlResolver.class, 100 urlResolver); 101 Object initialObject = unoUrlResolver.resolve( 102 "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext"); 103 104 this.context = (XComponentContext) UnoRuntime.queryInterface( 105 XComponentContext.class, 106 initialObject); 107 } 108 109 112 private void initDesktop() 113 throws Exception 114 { 115 XMultiComponentFactory serviceManager = context.getServiceManager(); 116 this.desktop = serviceManager.createInstanceWithContext( 117 "com.sun.star.frame.Desktop", 118 context); 119 } 120 121 123 public void convertFile(URL from, URL to) 124 throws Exception 125 { 126 XComponent document = loadDocument(from); 127 this.storeDocument(document, to); 128 document.dispose(); 129 } 130 131 private XComponent loadDocument(URL from) 132 throws Exception 133 { 134 XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface( 136 XComponentLoader.class, 137 desktop); 138 XComponent document = (XComponent)componentLoader.loadComponentFromURL( 139 from.toExternalForm(), 140 "_blank", 141 0, 142 new PropertyValue[] {} 143 ); 144 145 if(document == null) 146 throw new IOException ("Unable to load : " + from); 147 148 return document; 149 } 150 151 private void storeDocument(XComponent document, URL to) 152 { 153 XStorable storable = (XStorable)UnoRuntime.queryInterface( 155 XStorable.class, 156 document); 157 158 for(int i=0;i<PDF_FILTERS.length;i++) 160 storeDocumentWithFilter(storable, PDF_FILTERS[i], to); 161 } 162 163 private void storeDocumentWithFilter(XStorable storable, String filter, URL to) 164 { 165 PropertyValue p = new PropertyValue(); 166 p.Name = "FilterName"; 167 p.Value = filter; 168 169 try { 170 storable.storeToURL(to.toExternalForm(), new PropertyValue[]{p}); 171 } catch(com.sun.star.io.IOException ioe) { 172 } 174 } 175 } | Popular Tags |