How to Convert PDF to Word in Java?

 As we all know, PDF documents not only have strong stability and compatibility, but also have strong security, which can effectively prevent others from inadvertently modifying the content of the document in the work. But at the same time, it also prevents the normal modification of the document. At this point, we can convert the PDF to Word document for modification or re-editing. Using software to convert PDF documents to Word documents is simple, but maintaining the layout and even the font format while converting is not. This article is divided into two parts on how to convert a PDF document to a Word document while maintaining the layout.

  • Convert PDF to Doc/Docx with Fixed Layout
  • Convert PDF to Doc/Docx with Flowable Structure

Fixed Layout mode has fast conversion speed and is conducive to maintaining the original appearance of PDF files to the greatest extent. However, the editability of the resulting document will be limited since each line of text in PDF will be presented in a separate frame in the generated Word document.

Flowable Structure is a full recognition mode. The converted content will not be presented in frames, and the structure of the resulting document is flowable. The generated Word document is easy to re-edit but may look different from the original PDF file.

Programming Environment

Method 1:

Introduced manually. Download Free Spire.PDF for Java locally, unzip it, and find the Spire.PDF.jar file in the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:


If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>

    <repository>

        <id>com.e-iceblue</id>

        <name>e-iceblue</name>

        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>

    </repository>

</repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.pdf.free</artifactId>

        <version>5.1.0</version>

    </dependency>

</dependencies>

Convert PDF to Doc/Docx with Fixed Layout

The following are the steps to convert PDF to Doc or Docx with fixed layout.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.

Full Code

Java

import com.spire.pdf.FileFormat;
import
com.spire.pdf.PdfDocument;

public class
ConvertPdfToWordWithFixedLayout {

   
public static void main(String[] args) {

       
//Create a PdfDocument object
       
PdfDocument doc = new PdfDocument();

       
//Load a sample PDF document
       
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

       
//Convert PDF to Doc and save it to a specified path
       
doc.saveToFile("output/ToDoc.doc", FileFormat.DOC);

       
//Convert PDF to Docx and save it to a specified path
       
doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX);
       
doc.close();
   
}

Convert PDF to Doc/Docx with Flowable Structure

The following are the steps to convert PDF to Doc or Docx with flowable structure.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Set the conversion mode as flow using PdfDocument. getConvertOptions().setConvertToWordUsingFlow() method.
  • Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.

Full Code

Java

import com.spire.pdf.FileFormat;
 import com.spire.pdf.PdfDocument;
public class
ConvertPdfToWordWithFlowableStructure {
   
public static void main(String[] args) {
       
//Create a PdfDocument object
       
PdfDocument doc = new PdfDocument();
       
//Load a sample PDF document
       
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
       
//Convert PDF to Word with flowable structure
       
doc.getConvertOptions().setConvertToWordUsingFlow(true);
       
//Convert PDF to Doc
       
doc.saveToFile("output/ToDoc.doc", FileFormat.DOC);
       
//Convert PDF to Docx
       
doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX);
       
doc.close();
   
}
}

 Effective Shot


Conclusion:

In this article, you have learned how to convert PDF to Word in Java. Not only that, we also have other functions, such as, Java: Convert PDF to Excel, Java: Convert PDF to Images and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.PDF for Java.





Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application