DEV Community

CodeSharing
CodeSharing

Posted on

4

Convert PDF to Word Document using Java

This article will demonstrate how to convert a PDF to Word document with a 3rd party free Java API.

Import JAR Dependency (2 Methods)

● Download the Free API (Free Spire.PDF for Java) and unzip it, then add the Spire.Pdf.jar file to your project as dependency.

● Directly 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>


Enter fullscreen mode Exit fullscreen mode

The original PDF document is shown as below:
Alt Text

Code Snippet



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


Enter fullscreen mode Exit fullscreen mode

The output Word document:
Alt Text

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay