引入依赖包
org.springframework.boot spring-boot-starter-web 2.2.5.RELEASE com.alibaba fastjson 1.2.59 org.Apache.poi poi 3.10.1 org.apache.poi poi-scratchpad 3.10.1 org.apache.poi poi-ooxml 3.10.1 fr.opeNSAgres.xdocreport org.apache.poi.xwpf.converter.core 1.0.6 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.PDF 1.0.6 fr.opensagres.xdocreport fr.opensagres.xdocreport.itext.extension 2.0.1
测试代码
package com.hjyang.doc.convert.pdf.doc.conver; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import Java.io.*; public class testDoc2Pdf { public static void wordToPdf(string docFile,String pdfFile) throws IOException { XWPFDocument document; InputStream doc = new FileInputStream(docFile); document = new XWPFDocument(doc); PdfOptions options = PdfOptions.create(); OutputStream out = new FileOutputStream(pdfFile); PdfConverter.getInstance().convert(document, out, options); doc.close(); out.close(); } public static void main(String[] args) { try { wordToPdf("D:/word2pdfFile/123.docx", "D:/word2pdfFile/123.pdf"); } catch (Exception e) { e.printStackTrace(); } } }
测试样式
说明:
(1)支持中文,不需要字体转化;
(2)样式保存较好。