word、excel文件转PDF(documents4j方式,简单)

1 documents4j方式

引入pom

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

#excel转pdf需要的jar
<dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-excel</artifactId>
            <version>1.1.12</version>
        </dependency>

2 代码实现(word,excel转pdf代码)

java 复制代码
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

public static void main(String[] args) {
        File inputWord = new File("E:\\111\\123.docx");
        File outputFile = new File("E:\\111\\123.pdf");
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
java 复制代码
/**
     * documents4j方式excel转pdf
     * @param file 源文件
     * @return 返回转成功的文件
     */
    public static File wordToPDFByDocuments4j(File file) {
        File outputFile = new File(String.format("%s%s%s%s", file.getParentFile(),  File.separator, System.currentTimeMillis(), ".pdf"));
        try  {
            InputStream docxInputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outputFile;
    }

2 poi方式

相关推荐
玩泥巴的2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
word·二次开发·office·com互操作
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
带娃的IT创业者2 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
reembarkation2 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation2 天前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf
csdn_aspnet2 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠2 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu2 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化