Java Project – PDF Converter

In this project, we aim to develop a PDF Converter using Java. It will help us convert .docx files to PDFs. We can attach a docx file from our system, and it will convert it into a PDF.

About Java PDF Converter

The objective of this project is to create a PDF Converter using Java that allows the user to select a Word document (.docx file) and convert it into a PDF format. The application uses a graphical user interface (GUI) for selecting files.

Prerequisites for Java PDF Converter

  • Basic knowledge of Java programming
  • Familiarity with Java Swing for creating GUI applications
  • Basic understanding of Maven for managing project dependencies
  • Maven and Java should be correctly installed on your system.

Download the Java PDF Converter Project

Please download the source code of the Java PDF Converter Project: Java PDF Converter Project Code.

Steps to Create a PDF Converter using Java

We should proceed by following these steps to implement our project.

Step 1: Create a Maven Project

We should create a new Maven project in our IDE (e.g., IntelliJ IDEA, Eclipse, or VS Code). In VS Code, we can do this by pressing Ctrl+Shift+P to open the Command Palette, typing “Maven: Create Maven Project“, and then selecting it.

Step 2: Add Required Dependencies

We add the required dependencies in the pom.xml file in our root directory.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>WordToPDFConverter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>oss.sonatype.org</id>
            <url>https://oss.sonatype.org/content/repositories/releases/</url>
        </repository>
    </repositories>
</project>

Explanation:

  • We have added the Apache POI dependency to our Project because it provides the ability to read, write, and manipulate Microsoft file formats such as .docx. It is a collection of libraries.
  • We have used 4 of them. Poi, as it is a core library, poi-ooxml helps us read and write docx files. poi-ooxml-schemas assists in processing the file’s content. Poi-scratchpad provides features not included in the core library.
  • We have added XDocReport Dependency as it directly supports the conversion of files from docx to PDF format.
  • We have added the iText Dependency so we can easily generate PDFs and process content.
  • JUnit for testing and Log4j for debugging.

Step 3: Create our Main Class

package com.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordToPDF {
    public static void main(String[] args) {
        UIManager.put("FileChooser.openDialogTitleText", "FirstCode Open File");
        UIManager.put("FileChooser.openButtonText", "FirstCode Open File");
       
        WordToPDF pdf = new WordToPDF();
        pdf.ConvertingToPDF();
    }

    public void ConvertingToPDF() {
        try {
            JFileChooser Choose_file = new JFileChooser();
            FileNameExtensionFilter docFilter = new FileNameExtensionFilter("DOCX Files", "docx");
            Choose_file.setFileFilter(docFilter);

            int result = Choose_file.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File docFile = Choose_file.getSelectedFile();
                InputStream doc = new FileInputStream(docFile);
                XWPFDocument document = new XWPFDocument(doc);

                Choose_file.setFileFilter(null);
                Choose_file.setSelectedFile(new File(docFile.getParent(), docFile.getName().replace(".docx", ".pdf")));
                result = Choose_file.showSaveDialog(null);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File pdfFile = Choose_file.getSelectedFile();
                    OutputStream out = new FileOutputStream(pdfFile);
                    PdfOptions options = PdfOptions.create();
                    PdfConverter.getInstance().convert(document, out, options);
                }
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Explanation:

  • We imported the necessary libraries first using java.io.*. We can read and write bytes from files.
  • javax.swing.JFileChooser allows users to select files or directories. Apache POI imports are used to handle Microsoft Office documents.
  • We defined a WordToPDF class, created an instance, and called the ConvertToPDF() method.
    We define ConvertToPDF() and create an instance of file_choice for file selection. With the help of docFilter, we allow the system to show only .docx files.
  • The result is used to store the user selection and creates an InputStream to read the selected .docx file. An XWPFDocument object represents Word documents.
  • Then, using file_choice, we change the extension from docx to PDF and wait for the user to take action on the name change. On Approval, it creates an OutputStream to write to the selected PDF file.
  • An Options object is created for PDF Conversion, and then the XWPFDocument is converted to PDF, and the output stream is written to it.
  • Catch is used to catch any IOExceptions that occur during the process and print the error message.

Java PDF Converter Output

selecting file

file choosed

coverting file

Summary

We have completed implementing the PDF Converter in Java, which allows us to select a file from our system and save it accordingly. We utilised various dependencies to complete this project, including the Java Swing package for file selection features. We discussed the project details, prerequisites, and the implementation of the project code.

I hope you liked this project. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *