Convert PDF to Word Document in Java

NateBennett
Nov 19, 2020

In the process of manipulating PDF documents, it’s inevitably that we may encounter a situation where we need to convert PDF to other document formats to meet the needs of different work occasions. In this article, I’m going to demonstrate how to convert a PDF to Word document with simple code.

Tools:
Free Spire.PDF for Java
● IntelliJ IDEA

Installation
Method 1: Download the Free Spire.PDF for Java and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

The original PDF document is shown as below:

Java Code

import com.spire.pdf.*;

public class ConvertPDF {
public static void main(String[] args) {

//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load the sample PDF file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\The Scarlet Letter.pdf");

//Save as .doc file
doc.saveToFile("output/ToDoc.doc",FileFormat.DOC);

//Save as. docx file
doc.saveToFile("output/ToDocx.docx",FileFormat.DOCX);
doc.close();
}
}

The output Word document:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

NateBennett
NateBennett

Written by NateBennett

Sharing code to help developers deal with office files

Responses (1)

Write a response