日期:2014-05-17  浏览次数:20504 次

openoffice 转化 文档至 pdf 执行失败
帮我看下下面这个程序, 关于 调用openoffice 将文档转化为 pdf的
namespace ConsoleApplication12
{
  class Program
  {
  private static Mutex _openOfficeLock = new Mutex(false, "OpenOfficeMutexLock-MiloradCavic");
  static void Main(string[] args)
  {
  bool obtained = _openOfficeLock.WaitOne(60 * 1000, false);
  try
  {


  if (!obtained)
  {
  throw new System.Exception(string.Format("Request for using OpenOffice wasn't served after {0} seconds. Aborting...", 30));
  }



  string filename =PathConverter("E:/test.doc");

  PropertyValue[] propertyValues = new PropertyValue[1];
  propertyValues[0] = new unoidl.com.sun.star.beans.PropertyValue();
  propertyValues[0].Name = "Hidden";
  propertyValues[0].Value = new uno.Any(true);



  //我们打开并连接一个OOo程序,这需要创建一个XComponentContext对象:
  XComponentContext xContext = uno.util.Bootstrap.bootstrap();

  // 创建 xFactory
  XMultiServiceFactory xFactory = (XMultiServiceFactory)xContext.getServiceManager();


  //创建XComponentLoader对象:
  XComponentLoader xLoader = (XComponentLoader)xFactory.createInstance("com.sun.star.frame.Desktop");
   

  // 准备开始加载文档

  // Preparing properties for loading the document


  // load begin
  XComponent xComponent = xLoader.loadComponentFromURL(filename, "_blank", 0, propertyValues);


  //Wait for loading
  while (xComponent == null)
  {
  System.Threading.Thread.Sleep(3000);
  }
  string outputFilePath = PathConverter("E:/test.pdf");
  saveDocument(xComponent, outputFilePath);

  xComponent.dispose();
  }
  catch (System.Exception ex)
  {
  Console.WriteLine(ex.Message);
  }
  finally
  {
  Process[] pt = Process.GetProcessesByName("soffice.bin");
  if (pt != null && pt.Length > 0)
  pt[0].Kill();

  Process[] ps = Process.GetProcessesByName("C:\\Program Files\\OpenOffice.org 3\\program\\soffice.exe");
  if (ps != null && ps.Length > 0)
  ps[0].Kill();
  Console.ReadKey();
  }
   


  }
  private static void saveDocument(XComponent xComponent, string fileName)
  {

  unoidl.com.sun.star.beans.PropertyValue[] propertyValue2 =
  new unoidl.com.sun.star.beans.PropertyValue[2];
  propertyValue2[0] = new unoidl.com.sun.star.beans.PropertyValue();
  propertyValue2[0].Name = "Overwrite";
  propertyValue2[0].Value = new uno.Any(true);
  property