使用C#reflection或perl自动打开并将OpenOffice Impress格式保存为html / jpeg

时间:2021-03-07 16:24:36

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

我想转换OpenOffice Impress Presentation文件并将其转换为HTML或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.

我找到了一些例子,但它们似乎已被打破。我想这样做,无论安装什么版本的OpenOffice并不重要,我不想将任何interop dll与我的应用程序捆绑在一起。因此,我正在寻找一种在C#反射中完成的解决方案,最好是使用Win32-OLE或Perl。

Also, how would you hide the OpenOffice GUI?

另外,您如何隐藏OpenOffice GUI?

2 个解决方案

#1


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

看看这个解决方案。可能需要对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

我发布了一个关于exportformats和你需要传递的字符串的先前解决方案

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;  
      } 

   }

#2


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

使用OpenOffice :: OODoc,它了解OpenOffice文档的XML格式,不需要运行openoffice二进制文件,甚至不需要安装openoffice。

#1


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

看看这个解决方案。可能需要对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

我发布了一个关于exportformats和你需要传递的字符串的先前解决方案

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;  
      } 

   }

#2


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

使用OpenOffice :: OODoc,它了解OpenOffice文档的XML格式,不需要运行openoffice二进制文件,甚至不需要安装openoffice。