===============
== ffff65535 ==
===============
十六个一[1111111111111111]

使用documents4j将转换office为pdf

documents4j word2pdf

使用documents4j将word、excel、ppt转换为pdf

仅支持windows环境转换,需要安装MS office


<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.1.7</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.1.7</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-excel</artifactId>
    <version>1.1.7</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-powerpoint</artifactId>
    <version>1.1.7</version>
</dependency>

使用的依赖


import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.conversion.msoffice.MicrosoftPowerpointBridge;
import com.documents4j.job.LocalConverter;

import java.io.*;

public class OfficeUtil {
    public static void main(String[] args) {
        String startFile = "C:\\test.doc";
        String overFile = "C:\\test.pdf";
        office2Pdf(startFile, overFile);
    }

    public static void office2Pdf(String officePath, String pdfPath) {
        try {
            InputStream inputStream = new FileInputStream(new File(officePath));
            OutputStream outputStream = new FileOutputStream(new File(pdfPath));
            String suffix = officePath.substring(officePath.lastIndexOf("."));
            IConverter converter = LocalConverter.builder().enable(MicrosoftPowerpointBridge.class).build();
            if ("doc".equals(suffix)) {
                converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            } else if ("docx".equals(suffix)) {
                converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            } else if ("xlsx".equals(suffix)) {
                converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
            } else if ("xls".equals(suffix)) {
                converter.convert(inputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
            } else if ("pptx".equals(suffix)) {
                converter.convert(inputStream).as(DocumentType.MS_POWERPOINT).to(outputStream).as(DocumentType.PDF).execute();
            } else if ("ppt".equals(suffix)) {
                converter.convert(inputStream).as(DocumentType.MS_POWERPOINT).to(outputStream).as(DocumentType.PDF).execute();
            }
            converter.shutDown();
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

进行转换的java代码


参考资料

GitHub - documents4j/documents4j: documents4j is a Java library for converting documents into another document format

https://documents4j.com/