javaPDf转换

package com.sunflower.rm.web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class PDFUtil {

    private  static Logger logger = LoggerFactory.getLogger(PDFUtil.class);

    /**
     * breOfficeを使って、ExcelからPDFに変換する。
     * @param inputFile
     * @param pdfFile
     * @return
     */
    public static boolean convertOffice2PDF(String inputFile, String pdfFile) {
        long start = System.currentTimeMillis();
        String command;
        boolean flag;
        String osName = System.getProperty("os.name");
        if (osName.contains("Windows")) {
            command = "cmd /c start soffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
        } else {
            command = "libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile;
        }
        flag = executeLibreOfficeCommand(command);
        long end = System.currentTimeMillis();
        logger.debug("{} msを掛かりました", end - start);
        return flag;
    }


    /**
     * コマンドを実施
     * @param command
     * @return
     */
    public static boolean executeLibreOfficeCommand(String command) {
        logger.info("変換開始.......");
        Process process;
        try {
            logger.debug("convertOffice2PDF cmd : {}", command);
            process = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            logger.error(" convertOffice2PDF {} error", command, e);
            return false;
        }
        int exitStatus = 0;
        try {
            exitStatus = process.waitFor();
            int i = process.exitValue();
            logger.debug("i----" + i);
        } catch (InterruptedException e) {
            logger.error("InterruptedException  convertOffice2PDF {}", command, e);
            return false;
        }
        if (exitStatus != 0) {
            logger.error("convertOffice2PDF cmd exitStatus {}", exitStatus);
        } else {
            logger.debug("convertOffice2PDF cmd exitStatus {}", exitStatus);
        }
        process.destroy();
        logger.info("変換終了.......");
        return true;
    }
}

 注意:要下载https://zh-cn.libreoffice.org/download/libreoffice/

二:

spire.xls第三方jar包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中将PDF转换为Word(.doc或.docx)通常涉及到使用第三方库,因为PDF和Word是两种格式,各有专门的API进行处理。Apache POI是一个广泛使用的Java库,可以用来操作Microsoft Office文档,包括Word,但PDF转Word的功能需要借助其他工具或者库,如iText、PDFBox、Aspose.PDF等。 以下是使用iText库(一个流行的PDF处理库)进行PDF转Word的一个简单示例: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.xmlparser.XMLWorkerHelper; import com.itextpdf.text.xmlparser.XHTMLWorker; public class PDFToWord { public static void main(String[] args) { try { // 读取PDF文件 PdfReader reader = new PdfReader("input.pdf"); // 创建一个新的Word文档 Document wordDoc = new Document(); try (OutputStream out = new FileOutputStream("output.doc")) { // 使用iText解析PDF转换为Word XMLWorkerHelper.getInstance().parseXHtml(wordDoc, out, reader); // 写入Word文件并关闭 wordDoc.close(); } // 关闭PDF reader reader.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,这只是一个基本示例,实际操作可能需要处理更多细节,如处理复杂的PDF结构、字体和样式等问题。在使用这些库之前,确保已经正确地添加了它们到项目中,并且可能需要处理版权许可问题,特别是商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谛仙0

本人已实现,编写不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值