Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

Converting Microsoft Word Document to PDF format using OpenOffice.org (Portable)

3.67/5 (18 votes)
6 Oct 2006CPOL5 min read 4   7.3K  
An article on using OpenOffice.org Portable to convert a Word document to PDF without requiring you to install OpenOffice.org on client machines.

Convert Microsoft Word Documents to PDF using OpenOffice.org

Contents

Introduction

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.

Requirements

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.

The 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

OpenOffice.org main window

Click "Organizer..." and then create a new library within the tab Libraries, name it "ConversionLibrary"

Create the new library

Change to the Modules tab, select "ConversionLibrary", and create a new module called "PDFConversion"

create the new module

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:

VB
REM  *****  BASIC  *****
Sub ConvertWordToPDF( cSourceFile , cDestinationFile)
   cURL = ConvertToURL( cSourceFile )
   ' Open the document.
   ' Just blindly assume that the document
   ' is of a type that OOo will
   '  correctly recognize and open -- 
   '   without specifying an import filter.

   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
          Array(MakePropertyValue( "Hidden", True ),) )

   cURL = ConvertToURL( cDestinationFile )
   
   ' Save the document using a filter.   
   oDoc.storeToURL( cURL, _
     Array(MakePropertyValue( "FilterName", "writer_pdf_Export" ),)
   
   oDoc.close( True )
End Sub


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
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function 

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).

Note

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.

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="PortableOpenOfficeDirectory" 
             value="OpenOfficePortable\"></add>
        <add key="PortableOpenOfficeExecutable" 
             value="OpenOfficePortable.exe"></add>
        <add key="PortableOpenOfficeConversionLibrary" 
             value="ConversionLibrary"></add>
        <add key="PortableOpenOfficeConversionModule" 
             value="PDFConversion"></add>
        <add key="PortableOpenOfficeConversionFunction" 
             value="ConvertWordToPDF"></add>
    </appSettings>
</configuration>

Hiding OpenOffice.org Portable splash screen

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=false to DisableSplashScreen=true. I've also changed the value of WaitForOpenOffice=false to WaitForOpenOffice=true. Read the documentation of OpenOffice.org Portable for more options.

Copy and modify the configuration file

[OpenOfficePortable]
OpenOfficeDirectory=App\openoffice
SettingsDirectory=Data\settings
OpenOfficeExecutable=soffice.exe
AdditionalParameters=
WaitForOpenOffice=true
RunDataLocally=false
DisableSplashScreen=true

Warning

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:

C#
//using PortableOpenOffice;//uncomment this if you added a reference to the dll
OpenOffice.Instance.ConvertToPDF(SourceFileName,DestinationFileName);

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.

Conclusion

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).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)