上传.PDF文件转为.DOCX文件

上传.PDF文件转为.DOCX文件,pdf中文件包含了图片和样式,转换为docx后的格式不错乱。

依赖了破解版的jar包:aspose-pdf-22.4.cracked.jar。该jar包可以在我的资源里下载。

代码如下:

 

package io.cxwm.commons.tools.utils;
import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class PdfUtil {

    public static void main(String[] args) {
        String pdf = "C:\\Users\\Wxian\\Desktop\\商务文件.pdf";
        pdf2docx(pdf);
    }

    /**
     * pdf转doc
     * @param pdfPath pdf文件路径
     */
    public static String pdf2docx(String pdfPath) {
        long old = System.currentTimeMillis();
        String wordPath = "";
        try {
            //新建一个word文档
            wordPath = pdfPath.substring(0,pdfPath.lastIndexOf("."))+".docx";
            FileOutputStream os = new FileOutputStream(wordPath);
            //doc是将要被转化的word文档
            Document doc = new Document(pdfPath);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.DocX);
            os.close();
            //转化用时
            long now = System.currentTimeMillis();
            System.out.println("Pdf 转 Word 共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 转 Word 失败...");
            e.printStackTrace();
        }
        return wordPath;
    }

    /**
     * 删除本地文件
     * @param filePath 本地文件路径
     */
    public static void delete(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            if (file.canWrite()) {
                boolean deleted = file.delete();
                if (deleted) {
                    System.out.println("文件删除成功");
                } else {
                    System.out.println("文件删除失败");
                }
            } else {
                System.out.println("文件不可写,无法删除");
            }
        } else {
            System.out.println("文件不存在,无法删除");
        }
    }

    /**
     * pdf文件转doc文件
     * @param file pdf文件
     * @return
     * @throws IOException
     */
    public static MultipartFile pdfCoverDocx(MultipartFile file) throws IOException {
        String projectPath = System.getProperty("user.dir");
        String pdfPath = projectPath + File.separator + file.getOriginalFilename();
        saveMultipartFileToLocal(file,pdfPath);
        String wordPath = PdfUtil.pdf2docx(pdfPath);
        File targetFile = new File(wordPath);
        file = convertFileToMultipartFile(targetFile);
        PdfUtil.delete(pdfPath);
        PdfUtil.delete(wordPath);
        return file;
    }

    /**
     * multipartFile转File
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
        File file = new File(multipartFile.getOriginalFilename());
        file.createNewFile();
        FileCopyUtils.copy(multipartFile.getInputStream(), new FileOutputStream(file));
        return file;
    }

    /**
     * File转multipartFile
     * @param file
     * @return
     * @throws IOException
     */
    public static MultipartFile convertFileToMultipartFile(File file) throws IOException {
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile("file",
                file.getName(), "application/octet-stream", input);
        return multipartFile;
    }

    /**
     * 保存文件到本地
     * @param multipartFile 文件
     * @param targetPath 本地路径
     * @throws IOException
     */
    public static void saveMultipartFileToLocal(MultipartFile multipartFile, String targetPath) throws IOException {
        File targetFile = new File(targetPath);
        try (InputStream inputStream = multipartFile.getInputStream();
             OutputStream outputStream = new FileOutputStream(targetFile)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    }

}

相关推荐

  1. .PDF文件转为.DOCX文件

    2024-04-03 08:36:03       19 阅读
  2. python文件docxpdf

    2024-04-03 08:36:03       43 阅读
  3. docx、excel、word转pdf文件

    2024-04-03 08:36:03       26 阅读

最近更新

  1. 用户特征和embedding层做Concatenation

    2024-04-03 08:36:03       0 阅读
  2. opencv 设置超时时间

    2024-04-03 08:36:03       1 阅读
  3. Nginx Websocket 协议配置支持

    2024-04-03 08:36:03       1 阅读
  4. Perl语言入门到高级学习

    2024-04-03 08:36:03       1 阅读
  5. 【 HTML基础知识】

    2024-04-03 08:36:03       1 阅读

热门阅读

  1. 音响或耳机音频处理常用的芯片

    2024-04-03 08:36:03       17 阅读
  2. 微信小程序中路由跳转方式

    2024-04-03 08:36:03       23 阅读
  3. Python网络爬虫(二):Requests库

    2024-04-03 08:36:03       12 阅读
  4. SQLAlchemy 的数据库引擎engine与连接对象Connection

    2024-04-03 08:36:03       17 阅读
  5. Knife4j配置使用笔记

    2024-04-03 08:36:03       16 阅读
  6. nodejs的express负载均衡

    2024-04-03 08:36:03       15 阅读
  7. 每天学习一个Linux命令之dpkg

    2024-04-03 08:36:03       18 阅读
  8. linux扩展正则表达式之+

    2024-04-03 08:36:03       13 阅读
  9. JVM中一次完整的 GC 流程

    2024-04-03 08:36:03       18 阅读
  10. 2023年网络安全领域新兴技术的发展特点

    2024-04-03 08:36:03       16 阅读