views:

974

answers:

2

I want to convert an OpenOffice Impress Presentation file and convert it to HTML or JPEG.

I have found a few examples, but they appear to be broken. I would like to do it, in a way that it does not matter what version of OpenOffice is installed, and I do not want to bundle any interop dlls with my application. Therefore, I am looking for a solution that is done in C# reflection, preferably, or Perl using Win32-OLE.

Also, how would you hide the OpenOffice GUI?

+2  A: 

Use OpenOffice::OODoc, it understands the XML format of OpenOffice documents, requires no openoffice binaries to be running or even to have openoffice installed.

MkV
+1  A: 

Check out this solution . There might need some changes on the declaration of the PropertyValues

public void Conversion(string sourcefile, string exportfile)
{
       Type tServiceManager = Type.GetTypeFromProgID("com.sun.star.ServiceManager", true);
       object oServiceManager = System.Activator.CreateInstance(tServiceManager);
       object oDesktop = Invoke(oServiceManager,"createinstance",BindingFlags.InvokeMethod,"com.sun.star.frame.Desktop");
       //Load Document
       Object[] arg = new Object[4];
       arg[0] = PathConverter(sourcefile);  // or "private:factory/swriter"  for a blank Document
       arg[1] = "_blank";
       arg[2] = 0;
       object loadproperty1 = CreatePropertyValue("Hidden", true);  // Executes the OpenOffice without UI
       arg[3] = new Object[] { loadproperty1};
       object oComponent = Invoke(oDesktop,"loadComponentFromUrl",BindingFlags.InvokeMethod,arg);

       //Create an array for the storeToUrl method  
       arg = new Object[2];
       arg[0] = PathConverter(exportfile);  
       object storeproperty1 = CreatePropertyValue("Overwrite", true);  // Overrites if file exits and prevents errors
       object storeproperty2 = CreatePropertyValue("FilterName", "HTML (StarWriter)");  // Export to HTML
       arg[1] = new Object[] { storeproperty1,storeproperty2 };
       Invoke(oComponent,"storeToUrl",BindingFlags.InvokeMethod,arg);
}

I published a previous solution regarding the exportformats and the string you need to pass

Helper Methods:

private static object CreatePropertyValue(object serviceManager,string name, object value)
    {
        object propertyvalue = Invoke(serviceManager, "Bridge_GetStruct", BindingFlags.CreateInstance|BindingFlags.InvokeMethod|BindingFlags.GetProperty,
                                 "com.sun.star.beans.PropertyValue");
        Invoke(propertyvalue, "Name", BindingFlags.SetProperty, name);
        Invoke(propertyvalue, "Value", BindingFlags.SetProperty, value);
        return propertyvalue;
    }    

        private static object Invoke(object obj, string method, BindingFlags binding, params object[] par)
        {
            return obj.GetType().InvokeMember(method, binding, null, obj, par);
        } 

    /// Convert into OO file format  
    /// The file.   
    /// The converted file  

    private static string PathConverter( string file)  
    { 
       try  
      { 
         file = file.Replace(@"\", "/"); 
         return "file:///"+file;  
      } 
       catch (System.Exception ex)  
      { 

          throw ex;  
      } 

   }
jmayor