Convert Word Document To PDF

In the course of business, there will be times when you want to convert a Microsoft Word document into a PDF document. The easiest way to do this that I have come across is to install OpenOffice. When you open a Word document in OpenOffice, you can save it as a PDF. The conversion is very good.

However, what if you want to do this programmatically from VB.NET? Well, it is very straight forward.

Download and install OpenOffice. You will need a copy of the Java Runtime Edition (JRE). There is a version of OpenOffice that you can download that includes the JRE. Set the PATH environment variable to include the paths to the JRE bin folder, and the OpenOffice program folder. Then download and extract JODConverter. Restart your computer.

When your computer has rebooted, open up a command window. Type the command java -version and press enter. This should output the version of the JRE that is installed on your system. Now type the command soffice and press enter. This should start OpenOffice. Your OpenOffice installation is complete.

Now before you go any further, you have everything you need to convert a Word document to PDF via the command line. The commands to do this are:

"C:\Program Files\ 3\program\soffice.exe" -headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager
java -jar D:\Tools\jodconverter-2.2.2\lib\jodconverter-cli-2.2.2.jar "D:\Files\Test.doc" "D:\Files\Test.PDF"

To execute this code in a consistent and easy manner from VB.NET is a very simple process. Open up Visual Studio .NET 2010 and create a new VB.NET Console Application. Copy the code below into the Module1.vb file and rename the file to EntryPoint.vb.

Imports System
Imports System.Configuration
Imports System.IO
Imports System.Text
Imports System.Collections.Generic

Public Class EntryPoint

Public Shared Sub Main(ByVal args As String())

Dim batchFileCommandText As New StringBuilder
' Prepare the batch file text.
batchFileCommandText.Append(" ")
batchFileCommandText.Append(" ")
batchFileCommandText.Append(" ")
' Create the batch file and save the batch file text.
Using batchFile As New StreamWriter(ConfigurationManager.AppSettings("BatchFile"))
End Using
' Execute the batch file.
' The conversion is a success, print a message and exit.
System.Console.WriteLine("Conversion successful...")
Catch ex As Exception
End Try
End Sub
End Class

Add an app.config file to your project and include the following application settings.

<clear />
<add key="OpenOffice" value="C:\Program Files\ 3\program\soffice.exe" />
<add key="OpenOfficeParameters" value="-headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" />
<add key="JODConverter" value="java -jar D:\Tools\jodconverter-2.2.2\lib\jodconverter-cli-2.2.2.jar" />
<add key="BatchFile" value="D:\Files\convert.bat" />
</appSettings >

Note: If your applications are installed in different locations to the one described above, the you will need to change your app.config file to point to the right locations.

Execute the code now passing in for the parameters the input file followed by the output file. If all goes according to plan, you should now have a generated PDF.