I'm new to Open office and having a trouble in converting ODT file to DOCX using C#. Please apologise if my question sounds kiddish or its a waste of time.
Below is the code i'm using.
Code: Select all
unoidl.com.sun.star.uno.XComponentContext xLocalContext = uno.util.Bootstrap.bootstrap();
unoidl.com.sun.star.lang.XMultiServiceFactory xRemoteFactory =
(unoidl.com.sun.star.lang.XMultiServiceFactory)xLocalContext.getServiceManager();
XComponentLoader aLoader = (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");
XComponent xComponent = initDocument(aLoader, PathConverter(strFilePathODT), "_blank");
saveDocument(xComponent, strFilePathODT, PathConverter(strFilePathDOCX));
InitDocument
------------------
private static XComponent initDocument(XComponentLoader aLoader, string file, string target)
{
try
{
PropertyValue[] openProps = new PropertyValue[1];
openProps[0] = new PropertyValue();
openProps[0].Name = "Hidden";
openProps[0].Value = new uno.Any(true);
XComponent xComponent = aLoader.loadComponentFromURL(file, target, 0, openProps);
return xComponent;
}
catch (Exception e)
{
throw e;
}
finally
{
aLoader = null;
file = null;
target = null;
}
}
Save Document
-------------------
private static void saveDocument(XComponent xComponent, string sourceFile, string destinationFile)
{
try
{
PropertyValue[] propertyValues = new PropertyValue[1];
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Overwrite";
propertyValues[0].Value = new uno.Any("MS Word 2007");
((XStorable)xComponent).storeToURL(destinationFile, propertyValues);
}
catch (Exception e)
{
throw e;
}
finally
{
xComponent = null;
sourceFile = null;
destinationFile = null;
}
}
Path Converter
--------------------
private static string PathConverter(string file)
{
try
{
if (file == null || file.Length == 0)
throw new NullReferenceException("Null or empty path passed to OpenOffice");
return String.Format("file:///{0}", file.Replace(@"\", "/"));
}
finally
{
file = null;
}
}
Please throw some light....
Many Thanks,
CodeMad