这个简短的教程旨在指导如何通过加载源 PDF 文件并将其保存为 Word 文件来用 Java 将 PDF 转换为 Word。在用 Java 编写 **PDF 到 Word 转换器时,您可以控制阅读源 PDF 文件。同样,输出 Word DOC 文件也可以通过设置其特征来自定义。
使用 Java 将 PDF 转换为 Word 的步骤
- 通过从 Maven 存储库中添加 Aspose.PDF 来配置项目以将 PDF 转换为 Word 文件
- 在 Document 类对象中加载源 PDF 文件以转换为 Word 文件
- 实例化 DocSaveOptions 类对象以配置输出 Word 文件
- 定义输出 Word 文件类型和识别模式以设置未来的编辑模式
- 设置源 PDF 文件读取参数,如水平接近度和项目符号识别标志
- 使用 DocSaveOptions 对象中的指定配置保存输出 Word 文件
上述步骤描述了使用 Java* 设计 *PDF 到 Word 转换器的过程,方法是从 Maven 存储库中识别所需的库和分步操作序列。将源PDF文件加载到Document类对象中,并初始化一个DocSaveOptions类对象,用于控制从源PDF文件的读取操作和设置输出Word文件的参数。输出 Word 文件可以保存为 DocSaveOptions 类对象中配置的任何 MS Word 支持的文件格式。
使用 Java 将 PDF 转换为 Word 的代码
package AsposePdf; | |
import com.aspose.pdf.DocSaveOptions; | |
import com.aspose.pdf.Document; | |
import com.aspose.pdf.ExcelSaveOptions; | |
import com.aspose.pdf.License; | |
public class ConvertPdfToWordInJava { | |
public static void main(String[] args) throws Exception { // main method to convert a PDF document to Word file format | |
// Instantiate the license to avoid trial limitations while converting the PDF to word file | |
License asposePdfLicenseDoc = new License(); | |
asposePdfLicenseDoc.setLicense("Aspose.pdf.lic"); | |
// Load the source PDF file that is to be converted to Word file | |
Document convertPDFDocumentToWord = new Document("input.pdf"); | |
// Initialize the DocSaveOptions class object to configure output word file | |
DocSaveOptions docSaveOptions = new DocSaveOptions(); | |
// Define the type of output Word file | |
docSaveOptions.setFormat(DocSaveOptions.DocFormat.Doc); | |
// Set the recognition mode to Flow for enabling it for editing in future | |
docSaveOptions.setMode(DocSaveOptions.RecognitionMode.Flow); | |
// Set the Horizontal proximity that defines width of space between text elements as 2.5 | |
docSaveOptions.setRelativeHorizontalProximity(2.5f); | |
// Switch on the recognition of bullets from the source PDF | |
docSaveOptions.setRecognizeBullets(true); | |
// Convert PDF to Word using the Document class function save | |
convertPDFDocumentToWord.save("output.doc", docSaveOptions); | |
System.out.println("Done"); | |
} | |
} |
- 将 PDF 转换为 Word Java 代码* 导入程序中使用的必要类并从磁盘加载源 PDF 文件。 DocSaveOptions 类支持设置很多参数,比如定义输出 Word 文件类型可以是 DOC 或 DOCX,设置标志以创建可编辑的输出 Word 文件,设置标志以识别项目符号,设置转换后的水平和垂直分辨率图片等等。
在这里,我们学习了如何借助示例代码将 PDF 转换为 Java 中的 Word。如果您想了解将 PDF 转换为 Excel 的过程,请参阅 如何在 Java 中将 PDF 转换为 Excel 上的文章。