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_2013);
            }
            // 删除临时文件
            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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值