/// <summary> /// Returns all services of the object. /// </summary> /// <param name="obj">The obj.</param> /// <param name="debug">if set to <c>true</c> the services will be printed /// to the System.Diagnostics.Debug output.</param> /// <returns>List of all supported service names.</returns> /// <remarks>This function is time limited to 200 ms.</remarks> public static string[] GetAllServicesOfObject(Object obj, bool debug = true) { String output = ""; string[] services = new string[0]; if (obj != null) { try { TimeLimitExecutor.WaitForExecuteWithTimeLimit(200, () => { XServiceInfo si = obj as XServiceInfo; if (si != null) { // FIXME: Access violation on service request try { services = si.getSupportedServiceNames(); if (debug) { output += (services.Length + "\tServices:"); foreach (var item in services) { output += "\n" + ("\tService: " + item); } System.Diagnostics.Debug.WriteLine(output); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception happened: " + ex.ToString()); } } else { System.Diagnostics.Debug.WriteLine("Can't get services of Object because it is not a XServiceInfo provider."); } }, "GetAllServices"); } catch (unoidl.com.sun.star.lang.DisposedException) { OO.CheckConnection(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Can't get services of Object: " + e); } } return(services); }
/// <summary> /// It try to export given document in HTML format. /// Current document will be converted to HTML and moved to new place on disk. /// A "new" file will be created by given URL (may be overwritten /// if it already exist). Right filter will be used automatically if factory of /// this document support it. If no valid filter can be found for export, /// nothing will be done here. /// </summary> /// <param name="xDocument">document which should be exported</param> /// <param name="sUrl">target URL for converted document</param> public static void SaveAsHtml(XComponent xDocument, String sUrl) { try { // First detect factory of this document. // Ask for the supported service name of this document. // If information is available it can be used to find out which // filter exist for HTML export. Normally this filter should be searched // inside the filter configuration but this little demo doesn't do so. // (see service com.sun.star.document.FilterFactory for further // informations too) // Well known filter names are used directly. They must exist in current // office installation. Otherwise this code will fail. But to prevent // this code against missing filters it check for existing state of it. XServiceInfo xInfo = (XServiceInfo)xDocument; if (xInfo != null) { // Find out possible filter name. String sFilter = null; if (xInfo.supportsService(OO.Services.DOCUMENT_TEXT)) { sFilter = "HTML (StarWriter)"; } else if (xInfo.supportsService(OO.Services.DOCUMENT_WEB)) { sFilter = "HTML"; } else if (xInfo.supportsService(OO.Services.DOCUMENT_SPREADSHEET)) { sFilter = "HTML (StarCalc)"; } // Check for existing state of this filter. if (sFilter != null) { XComponentContext xCtx = OO.GetContext(); var xFilterContainer = (XNameAccess)xCtx.getServiceManager().createInstanceWithContext( OO.Services.DOCUMENT_FILTER_FACTORY, xCtx); if (xFilterContainer.hasByName(sFilter) == false) { sFilter = null; } } // Use this filter for export. if (sFilter != null) { // Export can be forced by saving the document and using a // special filter name which can write needed format. Build // necessary argument list now. // Use special flag "Overwrite" too, to prevent operation // against possible exceptions, if file already exist. var lProperties = new PropertyValue[2]; lProperties[0] = new PropertyValue { Name = "FilterName", Value = Any.Get(sFilter) }; lProperties[1] = new PropertyValue { Name = "Overwrite", Value = Any.Get(true) }; XStorable xStore = (XStorable)xDocument; xStore.storeAsURL(sUrl, lProperties); } } } catch (unoidl.com.sun.star.io.IOException exIo) { // Can be thrown by "store()" call. // Do nothing then. Saving failed - that's it. System.Diagnostics.Debug.WriteLine(exIo); } catch (RuntimeException exRuntime) { // Can be thrown by any uno call. // Do nothing here. Saving failed - that's it. System.Diagnostics.Debug.WriteLine(exRuntime); } catch (unoidl.com.sun.star.uno.Exception exUno) { // Can be thrown by "createInstance()" call of service manager. // Do nothing here. Saving failed - that's it. System.Diagnostics.Debug.WriteLine(exUno); } }
static void Main(string[] args) { InitOpenOfficeEnvironment(); XMultiServiceFactory multiServiceFactory = Connect(); XComponent xComponent = null; string docFileName = @"C:\test3.doc"; try { XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory .createInstance("com.sun.star.frame.Desktop"); //set the property PropertyValue[] propertyValue = new PropertyValue[1]; PropertyValue aProperty = new PropertyValue(); aProperty.Name = "Hidden"; aProperty.Value = new uno.Any(false); propertyValue[0] = aProperty; xComponent = componentLoader .loadComponentFromURL(PathConverter(docFileName), "_blank", 0, propertyValue); XTextDocument xTextDocument = ((XTextDocument)xComponent); XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)xTextDocument.getText(); XEnumeration xParagraphEnumeration = xEnumerationAccess.createEnumeration(); while (xParagraphEnumeration.hasMoreElements()) { uno.Any element = xParagraphEnumeration.nextElement(); XServiceInfo xinfo = (XServiceInfo)element.Value; if (xinfo.supportsService("com.sun.star.text.TextTable")) { Console.WriteLine("Found Table!"); XTextTable xTextTable = (XTextTable)element.Value; String[] cellNames = xTextTable.getCellNames(); for (int i = 0; i < cellNames.Length; i++) { XCell xCell = xTextTable.getCellByName(cellNames[i]); XText xTextCell = (XText)xCell; String strText = xTextCell.getString(); Console.WriteLine(strText); } } else { XTextContent xTextElement = (XTextContent)element.Value; XEnumerationAccess xParaEnumerationAccess = (XEnumerationAccess)xTextElement; // create another enumeration to get all text portions of //the paragraph XEnumeration xTextPortionEnum = xParaEnumerationAccess.createEnumeration(); //step 3 Through the Text portions Enumeration, get interface //to each individual text portion while (xTextPortionEnum.hasMoreElements()) { XTextRange xTextPortion = (XTextRange)xTextPortionEnum.nextElement().Value; Console.Write(xTextPortion.getString()); } } } } catch (unoidl.com.sun.star.uno.Exception exp1) { Console.WriteLine(exp1.Message); Console.WriteLine(exp1.StackTrace); } catch (System.Exception exp2) { Console.WriteLine(exp2.Message); Console.WriteLine(exp2.StackTrace); } finally { xComponent.dispose(); xComponent = null; } Console.WriteLine("Done."); Console.ReadLine(); }