How to Convert a Word Document to PDF Using Java

Written by ben abeyta | 13/05/2017
How to Convert a Word Document to PDF Using Java
PDFs produce better quality prints than Word documents. (manufacture of printed forms image by terex from Fotolia.com)

Though Word documents are easy to create, they still lack the versatility of portable document format (PDF) documents. For example, PDF documents produce better quality prints than Word documents, according to Planet PDF. Archiving, combining multiple formats and password protection are a few other areas where PDF documents score over Word documents. A number of websites convert Word documents to PDF free of cost; however, it is essential to share an e-mail address with such websites to entail these services. Alternatively, the same job can be done on any machine by using Java, and it doesn't involve sharing an e-mail address.

Start the NetBeans IDE and create a new Java application, name the project "pdf1." Type "\pdf1" in the “Project Location” box at the end of the default location. This will add a new subfolder to that location. Check the “Create Main Class” checkbox, and confirm that the text box next to it reads "pdf1.Main." Click “Finish.”

Extract the JOD Converter zip file to the "lib" subfolder to the first "pdf1" subfolder. After unzipping, ensure that the right pane of Windows Explorer for this folder displays the following files:

lib

pdf1

src

nbproject

test

Import the library into the IDE. In IDE, right-click on the "pdf1" project in the projects window and click on “Properties.” Select the "Libraries" tab and click "Add JAR/Folder.” Click on the "lib" folder and press "Ctrl-A." This will select all the .jar files inside the folder. Select the "Reference as Relative Path" option and press “OK.”

Copy the source code below and paste it into the IDE's code window:

package pdf1;

import java.io.*;

import com.artofsolving.jodconverter.openoffice.connection.*;

import com.artofsolving.jodconverter.openoffice.converter.*;

import com.artofsolving.jodconverter.*;

public class Main {

public static void main(String[] args) throws Exception {

OpenOfficeConnection OOconnection = new SocketOpenOfficeConnection(8100);

OOconnection.connect();

File inFile = new File("doc1.doc");

File outFile = new File("doc1.pdf");

// the conversion

DocumentConverter PDFconverter = new OpenOfficeDocumentConverter(OOconnection);

PDFconverter.convert(inFile, outFile);

OOconnection.disconnect();

}

}

Start OpenOffice as a background service. Select "Start -> Run," type "cmd" and press "Enter." Browse to the "program" subfolder of OpenOffice using the DOS command "cd" (change directory). Type:

cd "<DRIVE>:\Program Files"

cd "OpenOffice.org <Version>"

cd program

Where <DRIVE> is the drive in which you installed OpenOffice and "<Version>" is the version. Make sure that the quotation marks are not omitted. Start OpenOffice at the command prompt by typing the following:

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

Create a Word document titled "doc1.doc" and copy it in the second “pdf1” folder. Under the "pdf1\pdf1" folder in Windows Explorer, the following list should be visible:

build

build.xml

manifest.mf

doc1.doc

nbproject

src

test

Compile and run the program from the Java IDE. This can be done by pressing “Shift-F6.” If successfully executed, you will see a “doc1.pdf” in the same folder as “doc1.doc.”

Tip

The most challenging task in performing the conversion is organising and working with the different folders and "packages" that Java needs to compile your program. If your understanding of Java packages is decent, the actual process will not be as complicated as it looks. Although there are many other Java IDEs, such as Eclipse and Aptana, NetBeans is best suited in this case. The steps to be followed are more or less the same; however, read the documentation for each of the IDEs before implementing and beginning the steps.

Warning

In order to successfully convert the Word file, rename it to "doc1.doc" and place it in the "doc1\doc1" subdirectory. Only then should the conversion program be executed.

Tips and warnings

  • The most challenging task in performing the conversion is organising and working with the different folders and "packages" that Java needs to compile your program. If your understanding of Java packages is decent, the actual process will not be as complicated as it looks.
  • Although there are many other Java IDEs, such as Eclipse and Aptana, NetBeans is best suited in this case. The steps to be followed are more or less the same; however, read the documentation for each of the IDEs before implementing and beginning the steps.
  • In order to successfully convert the Word file, rename it to "doc1.doc" and place it in the "doc1\doc1" subdirectory. Only then should the conversion program be executed.

Things you need

  • Java Development Kit (JDK)
  • Open Office Suite
  • Java Open Document Converter
  • NetBeans Integrated Developing Environment (IDE)

Show MoreHide

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.