以下提供两种方法实现,建议使用aspose
一、第一种,使用aspose-words Word转PDF
第一步:首先需要下载aspose-words-15.8.0包,官方的地址很慢,并且包下载不下来,有需要可以去GITHUB上寻找,这里我提供一个离线包CSDN下载地址:aspose-words-15.8.0.rar-其它文档类资源-CSDN下载
第二步:把jar包使用pom或则web项目引入
此处演示pom.xml导入,下载以上CSDN中资源放入Maven的repository中即可导入
<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>15.8.0</version><classifier>jdk16</classifier>
</dependency>
第三步:配置一下License.xml,和水印说byebye,智能破解(xml文件放入当前包resource目录下即可)
<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
第四步:pdf转word
/*** @author : * @date : * description : Aspose工具类*/
public class AsposeUtil {/*** 加载license 用于破解 不生成水印** @author LCheng* @date 2020/12/25 13:51*/@SneakyThrowsprivate static void getLicense() {try (InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("License.xml")) {License license = new License();license.setLicense(is);}}/*** word转pdf** @param wordPath word文件保存的路径* @param pdfPath 转换后pdf文件保存的路径* @author LCheng* @date 2020/12/25 13:51*/@SneakyThrowspublic static void wordToPdf(String wordPath, String pdfPath) {getLicense();File file = new File(pdfPath);try (FileOutputStream os = new FileOutputStream(file)) {Document doc = new Document(wordPath);doc.save(os, SaveFormat.PDF);}}
}
二、第二种,使用poi和最初的itextpdf包(这个方法和最新版的poi包有冲突,不能兼容最新版的poi包)
第一步pom导入:
<dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.3</version></dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.15</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.1</version>
</dependency>
第二步:转换
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import lombok.SneakyThrows;
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 WordToPdf {private final static String DOC_X = "E:\\out\\汛期简报第23期.docx";private final static String PDF = "E:\\out\\test.pdf";/*** 第二种方法* @param wordPath* @param resOut*/public static void convertPdf (String wordPath,OutputStream resOut){//读取wordInputStream inputStream = null;try {inputStream = new FileInputStream(wordPath);XWPFDocument doc = new XWPFDocument(inputStream);PdfOptions options = PdfOptions.create();options.fontProvider((familyName, encoding, size, style, color) -> {BaseFont bfChinese = null;try {bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);} catch (Exception e) {e.printStackTrace();}Font fontChinese = new Font(bfChinese, size, style, color);if (familyName != null)fontChinese.setFamily(familyName);return fontChinese;});PdfConverter.getInstance().convert(doc, resOut, options);} catch (IOException e) {e.printStackTrace();}finally {if(inputStream!=null){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}}/*** 第一种方法,很完整的生成,不影响包冲突(aspose-words)* @param wordPath* @param resOut*/public static void convertPdfFinish(String wordPath,OutputStream resOut){//破解getLicense();try {Document doc = new Document(wordPath);doc.save(resOut,SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();}}/*** 加载license 用于破解 不生成水印** @author LCheng* @date 2020/12/25 13:51*/@SneakyThrowsprivate static void getLicense() {try (InputStream is = WordToPdf.class.getClassLoader().getResourceAsStream("License.xml")) {License license = new License();license.setLicense(is);}}/*** word转pdf** @param wordPath word文件保存的路径* @param pdfPath 转换后pdf文件保存的路径* @author LCheng* @date 2020/12/25 13:51*/@SneakyThrowspublic static void wordToPdf(String wordPath, String pdfPath) {getLicense();File file = new File(pdfPath);try (FileOutputStream os = new FileOutputStream(file)) {Document doc = new Document(wordPath);doc.save(os, SaveFormat.PDF);}}public static void main(String[] args) {wordToPdf(DOC_X,PDF);}/*public static void main(String[] args) {try {InputStream is = new FileInputStream(DOC_X);XWPFDocument doc = new XWPFDocument(is);// 在没有字体的服务器上发布要用到下面 options,同时在resource目录下加入字体文件, windows 服务器上可不加PdfOptions options = PdfOptions.create();options.fontProvider((familyName, encoding, size, style, color) -> {try {BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfChinese, size, style, color);if (familyName != null)fontChinese.setFamily(familyName);return fontChinese;} catch (Exception e) {e.printStackTrace();return null;}});OutputStream out = new FileOutputStream(PDF);PdfConverter.getInstance().convert(doc, out, options);out.close();} catch (Exception e) {e.printStackTrace();}}*/
}