documents4j 文档转换

简介: documents4j 文档转换

documents4j 是一个 Java 库,可以将文档转换为另一种文档格式。

https://github.com/documents4j/documents4j

<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.6.0</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-local</artifactId>
        <version>1.1.5</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-transformer-msoffice-word</artifactId>
        <version>1.1.5</version>
    </dependency>
</dependencies>
@RequestMapping("/to")
@RestController
public class PdfController {
    /**
     * doc文件转pdf
     *
     * @param path 文件路径
     */
    @RequestMapping("/doc2pdf")
    public ResponseEntity<byte[]> doc2pdfFileUpload(String path) throws IOException {
        URL url = new URL(path);
        URLConnection conn = url.openConnection();
        InputStream inputStream = conn.getInputStream();
        return doc2Pdf(inputStream, ".doc", "fileName.pdf");
    }
    /**
     * docx、xlsx、转pdf
     *
     * @param fileType docx、doc、xls、xlsx
     * @param fileName pdf名称
     */
    public ResponseEntity<byte[]> doc2Pdf(InputStream docxInputStream, String fileType, String fileName) throws IOException {
        // 转换后的pdf临时路径
        File outputFile = new File("C:/Users/admin002/Desktop/folder/doc/" + fileName);
        IConverter converter = LocalConverter.builder().build();
        ResponseEntity<byte[]> fileResult = null;
        try (OutputStream outputStream = Files.newOutputStream(outputFile.toPath());
             // 导出pdf文件给前端
             FileInputStream inputStream = new FileInputStream(outputFile);
             ) {
            if (".docx".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            } else if (".doc".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            } else if (".xls".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
            } else if (".xlsx".equals(fileType)) {
                converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
            }
            byte[] bytes = new byte[(int) outputFile.length()];
            inputStream.read(bytes);
            HttpHeaders headers = new HttpHeaders();
            // 此处pdf名称需要传入
            headers.set("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            fileResult = new ResponseEntity(bytes, headers, HttpStatus.OK);
        } finally {
            docxInputStream.close();
        }
        return fileResult;
    }
}

以上方式依赖于Windows上的MS Word、MS Excel,必须使用Windows。

只有在以下情况下才能运行 LocalConverter构建:

  • JVM在MS Windows平台上运行,该平台附带VBS的Microsoft脚本主机。
  • MS Word 版本必须在2007 或更高版本。安装了 PDF 插件时,才支持 PDF 转换。该插件包含在了Word 2010及更高版本的MS Word中。

未完待续。。。。

目录
打赏
0
0
0
0
16
分享
相关文章
C# 删除Word文档中的段落
【11月更文挑战第3天】本文介绍了两种方法来操作 Word 文档:一是使用 `Microsoft.Office.Interop.Word` 库,适用于 Windows 环境下操作 Word 文档,需引用相应库并在代码中引入命名空间;二是使用 Open XML SDK,适用于处理 .docx 格式的文档,通过引用 `DocumentFormat.OpenXml` 库实现。文中提供了示例代码,展示了如何打开、删除段落并保存文档。
108 5
将word文档转换成pdf文件方法
在Java中,将Word文档转换为PDF文件可采用多种方法:1) 使用Apache POI和iText库,适合处理基本转换需求;2) Aspose.Words for Java,提供更高级的功能和性能;3) 利用LibreOffice命令行工具,适用于需要开源解决方案的场景。每种方法都有其适用范围,可根据具体需求选择。
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
438 2
LangChain-21 Text Splitters 内容切分器 支持多种格式 HTML JSON md Code(JS/Py/TS/etc) 进行切分并输出 方便将数据进行结构化后检索
LangChain-21 Text Splitters 内容切分器 支持多种格式 HTML JSON md Code(JS/Py/TS/etc) 进行切分并输出 方便将数据进行结构化后检索
153 0
PDF转Word有那么难吗?做一个文件转换器,都解决了
这个小工具软件主要针对两个文件格式的转换,一个是针对KML文件对KML文件里面的经纬度的提取和生成,另一个是对PDF文件的格式转换,相信大家用过WPS,WPS的PDF转换是需要开会员才能转,所以我想自己写一个小工具软件,给大家白嫖一下,由于第一次做这种软件有个别考虑不周到的地方,希望大家谅解,我继续学习给大家做更好更优质的小工具给大家使用。文章最后贴了源码和软件安装包大家下载安装就可以用了,源码在后面大家想要继续增加功能也可以,这个小工具是用C#开发的,有一点点小bug不影响使用。家人们建议点赞收藏,你的点赞是我更文的动力。
318 0
提取出 Word 文档里的图片 并利用 python 批量转换格式
日常工作中,你是否遇到过这样的场景,领导发来一份 Word 文档,要求你将文档中的图片存储到一个文件夹内,并且还要将图片都改成 .jpg 或者 .png,你会怎么办?你是不是一边内心崩溃,一边开始一张张的 另存为。今天,庭云教你两招省时省力的方法,不管文档中有几张甚到几百张图片,你都可以快速保存下来。
276 0
提取出 Word 文档里的图片 并利用 python 批量转换格式
利用java实现doc转换pdf
word目前应该是现在最主流的编辑软件了吧,基本每个人都会用到,功能也十分强大,应用人群广泛,但是他也存在一些问题,比如,不同软件或者不同操作系统之间传输时,格式会发生变化,这种变化很让人恼火。所以现在越来越多的人把word转换成pdf格式文件,以保证文件格式不发生变化。
3959 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等