Java轉換Word文件到PDF文件
阿新 • • 發佈:2018-03-19
plus filepath fileinput mage href except AC inf ext
使用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
Java轉換Word文件到PDF文件