Java转换Word文件到PDF文件
使用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
作者:马洪彪
出处:http://www.cnblogs.com/mahongbiao/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。