使用Docx4j将Word文件转换为PDF文件:
public static void convertDocxToPDF(String docxFilePath, String pdfPath) throws Exception { OutputStream os = null; try { // 加载文件 File docx = new File(docxFilePath); InputStream is = new FileInputStream(docx); WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(is); // 字体转换 Mapper fontMapper = new IdentityPlusMapper(); fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai")); fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong")); fontMapper.put("华文宋体", PhysicalFonts.get("STSong")); fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong")); fontMapper.put("隶书", PhysicalFonts.get("LiSu")); fontMapper.put("宋体", PhysicalFonts.get("SimSun")); fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei")); fontMapper.put("黑体", PhysicalFonts.get("SimHei")); fontMapper.put("楷体", PhysicalFonts.get("KaiTi")); fontMapper.put("新宋体", PhysicalFonts.get("NSimSun")); fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB")); fontMapper.put("仿宋", PhysicalFonts.get("FangSong")); fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312")); fontMapper.put("幼圆", PhysicalFonts.get("YouYuan")); mlPackage.setFontMapper(fontMapper); // os = new FileOutputStream(pdfPath); FOSettings foSettings = Docx4J.createFOSettings(); foSettings.setWmlPackage(mlPackage); Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); } catch (Exception ex) { ex.printStackTrace(); } finally { IOUtils.closeQuietly(os); } }
官网地址:https://www.docx4java.org/forums/
支持Word、Excel、PPT:
转换Word为PDF,依赖于Plutext,官网地址:http://converter-eval.plutext.com/
Plutext为商业软件包,提供180天试用。
此外可以使用FO,该方式免费。
webapp.docx4java.org/OnlineDemo/docx_to_pdf.html?_ga=2.226968541.1700223194.1521458163-1699339587.1517224689
原文地址:https://www.cnblogs.com/mahongbiao/p/8604272.html
时间: 2024-10-24 23:02:22