700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java使用aspose.pdf或者spire.pdf 将pdf文件转word 实测

java使用aspose.pdf或者spire.pdf 将pdf文件转word 实测

时间:2019-09-12 00:17:30

相关推荐

java使用aspose.pdf或者spire.pdf 将pdf文件转word 实测

1. aspose.pdf

aspose.pdf不是破解版的只能转3页,所以我弄了个破解版, aspose.pdf破解版在网上都有破解方法也比较简单,这里就不说了,直接引入破解版的jar包,在这里我用的是aspose-pdf-21.11.jar版本,代码比较简单

long startTime = System.currentTimeMillis();try (InputStream in = new FileInputStream("C:\\Users\\JBW\\Desktop\\测试.pdf");OutputStream out = new FileOutputStream("C:\\Users\\JBW\\Desktop\\导出.docx");) {com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(in);System.out.println("初始化doc耗时:" + (System.currentTimeMillis()-startTime));pdfDoc.save(out, com.aspose.pdf.SaveFormat.Doc);System.out.println("耗时:" + (System.currentTimeMillis()-startTime));pdfDoc.close();}

我的pdf有12页,然后我这样转发现用了将近120多秒

2. spire.pdf

spire.pdf也是需要收费的,免费版的将pdf转word文档只能转10页,我搜索了一些资料,也是不知道怎么实现破解版,所以这里就用免费版的。但是就是这十页,只用了 10秒左右。这个代码也比较简单

public static void main(String[] args) {long startTime = System.currentTimeMillis();String srcPath = "C:\\Users\\JBW\\Desktop\\测试.pdf";String desPath = "C:\\Users\\JBW\\Desktop\\导出.docx";PdfDocument pdf = new PdfDocument();pdf.loadFromFile(srcPath);//保存为Word格式pdf.saveToFile(desPath, FileFormat.DOCX);removeWatermark(new File(desPath));System.out.println("耗时:"+ (System.currentTimeMillis()-startTime));}//移除文字水印public static boolean removeWatermark(File file) {try {XWPFDocument doc = new XWPFDocument(new FileInputStream(file));//添加水印addWaterMark(doc, "测试");// 段落List<XWPFParagraph> paragraphs = doc.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {String text=paragraph.getText();if (text.contains("Evaluation Warning")){List<XWPFRun> runs = paragraph.getRuns();runs.forEach(e-> e.setText("",0));}}FileOutputStream outStream = new FileOutputStream(file);doc.write(outStream);outStream.close();} catch (IOException e) {e.printStackTrace();}return true;}

removeWatermark方法是去除spire.pdf水印的。

因为上面的代码只能转十页,所以我们换个思路,我们把pdf文件每页生成一份pdf,然后将pdf转成docx文档,在将docx文档合并起来,这样就可以实现,耗时的话是50多秒,也是比上面的aspose.pdf破解版快,代码

public static void main(String[] args) {long startTime = System.currentTimeMillis();String srcPath = "C:\\Users\\JBW\\Desktop\\测试.pdf";String desPath = "C:\\Users\\JBW\\Desktop\\导出.docx";// 加载原始 PDF 文件PdfDocument originalPdf = new PdfDocument();originalPdf.loadFromFile(srcPath);// 创建一个新的空白 Word 文档,用于存储合并后的页面Document mergedDoc = new Document();// 遍历每一页,将其转换为 Word 文档并添加到新文档中for (int i = 0; i < originalPdf.getPages().getCount(); i++) {// 提取单个页面PdfDocument page = new PdfDocument();page.insertPage(originalPdf, i);// 将单个页面保存为 Word (.docx) 格式文件String tempDocxPath = "temp_page_"+i+".docx";page.saveToFile(tempDocxPath, FileFormat.DOCX);// 加载临时 Word 文档并将其添加到最终的合并文档中if (i ==0){mergedDoc.loadFromFile(tempDocxPath);} else {mergedDoc.insertTextFromFile(tempDocxPath, com.spire.doc.FileFormat.Docx_);}// 删除临时文件File file = new File(tempDocxPath);file.delete();}// 保存合并后的 PDF 文件mergedDoc.saveToFile(desPath);removeWatermark(new File(desPath));// 关闭文档originalPdf.clone();System.out.println("耗时:"+ (System.currentTimeMillis()-startTime));}

总结

不知道是不是aspose.pdf破解版的原因,速度是比较慢的,所以在这里我是选择spire.pdf

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。