word转化pdf,pdf转化word

pom

<!--excel 依赖1-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>

        <!--excel 依赖2-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--pdf转word依赖-->
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.pdf.free</artifactId>
            <version>5.1.0</version>
        </dependency>

pdf转化word添加依赖时需要设置仓库为

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

工具类

package com.test.file.utils;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author清梦
 * @site www.xiaomage.com
 * @company xxx公司
 * @create 2023-06-01 14:04
 */
public class WordToPdfUtil {

    /**
     * 将word转为pdf
     * 原文链接:https://blog.csdn.net/weixin_38409915/article/details/125317664
     * @param wordPath
     * @param pdfPath
     * @throws Exception
     */
    public static void wordToPdf(String wordPath,String pdfPath)throws Exception{
        FileInputStream fileInputStream = new FileInputStream(wordPath);
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        PdfOptions pdfOptions = PdfOptions.create();
        FileOutputStream fileOutputStream = new FileOutputStream(pdfPath);
        PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
        fileInputStream.close();
        fileOutputStream.close();

    }


    /**
     * 将 PDF 转换为固定布局的 Doc/Docx 文档 缺陷:转化3页
     * 原文链接:https://blog.csdn.net/Eiceblue/article/details/128253102
     * @param pdfPath
     * @param docPath
     * @param docxPath
     */
    public static void pdfToWordByFIx(String pdfPath,String docPath,String docxPath){
        //创建一个 PdfDocument 对象
        PdfDocument doc = new PdfDocument();

        //加载 PDF 文件
        doc.loadFromFile(pdfPath);

        //将PDF转换为Doc格式文件并保存
        doc.saveToFile(docPath, FileFormat.DOC);

        //将PDF转换为Docx格式文件并保存
        doc.saveToFile(docxPath, FileFormat.DOCX);
        doc.close();

    }



    /**
     *将 PDF 转换为流动形态的 Doc/Docx 文档 缺陷:转化3页
     * @param pdfPath
     * @param docPath
     * @param docxPath
     */
    public static void pdfToWordByFlow(String pdfPath,String docPath,String docxPath){
        //创建一个 PdfDocument 对象
        PdfDocument doc = new PdfDocument();

        //加载 PDF 文件
        doc.loadFromFile(pdfPath);

        //将 PDF 转换为流动形态的Word
        doc.getConvertOptions().setConvertToWordUsingFlow(true);

        //将PDF转换为Doc格式文件并保存
        doc.saveToFile(docPath, FileFormat.DOC);

        //将PDF转换为Docx格式文件并保存
        doc.saveToFile(docxPath, FileFormat.DOCX);
        doc.close();
    }

    public static void main(String[] args) throws Exception{
        //wordToPdf("D:\\学习资料\\java\\day22-Redis.docx","D:\\tmp\\pdf\\Redis.pdf");
        pdfToWordByFIx("D:\\tmp\\pdf\\Redis.pdf","D:\\tmp\\pdf\\Redis.doc","D:\\tmp\\pdf\\Redis.docx");
        pdfToWordByFlow("D:\\tmp\\pdf\\Redis.pdf","D:\\tmp\\pdf\\Redis_flow.doc","D:\\tmp\\pdf\\Redis_flow.docx");
    }
}

测试

运行工具类中的方法,在指定目录下生成文件
word转化pdf,pdf转化word_第1张图片
注意:pdf转化成word时,只能转3页,页数太多
参考博客:
1.https://blog.csdn.net/weixin_38409915/article/details/125317664
2.https://blog.csdn.net/Eiceblue/article/details/128253102

本博客内容源码地址:https://gitee.com/qfp17393120407/file-operator.git

你可能感兴趣的:(word,pdf,java)