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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM