[测评系统]--测评系统核心代码库
wzp
2025-06-17 4ce181bd784aeb873a4751826c227161d35de2b5
src/main/java/com/ots/project/tool/PdfUtil.java
@@ -17,7 +17,7 @@
     * 设置用户字体位置
     */
    public static void setFont() {
        String[] fonts = {"C:\\Users\\大头\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "C:\\Users\\datou\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "/usr/share/fonts"};
        String[] fonts = {"C:\\Windows\\Fonts", "C:\\Users\\datou\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "/usr/share/fonts"};
        List<FontSourceBase> fontSources = new ArrayList(Arrays.asList(FontSettings.getDefaultInstance().getFontsSources()));
        for (int i = 0; i < fonts.length; i++) {
            FolderFontSource folderFontSource = new FolderFontSource(fonts[i], true);
@@ -27,23 +27,55 @@
        FontSettings.getDefaultInstance().setFontsSources(fontSourceBases);
    }
//    public static void convertPDF(String input) {
//        try {
//            registerWord();
//            setFont();
//            Document pres = null;
//            //手动替换输出pdf名称
//            String output = input.replaceAll(".docx", ".pdf");
//            long old = System.currentTimeMillis();
//            FileInputStream fileInput = new FileInputStream(input);
//            pres = new Document(fileInput);
//            FileOutputStream out = new FileOutputStream(new File(output));
//            pres.save(out, SaveFormat.PDF);
//            out.close();
//            long now = System.currentTimeMillis();
//            log.info("【转换】doc转pdf耗时:{}秒", ((now - old) / 1000.0));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    public static void convertPDF(String input) {
        FileInputStream fileInput = null;
        FileOutputStream out = null;
        Document pres = null;
        try {
            registerWord();
            setFont();
            Document pres = null;
            //手动替换输出pdf名称
            String output = input.replaceAll(".docx", ".pdf");
            long old = System.currentTimeMillis();
            FileInputStream fileInput = new FileInputStream(input);
            fileInput = new FileInputStream(input);
            pres = new Document(fileInput);
            FileOutputStream out = new FileOutputStream(new File(output));
            out = new FileOutputStream(output);
            pres.save(out, SaveFormat.PDF);
            out.close();
            long now = System.currentTimeMillis();
            log.info("doc转pdf耗时:{}秒", ((now - old) / 1000.0));
            log.info("【转换】doc转pdf耗时:{}秒", ((now - old) / 1000.0));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 确保资源释放
            if (pres != null) pres = null; // 关键:释放Document原生资源
            if (fileInput != null) {
                try { fileInput.close(); } catch (IOException e) { /* ignore */ }
            }
            if (out != null) {
                try { out.close(); } catch (IOException e) { /* ignore */ }
            }
        }
    }
@@ -106,7 +138,7 @@
    public static void main(String[] args) {
        //convertPDF("D:\\测评系统\\home\\林_PAQ_GS_TH.docx");
        convertPDF("C:\\Users\\大头\\Desktop\\林3_CIAQ_IA_CN.docx");
        convertPDF("C:\\Users\\mac\\Desktop\\API_Fan_IA_CN.docx");
    }
}