Thursday, 24 December 2020

Convert PDF to Word (Doc/Docx) in Java using a free API

As we all know, PDF is widely used for sending the document out to third parties because of its compatibility across multiple platforms. However, in some cases, we need to convert PDF to an editable document format, such as Doc/Docx. This tutorial will showcase how to convert PDF to Word programmatically using Java codes. It’s worth mentioning that I used a free API called Free Spire.PDF for Java to do it.

Before typing codes, you need to download the package Free Spire.PDF for Java and add Spire.Pdf.jar file to your project or reference it using the following Maven configurations:

<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 testing PDF file including an image, text and hyperlink:

Convert PDF to Word (Doc/Docx) using Java

Free Spire.PDF for Java supports converting a PDF document to Word (Doc/Docx) format with several

lines of code. Here are steps required to do it.

l  Create a PdfDocument instance and load a PDF file.

l  Call the SaveToFile method with the output DOC/DOCX file’s name and FileFormat arguments.

import com.spire.pdf.*;

public class PDFToWord {
public static void main(String[] args) {
//create a PdfDocument object
PdfDocument doc = new PdfDocument();

//load a sample PDF file
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//save as .doc file
doc.saveToFile("output/ToDoc.doc",FileFormat.DOC);
doc.saveToFile("output/ToDoc.docx",FileFormat.DOCX);
doc.close();
}
}

Output



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...