DEV Community

Gia
Gia

Posted on

How to Convert PDF to Word with Fixed Layout

Document conversion is very common, such as converting Word to PDF, PDF to Excel, and so on. Sometimes, you may need to make some adjustments to your PDF files. However, the stability of PDF also increases the difficulty of modifying documents. Why not try converting the PDF to a Word file format? As you know, editing or formatting Word documents is a piece of cake for most people. Through this conversion, users can also easily handle PDF files, which improves productivity and efficiency. Next, I will introduce how to achieve this conversion programmatically in Java.

Step 1: Install a PDF Library

Although there are many software programs available that can quickly achieve conversion for users, I still want to recommend Free Spire.PDF for Java. It not only provides fast conversion functions but also maintains the original layout of the pages during conversion.
You can download Free Spire.PDF for Java to local and unzip it.

Step 2: Import JAR file to your project

Take IntelliJ IDEA 2018 as an example
Then create a new project in IntelliJ IDEA, click “File”- “Project Structure” - “Modules” - “Dependencies” in turn. Choose the “JARs or Directories” under the right green plus, and find the “Spire.Pdf.jar” in the lib folder of the decompressed package and import it to the project.

Step 3: Edit code

Here is a sample code:

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("sample.pdf");

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

Image description

The following explanation can help you understand this code easily:
Firstly, create a new class named “ConvertPdfToWordWithFixedLayout”. Create a “PdfDocument” object and load your PDF file using “PdfDocument.loadFromFile()” method. Finally, Convert the PDF document to a Doc or Docx format file using “PdfDocument.saveToFile()”method.

Top comments (0)