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破解版快,如果想要具体的代码,点击下载
总结
不知道是不是aspose.pdf破解版的原因,速度是比较慢的,所以在这里我是选择spire.pdf


















