This article explains a way to use OpenOffice.org Portable to convert a Microsoft Word Document to PDF Format. The purpose of using OpenOffice.org Portable is not having to install OpenOffice.org on client machines. The operation is very simple, we just execute OpenOffice.org Portable - as if we were running it from the command line - from our code, with some arguments that indicate it to run a macro which handles the conversion.
To my knowledge, there are two articles that explain how to do this conversion, on CodeProject:
The former is a very complicated way and you have to install MS-Office. You would have this installed anyway if you are working with Word documents though; the second one requires that you have installed OpenOffice.org, which is not that bad but I wanted to avoid the task of installing it by using an XCopy kind of method.
Before running the demo or your application using the code, you need to download and extract OpenOffice.org Portable into the same directory the application resides in. Version 2.0.3 or later should work fine due to a bug that has been fixed, and allows us to run OpenOffice.org Portable hidden. After you have done that, you will need to add the following macro.
Execute OpenOfficePortable.exe inside the OpenOfficePortable\ directory where you extracted the files; the first time you run it, a wizard will show up. Follow the instructions, you don't need to register for the application to work. After you've done that you should see the OpenOffice.org main window.
Choose Tools->Macros->Organize Macros->OpenOffice.org Basic
Click "Organizer..." and then create a new library within the tab Libraries, name it "ConversionLibrary"
Change to the Modules tab, select "ConversionLibrary", and create a new module called "PDFConversion"
Once you've done that, click the Edit button to open the macro editor. The default is to already have a macro called Main. You can replace it or leave it. Paste the following code into the editor, save and close:
Sub ConvertWordToPDF( cSourceFile , cDestinationFile)
cURL = ConvertToURL( cSourceFile )
oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
Array(MakePropertyValue( "Hidden", True ),) )
cURL = ConvertToURL( cDestinationFile )
oDoc.storeToURL( cURL, _
Array(MakePropertyValue( "FilterName", "writer_pdf_Export" ),)
oDoc.close( True )
Function MakePropertyValue( Optional cName As String, _
Optional uValue ) As com.sun.star.beans.PropertyValue
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
MakePropertyValue() = oPropertyValue
The original macro was found in the OpenOffice.org forum.
I modified it to fit my needs, so can you.
Now you should be able to run the demo application or your own application. If you've done this, you probably have noticed an OpenOffice.org Portable splash screen. To disable this behavior, read the Hiding OpenOffice.org Portable splash screen section below. If you used different names for the library, module, macro, directory of
OpenOffice.org Portable or the executable name; you should change those values in the application configuration file (UsingOpenOffice.exe.config in the demo).
If you are using the code in your own application, you must add an application configuration file with the next keys in the
appSettings section, or an exception will be thrown.
To hide the OpenOffice.org Portable splash screen, copy the file OpenOfficePortable.ini from the OpenOfficePortable\Other\OpenOfficePortableSource directory to
OpenOfficePortable\ and edit it changing the value of
DisableSplashScreen=true. I've also changed the value of
WaitForOpenOffice=true. Read the documentation of OpenOffice.org Portable for more options.
There are known issues on using OpenOffice.org Portable, you should read at least this article. If you can run OpenOffice.org Portable and do the conversion manually, chances for the code to run properly are better.
Long Paths and Odd Characters - OpenOffice.org doesn't like running from overly long paths. So, while it will work directly from your desktop... if you have it within a subdirectoy on your desktop, it will probably fail claiming that some files are corrupted. OpenOffice.org also doesn't like certain characters like dollar signs in the path. This is why it's best to have the OpenOfficePortable directory in the root of your drive, or within a PortableApps directory in the root of your drive. The exact length is being determined and the next release of the launcher will check for this.
So, try to locate your application or the demo in a short path so that the OpenOffice.org Portable path is for example: c:\Program Files\Your Company\Your Program\OpenOfficePortable will probably work, but if you try something like: C:\Documents and Settings\Your User\My Documents\Visual Studio Projects\Your Project\bin\Debug\OpenOfficePortable will probably fail.
Using the code
After you've done everything that is required - downloaded OpenOffice.org Portable and extracted it into the same folder of the application, added the macro and configured the application and the OpenOffice.org Portable configuration files if needed - you may use the code simply by adding the file OpenOffice.cs to your project or adding a reference to the DLL and then calling it in the following manner:
Points of Interest
Even though this article explains how to use OpenOffice.org Portable to convert a Microsoft Word Document to PDF Format, it could be used to do many other conversions. You only need to modify the macro used in this example. Even without modifying the macro, you could do conversions from any OpenOffice.org supported format to PDF because we relied - not specifying any import filter in the macro - that the document would be recognized and opened by the OpenOffice.org.
This document presented a very simple way to convert Microsoft Word Document to PDF format. All the real work is done by OpenOffice.org so be sure to visit their site and read the license on their software. You should also visit this website which has done a great work on doing many applications portable (for us not having to install software).