How to Convert a Word Document to PDF Using Java

Updated July 20, 2017

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:






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


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

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

// the conversion

DocumentConverter PDFconverter = new OpenOfficeDocumentConverter(OOconnection);

PDFconverter.convert(inFile, outFile);




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 ":\Program Files"

cd " "

cd program

Where is the drive in which you installed OpenOffice and "" 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=,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:







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


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'll Need

  • Java Development Kit (JDK)
  • Open Office Suite
  • Java Open Document Converter
  • NetBeans Integrated Developing Environment (IDE)
Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Ben Abeyta began writing in 2009. His work has appeared on the websites Financial Web and CVTips. He served in the U.S. Navy, where he received intense medical training. Abeyta holds has a Master of Business Administration in global management from the University of Phoenix.