Java 实现 WORD转PDF,用Documents4j

window系统,安装有MS office 可以使用

参考地址:https://stackoverflow.com/a/59624576

依赖:


    com.documents4j
    documents4j-local
    1.0.3


    com.documents4j
    documents4j-transformer-msoffice-word
    1.0.3

 Java代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

public class Document4jApp {

    public static void main(String[] args) {

        File inputWord = new File("E:\\data\\1需求说明书.doc");
        File outputFile = new File("Test_out2.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();
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

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