Java:pdf转word

转换思路

  1. 使用第三方库Spire的免费版本,转换页数要求11页
  2. 输入一个pdf时候,小于11页直接转换,大于11页就先切分成子pdf
  3. 对每一个小的pdf进行转换,最后再合并。

总体上就是一个大的pdf拆分转换再合并的问题。

特点

  1. 图片不会转换
  2. 文字正常转换
  3. 数学公式正常转换
  4. 排版不会失真

完全的不失真也做不到,会有微小的差别,但是和pdf基本上一样

java代码

pom.xml

<repositories>
	<repository>
		<id>com.e-iceblue</id>
		<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
	</repository>
</repositories>

<dependencies>
	<dependency>
		<groupId>e-iceblue</groupId>
		<artifactId>spire.doc.free</artifactId>
		<version>2.7.3</version>
	</dependency>
	<dependency>
		<groupId>e-iceblue</groupId>
		<artifactId>spire.pdf.free</artifactId>
		<version>2.6.3</version>
	</dependency>
</dependencies>

文件工具类

package cn.com.javakf.utils;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

import java.io.File;

public class FileUtil {
    /**
     * 判断是否是pdf文件
     *
     * @param path
     * @return
     */
    public static boolean isPDFFile(String path) {
        File file = new File(path);
        String filename = file.getName();
        if (filename.endsWith(".pdf")) {
            return true;
        }
        return false;
    }

    /**
     * 创建子pdf目录,子doc目录
     *
     * @param pdfPath
     * @param docPath
     * @return
     */
    public static boolean createSubdirectory(String pdfPath, String docPath) {
        File pdfFile = new File(pdfPath);
        File docFile = new File(docPath);
        if (!pdfFile.exists()) {
            pdfFile.mkdirs();
        }
        if (!docFile.exists()) {
            docFile.mkdirs();
        }
        return true;
    }

    /**
     * 合并word文档
     *
     * @param docPath 要合并文档路径
     * @param desPath 合并后文件路径
     * @return
     */
    public static boolean mergeWordDocument(String docPath, String desPath) {
        File[] files = getFiles(docPath);
        Document document = new Document(docPath + "test0.docx");
        for (int i = 1; i < files.length; i++) {
            document.insertTextFromFile(docPath + "test" + i + ".docx", FileFormat.Docx_2013);
        }
        //保存合并的doc文档
        document.saveToFile(desPath);
        return true;
    }

    /**
     * 取目录下的所有文件
     *
     * @param path
     * @return
     */
    public static File[] getFiles(String path) {
        File file = new File(path);
        File[] files = file.listFiles();
        if (files == null) {
            return null;
        }
        return files;
    }


    /**
     * 删除目录(包含文件)
     *
     * @param path
     */
    public void deleteDirectory(String path) {
        File file = new File(path);
        if (file.exists()) {
            deleteFile(file);
        }
    }

    /**
     * 删除文件
     *
     * @param file
     */
    public void deleteFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                deleteFile(files[i]);
            }
        }
        file.delete();
    }

}

pdf转word

package cn.com.javakf.utils;

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

import java.io.File;

public class SpirePdfUtil {

    /**
     * 如果是大文件,需要进行切分,子pdf路径
     */
    String pdfPath = "./pdf/";
    /**
     * 如果是大文件,需要对子pdf文件进行转化,子doc路径
     */
    String docPath = "./doc/";

    /**
     * pdf转word
     *
     * @param srcPath
     * @return
     */
    public String pdfToWord(String srcPath) {
        boolean result = false;
        try {
            //最终生成的doc所在目录,默认和源文件同一文件夹
            String desPath = srcPath.substring(0, srcPath.length() - 4) + ".docx";
            // 1、判断输入的是否是pdf文件
            boolean pdfflag = FileUtil.isPDFFile(srcPath);
            if (pdfflag) {
                // 2、加载pdf
                PdfDocument pdf = new PdfDocument();
                pdf.loadFromFile(srcPath);
                // 3、如果pdf的页数小于11,那么直接进行转化
                if (pdf.getPages().getCount() <= 10) {
                    pdf.saveToFile(desPath, FileFormat.DOCX);
                } else {
                    // 3、否则输入的页数比较多,就开始进行切分再转化
                    // 创建临时子pdf目录,子doc目录
                   boolean flag = FileUtil.createSubdirectory(pdfPath, docPath);
                    if (flag) {
                        // 第一步:将其进行切分,每页一张pdf
                        pdf.split(pdfPath + "test{0}.pdf", 0);
                        // 第二步:将切分的pdf,一个一个进行转换
                        File[] files = FileUtil.getFiles(pdfPath);
                        for (int i = 0; i < files.length; i++) {
                            PdfDocument sonpdf = new PdfDocument();
                            sonpdf.loadFromFile(files[i].getAbsolutePath());
                            sonpdf.saveToFile(docPath + files[i].getName().substring(0,
                                    files[i].getName().length() - 4) +
                                    ".docx", FileFormat.DOCX);
                        }
                        //第三步:对转化的doc文档进行合并,合并成一个大的word
                        result = FileUtil.mergeWordDocument(docPath, desPath);
                    } else {
                        return "创建文件夹失败!!!";
                    }
                }
            } else {
                return "不是pdf文件!!!";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //合并word文档成功后,删除临时创建的子pdf目录,子doc目录
            if (result == true) {
                new FileUtil().deleteDirectory(pdfPath);
                new FileUtil().deleteDirectory(docPath);
            }
        }
        return "OK";
    }

}

测试

package cn.com.javakf.test;

import cn.com.javakf.utils.SpirePdfUtil;

public class PdfToWordTest {

    public static void main(String[] args) {
        String result = new SpirePdfUtil().pdfToWord("D:\\test.pdf");
        System.out.println(result);
    }

}

Evaluation Warning : The document was created with Spire.PDF for Java.

解决办法:因为这段文字只出现在第一页,所以在文档创建时先添加一个空白页,最后再把空白页去掉。

参考:
https://github.com/fengdongdongwsn/PdfTool
https://www.e-iceblue.cn/spirepdfjava/spire-pdf-for-java-program-guide-content.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值