How to Convert Office to PDF (Word, Excel & PPT) using Java

As we have been receiving an increasing number of requests for Java based sample code for the Muhimbi PDF Converter Services, we have decided to lift the relevant chapter from the Developer Guide and publish it in this blog post. A .NET version of this post is available here.

For those not familiar with the product, the MDCS is a server based SDK that allows software developers to convert typical Office files, including MS-Word, Excel, PowerPoint, Visio, Publisher and InfoPath, to PDF format using a robust, scalable but friendly Web Services interface from Java and .NET based solutions.

Even though the MDCS itself must run on a Windows based server, it has been designed to interoperate with non-Windows platforms such as Java. This section describes how to convert documents to PDF format using a Java based environment.

The full version of the sample code discussed in this post, including pre generated proxies, is installed alongside each copy of the MDCS.

The example described below assumes the following:

  1. The JDK has been installed and configured.

  2. The MDCS and all prerequisites have been installed in line with the Administration Guide.

  3. The MDCS is running in the default anonymous mode. This is not an absolute requirement, but it makes initial experimentation much easier.

The first step is to generate proxy classes for the web service by executing the following command:

Feel free to change the package name and destination directory to something more suitable for your organisation.

If the Muhimbi Conversion Service is not located on the same system as where wsimport is executed then change localhost to the name of the server running the Conversion Service. You will also need to change the host name in the Conversion Service’s config file. A convenient shortcut to the Installation folder is located in the Muhimbi Start Menu. Open Muhimbi.DocumentConverter

.Service.exe.config, search for baseAddress and change the host name. Restart the Muhimbi Document Converter Service to activate the change.

Wsimport automatically generates the Java class names. Unfortunately some of the generated names are rather long and ugly so you may want to consider renaming some, particularly the Exception classes, to something friendlier. This, however, means that if you ever run wsimport again you will need to re-apply those changes. For more information have a look at the high level overview of the Object Model exposed by the web service.

Once the proxy classes have been created add the following sample code to your project. Run the code and make sure the path to the document to convert is specified on the command line. ( Download Source Code)

This example sets ConversionSettings.Format to OutputFormat.PDF. As a result the file is converted to the default PDF format. It is possible to convert files to other file formats as well by setting this property to a different value. For details see this blog post.

Labels: Articles, Java, pdf, PDF Converter, PDF Converter Services

Have a Question?
We’re Always Happy to Help.

© Muhimbi Ltd. 2008 - 2024
This website uses cookies to ensure you get the best experience. Learn more