读书人

openoffice 转化 文档至 pdf 实施失败

发布时间: 2012-09-20 09:36:50 作者: rapoo

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);
propertyValue2[1] = new unoidl.com.sun.star.beans.PropertyValue();
propertyValue2[1].Name = "FilterName";
propertyValue2[1].Value = new uno.Any("writer_pdf_Export");



((XStorable)xComponent).storeToURL(fileName, propertyValue2);
}

private static string PathConverter(string file)
{
try
{
file = file.Replace(@"\", "/");

return "file:///" + file;
}
catch (System.Exception ex)
{
throw ex;
}
}

}
}

红色部分执行不过去呢!

[解决办法]
可能是FilePath转换成file:///....时的问题,代码是没错的。。提示什么错误呢。。

对于Office用WPS转换好点,我见过最完美的是Adobe的AcrobatPro,但是收费。。。

读书人网 >asp.net

热点推荐