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 document content during work. But at the same time, it also hinders the normal modification of the document. At this time, we can convert the PDF to a Word document for modification or re-editing. Converting PDF documents to Word documents using software is easy, but maintaining layout and even font formatting while converting is not. This article will be divided into the following two parts to introduce how to convert PDF to Word document while maintaining the layout.
-
Convert PDF to fixed layout Doc/Docx documents
-
Convert PDF to Doc/Docx document in flowing form
The conversion speed of the fixed layout mode is fast, which is conducive to maintaining the original appearance of the PDF file to the greatest extent. However, the editability of the resulting document will be limited because each line of text in the PDF will appear in a separate frame in the resulting Word document.
Flow patterns are a complete recognition pattern. Converted content is not rendered in frames, and the structure of the resulting document is fluid. The resulting Word document is easily re-editable, but may not look the same as the original PDF file.
Code compilation environment:
IntelliJ IDEA 2018(jdk 1.8.0)
PDF Jar package: Free Spire.PDF for Java 5.1.0
1. Import the jar package
Import method 1:
Imported manually. Download Free Spire.PDF for Java locally, unzip it, and find the Spire.PDF.jar file under the lib folder. Open the following interface in IDEA, and import the jar file in the local path into the Java program:
Import method 2: If you want to pass Maven installed, you can add the following code in the pom.xml file to import the JAR file. the
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-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 fixed layout Doc/Docx documents
The following are the specific steps to convert PDF to fixed layout Doc/Docx documents.
-
Create a PdfDocument object.
-
Load a PDF file using the PdfDocument.loadFromFile() method.
-
Use the PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method to convert a PDF document to a Doc or Docx format file.
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 PDF file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample document.pdf"); //Convert PDF to Doc format file and save doc.saveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx format file and save doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.close(); } }
Convert PDF to Doc/Docx document in flowing form
The following are the specific steps to convert PDF to Doc/Docx documents in flowing form:
-
Create a PdfDocument object.
-
Load a PDF file using the PdfDocument.loadFromFile() method.
-
Use the PdfDocument.getConvertOptions().setConvertToWordUsingFlow() method to set the conversion mode to flow.
-
Use the PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method to convert a PDF document to a Doc or Docx format file.
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 PDF file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample document.pdf"); //Convert PDF to Word in flowing form doc.getConvertOptions().setConvertToWordUsingFlow(true); //Convert PDF to Doc format file and save doc.saveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx format file and save doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.close(); } }
renderings
—End of this article—