[测评系统]--测评系统核心代码库
zhijie
2021-09-06 f927e53eb5c98de2e19bc1b47512ff655c54d2ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.ots.project.tool;
 
import com.aspose.words.*;
import lombok.extern.slf4j.Slf4j;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
@Slf4j
public class PdfUtil {
 
    /**
     * 设置用户字体位置
     */
    public static void setFont(){
        String[] fonts = {"C:\\Users\\大头\\AppData\\Local\\Microsoft\\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);
            fontSources.add(folderFontSource);
        }
        FontSourceBase[] fontSourceBases = fontSources.toArray(new FontSourceBase[fontSources.size()]);
        FontSettings.getDefaultInstance().setFontsSources(fontSourceBases);
    }
 
    public static void convertPDF(String input){
        try {
            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 main(String[] args) {
        //convertPDF("D:\\测评系统\\home\\林_PAQ_GS_TH.docx");
        convertPDF("D:\\测评系统\\home\\uploadPath\\测试2_PAQ_GS_CN.docx");
    }
 
}