本文记录使用documents4j来将word文件转化为pdf文件
文章目录
- 程序实例
- maven导入
- 代码实现
- 程序结果
- 本文小结
程序实例
maven导入
<!--word转pdf--><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>
代码实现
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import lombok.extern.slf4j.Slf4j;import java.io.*;@Slf4j
public class Main {public static void main(String[] args) {File inputWord = new File("D:\\pdf\\test.doc");File outputFile = new File("D:\\pdf\\test.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();}}
}
程序结果
本文小结
本文记录了使用documents4j来将word文件转化为pdf文件