org.apache.poi——将 office的各种类型文件(word等文件类型)转为 pdf

org.apache.poi------将 office的各种类型文件(word等文件类型)转为 pdf

简介

使用方法

word转pdf

Maven坐标为

bash 复制代码
		<dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.0.3</version>
        </dependency>

核心方法如下所示。

bash 复制代码
//创建转换器
IConverter converter = LocalConverter.builder().build();
//转换需要的参数,依次是输入流、转换前的原类型、输出流、转换后的目标类型
boolean execute = converter.convert(docxInputStream)
                  .as(DocumentType.DOCX)
                  .to(outputStream)
                  .as(DocumentType.PDF)
                  .schedule().get();

使用示例

word转pdf

亲测有效

bash 复制代码
//    public static void main(String[] args) {
//        WordToPdfConverter converter = new WordToPdfConverter("C:\\Users\\LJH\\Desktop\\上传文件sha1为02.docx","C:\\Users\\LJH\\Desktop\\testConvert.pdf");
//        converter.convert();
//    }

public class WordToPdfConverter {

    String sourcePath;
    String targetPath;

    public WordToPdfConverter(String sourcePath, String targetPath){
        this.sourcePath = sourcePath;
        this.targetPath = targetPath;
    }


    /**
     * 输入:目前被限制为Path.toUri()
     * return:可能为null
     * */
    public File convert() {
        File inputWord = new File(sourcePath.toUri());
        File outputPdf = new File(targetPath.toUri());
        InputStream docxInputStream = null;
        OutputStream outputStream = null;
        try {
            docxInputStream = new FileInputStream(inputWord);
            outputStream = new FileOutputStream(outputPdf);
            IConverter converter = LocalConverter.builder().build();
            boolean execute = converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF)
                    .schedule().get();
            if (execute) {
                System.out.println("转换完毕 targetPath = " + outputPdf.getAbsolutePath());
            } else {
                System.out.println("[documents4J] word转pdf失败:");
                return null;
            }

            converter.shutDown();
        } catch (Exception e) {
            System.out.println("[documents4J] word转pdf失败:"+e.toString());
            return null;
        }finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (docxInputStream != null) {
                try {
                    docxInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return outputPdf;
    }
}
相关推荐
2301_803538953 小时前
Java读取Word图片的两种实用方法
java·开发语言·word
gc_22995 小时前
学习C#调用OpenXml操作word文档的基本用法(36:学习特殊符号类-1)
word·openxml·run
2501_930707787 小时前
使用C#代码替换 PDF 文档中的文本
pdf
周末也要写八哥11 小时前
Visual C++6.0下载安装流程及PDF学习手册资源
c++·学习·pdf
优化控制仿真模型11 小时前
2026初中英语考纲词汇表(1600词)PDF电子版
经验分享·pdf
penngo12 小时前
FlowLoom:基于 Apache Spark 的可视化数据处理平台
大数据·spark·apache
2401_8769641313 小时前
27考研优路|肖睿|唐辛|师大集训营|大牙|B站橙啦101公共课PDF
考研·pdf
2401_8769641313 小时前
27余峰|苏一|大李子|鹿吖101公共课托管班网课PDF
pdf
SEO-狼术13 小时前
Visualize Trends with Bar Charts
pdf·.net
私人珍藏库14 小时前
【PC】[吾爱大神原创工具] PDFImageViewer V1 永久免费的PDF图像查看和导出工具
windows·pdf·工具·软件·多功能