Convert PDF to Word using Java

You can easily convert your PDF documents into Word documents (.docx or .doc) programmatically in your Java applications. Such conversion is useful when you need to edit the text of your PDF documents or may need to apply the text formatting. In this article, you are going to learn how to convert PDF to Word using Java.

The following topics are discussed/covered in this article:

Java API to Convert PDF to Word

I will be using GroupDocs.Conversion for Java API for the conversion of PDF to DOCX. This API provides a fast, efficient, and reliable file conversion solution into Java applications without installing any external software. It supports conversions among all popular business document formats such as PDF, HTML, Email, Word, Excel, PowerPoint, Project, Photoshop, CorelDraw, AutoCAD, raster image file formats, and many more. It also allows you to display the whole ‎document, or render it partially to speed up the process. The API is compatible with all Java versions and supports popular operating systems (Windows, Linux, macOS) that are capable to run Java runtime.

Download and Configure

You can download the JAR of the API or just add the following pom.xml configuration in your Maven-based Java application to try the below-mentioned code examples.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-conversion</artifactId>
        <version>21.7</version> 
</dependency>

Convert PDF to Word using Java

You can convert PDF documents to Word by following the simple steps given below:

  1. Create an instance of the Converter class
  2. Provide the input file path
  3. Create an instance of WordProcessingConvertOptions
  4. Set the start page number
  5. Provide total pages to convert
  6. Set output file format
  7. Call the Convert() method along with the output file path and convert options

The following code sample shows how to convert a PDF file into a Word document using Java.

Convert PDF to Word using Java
Convert PDF to Word using Java

The Converter class is the main class that controls the document conversion process. It provides various methods to convert documents of supported file formats. The Convert() method of this class converts source documents and takes two input parameters, the file path to the source document and ConvertOptions to convert a specific source document to desired target file type.

The WordProcessingConvertOptions class provides options for conversion to WordProcessing file type. The setPageNumber() method allows setting the starting page number to start conversion. Whereas, the setPagesCount() method defines the total number of pages to be converted starting from the defined page number. The setFormat() method of this class enables you to set the output format of the converted document. It takes the WordProcessingFileType enumeration type as input.

Convert Specific Pages of PDF to Word

You can convert specific pages of a PDF document to Word by following the simple steps given below:

  1. Create an instance of the Converter class
  2. Provide the input file path
  3. Create an instance of WordProcessingConvertOptions
  4. Set page numbers list to convert
  5. Call the Convert() method along with the output file path and convert options

The following code sample shows how to convert specific pages from a PDF file into a Word document using Java.

The WordProcessingConvertOptions class provides the setPages() method to convert specific page numbers defined in a comma-separated list from a source document.

Load Pasword Protected PDF and Convert to Word

You can convert password-protected PDF documents to Word by following the simple steps given below:

  1. Create PdfLoadOptions
  2. Set password
  3. Create an instance of the Converter class
  4. Provide the input file path
  5. Create an instance of WordProcessingConvertOptions
  6. Call the Convert() method along with the output file path and convert options

The following code sample shows how to convert a password-protected PDF file into a Word document using Java.

The PdfLoadOptions class provides various options to load PDF documents. The setPassword() method of this class enables you to unprotect the protected document by providing its password.

You can find more details about “Load PDF document with options” in the documentation.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to convert PDF documents to Word using Java. You have also learned how to convert a password-protected PDF file to a Word document. Moreover, you have learned how to convert specific pages from a PDF to a Word document programmatically. You can learn even more about GroupDocs.Conversion Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also