Click here to Skip to main content
Click here to Skip to main content

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

By , 6 Oct 2006
Rate this:
Please Sign up or sign in to vote.

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:

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 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:

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

About the Author

cirovladimir
Web Developer
Mexico Mexico
No Biography provided

Comments and Discussions

 
QuestionDll OpenOffice PinmemberMember 1068396524-Mar-14 7:09 
Questionvery nice work PinmemberMember 391328327-Feb-14 2:47 
QuestionOpen Office Size Reduction Pinmemberrenegade0912-Oct-13 22:48 
AnswerRe: Open Office Size Reduction Pinmembercirovladimir3-Oct-13 12:46 
GeneralRe: Open Office Size Reduction Pinmemberrenegade0916-Oct-13 22:32 
QuestionException in macros PinmemberMember 995352819-Sep-13 2:35 
GeneralExcelent PinmemberMember 786715526-Jun-13 18:42 
QuestionRunning a Script to Convert PDF Pinmemberbyronsteffan17-Jun-13 1:44 
QuestionConversion of DOCX Documents Pinmemberbyronsteffan12-Jun-13 3:32 
AnswerRe: Conversion of DOCX Documents Pinmembercirovladimir12-Jun-13 8:08 
GeneralRe: Conversion of DOCX Documents Pinmemberbyronsteffan17-Jun-13 1:02 
QuestionSessions are expired Pinmemberrajapalavesam11-Jan-12 0:41 
QuestionApplication logged out Pinmemberrajapalavesam10-Jan-12 18:19 
BugApplication logged out Pinmemberrajapalavesam10-Jan-12 18:17 
QuestionError - Expected ")" [modified] PinmemberLectrician4-Jan-12 5:35 
AnswerRe: Error - Expected ")" Pinmemberciro_vladimir4-Jan-12 12:54 
GeneralRe: Error - Expected ")" PinmemberLectrician5-Jan-12 5:44 
QuestionImages in word file does't processed PinmemberRavi Mori8-Sep-11 19:47 
AnswerRe: Images in word file does't processed Pinmemberciro_vladimir9-Sep-11 7:19 
GeneralProblem with code bar in document PinmemberFeferes.net7-Oct-10 11:45 
Generalproblem Using in Web Application [modified] PinmemberTyrone_dude20-Jan-10 1:37 
GeneralRe: problem Using in Web Application PinmemberCharles Kong18-Feb-10 3:32 
GeneralRe: problem Using in Web Application Pinmemberciro_vladimir18-Feb-10 3:39 
Generalproblem on window server 2008 Pinmembershailendra118717-Jan-10 23:22 
GeneralRe: problem on window server 2008 Pinmemberciro_vladimir18-Jan-10 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 6 Oct 2006
Article Copyright 2006 by cirovladimir
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid