How to Convert ODT to DOCX using C#

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
CodeMad
Posts: 3
Joined: Tue Feb 23, 2010 2:37 pm

How to Convert ODT to DOCX using C#

Post by CodeMad »

Folks,

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

        }
I could see my docx created under a specified folder but unable to open it as it says the file is corrupted. Please can someone tell me what is really wrong with the code? Moreover the package contents are normally different from an ODT and DOCX. When i rename the converted docx file to .zip i'm seeing the same content as .odt. So it tells me the conversion hasn't really happened.

Please throw some light....

Many Thanks,
CodeMad
OpenOffice 2.1 on WinXPPro SP3
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: How to Convert ODT to DOCX using C#

Post by RoryOF »

It is questionable how good the conversion will be. I doubt it will be accurate enough to be worth the effort.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
CodeMad
Posts: 3
Joined: Tue Feb 23, 2010 2:37 pm

Re: How to Convert ODT to DOCX using C#

Post by CodeMad »

RoryOF wrote:It is questionable how good the conversion will be. I doubt it will be accurate enough to be worth the effort.
Hi RoryOF,

Thanks for your reply. Could do the conversion when i open the odt in Open Office and save in MS Word format manually.
Now i'm a situation where i need to check the possibility of programing it :(

Many Thanks,
CodeMad
OpenOffice 2.1 on WinXPPro SP3
CodeMad
Posts: 3
Joined: Tue Feb 23, 2010 2:37 pm

Re: How to Convert ODT to DOCX using C#

Post by CodeMad »

Just to add-up to the question.
Now we have a system that merges 2 ODT file and creates a final DOCX file. While merging, the file is getting opened in Open Office and SaveAs DOCX is happening. This ultimately opens and instance of Open Office. But my requirement is to merge these 2 ODTs to arrive at a final DOCX without opening Open office or MS word instance.
OpenOffice 2.1 on WinXPPro SP3
fst
Volunteer
Posts: 152
Joined: Wed Nov 28, 2007 2:31 pm

Re: How to Convert ODT to DOCX using C#

Post by fst »

Hi,

are you sure you can save to DOCX ?

At least the version from Openoffice.org can't save to docx as far as I know.

Frank
Frank

If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Post Reply