java在不改变格式的情况下将PDF转换为DOCX
我正在尝试将包含表格的PDF文件转换为DOCX类型。当我试图转换它时,我只得到纯文本的输出。如何在不改变格式的情况下将包含表格的整个PDF转换为DOCX?我尝试使用JAVA,下面是代码片段
public static void main(String[] args) throws IOException {
System.out.println("Document converted started");
XWPFDocument doc = new XWPFDocument();
String pdf = "C:\\Users\\30216\\Desktop\\wordtopdf\\sample_full.pdf";
PdfReader reader = new PdfReader(pdf);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
TextExtractionStrategy strategy = parser.processContent(i,
new SimpleTextExtractionStrategy());
String text = strategy.getResultantText();
XWPFParagraph p = doc.createParagraph();
XWPFRun run = p.createRun();
run.setText(text);
run.addBreak(BreakType.PAGE);
}
FileOutputStream out = new FileOutputStream("C:\\Users\\30216\\Desktop\\wordtopdf\\pdftoword.docx");
doc.write(out);
out.close();
reader.close();
System.out.println("Document converted successfully");
}
}
共 (0) 个答案