使用spire.pdf.free将pdf转为doc的java代码

一、前言

最近需要把pdf转为doc,但是找不到免费工具,好多收费的。

最后找到了一个spire.pdf.free-5.1.0.jar,可以用java代码把pdf转为doc。

有一个坑的地方是,这个免费版jar包只能转前10页pdf;如果pdf有图片等,就只能转前3页了;付费版jar包那就很贵了,买不起。

就当救急的时候用一下吧。

二、代码

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class PdfToDoc {


    public static void main(String[] args) {
        System.out.println("需要输入1个入参,待转换pdf全路径,例如:");
        System.out.println("C:/Users/Administrator/Desktop/示例文档.pdf");

        if(args.length!=1){
            System.out.println("参数错误,无法执行");
            return;
        }

        toFixOrFlow(args[0],false);
        toFixOrFlow(args[0],true);
        System.out.println("转换完成");

    }

    public static void toFixOrFlow(String url,boolean isFlow) {

        String pre = "_fix";

        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(url);
        if(isFlow){
            doc.getConvertOptions().setConvertToWordUsingFlow(true);
            pre = "_flow";
        }
        doc.saveToFile(url.substring(0,url.length()-4)+pre+".doc", FileFormat.DOC);
        doc.saveToFile(url.substring(0,url.length()-4)+pre+".docx", FileFormat.DOCX);
        doc.close();
    }

}

这个代码转出了2个doc,2个docx,分别是固定布局和流布局的,转换后看看哪种样式还行就用哪种吧。

三、其它备注

1.spire.pdf.free-5.1.0.jar,可以用maven下载:


<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

2.spire.pdf.free-5.1.0.jar官网:https://www.e-iceblue.com/Download/pdf-for-java-free.html

3.本人把上面的代码和jar包封装了下,放到了csdn资源里,也可以用:https://download.csdn.net/download/BHSZZY/88244601

猜你喜欢

转载自blog.csdn.net/BHSZZY/article/details/132451549
今日推荐