Java 代码实现pdf转word文件 | 无损转换完整代码
时间: 2023-11-13 21:00:35 浏览: 145
好的,我了解了。以下是用Java实现pdf转word文件的完整代码:
```java
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
public class PdfToWordConverter {
public static void main(String[] args) throws Exception {
String sourcePath = "example.pdf";
String targetPath = "example.doc";
convertPdfToWord(sourcePath, targetPath);
}
public static void convertPdfToWord(String sourcePath, String targetPath) throws Exception {
PdfReader reader = new PdfReader(sourcePath);
HWPFDocument wordDocument = new HWPFDocument();
Range range = wordDocument.getRange();
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(targetPath), "UTF-8");
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
String content = PdfTextExtractor.getTextFromPage(reader, i);
range.insertAfter(content);
}
writer.write(wordDocument.getDocumentText());
writer.flush();
writer.close();
}
}
```
这段代码利用了iText和POI这两个Java库,其中iText是用来读取PDF文件中的文本内容,POI是用来将文本内容写入到word文档中。你可以将sourcePath和targetPath替换成实际的PDF文件路径和要生成的word文件路径。
阅读全文