jinlin
2025-04-10 af67fb927c3f30fa70df834f0e97f0b4a91e6119
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.example.client.service;
 
import org.springframework.stereotype.Service;
 
@Service
public class ExcelToPdfService {/*
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = ExcelToPdfService.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    *//**
     * excel 转为pdf 输出。
     *
     * @param sourceFilePath 原始excel路径
     * @param desFilePathd   输出的PDF路径
     *//*
    public static void convertToPdf(String sourceFilePath, String desFilePathd) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            Workbook wb = new Workbook(sourceFilePath);
 
            FileOutputStream fileOS = new FileOutputStream(desFilePathd);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            // 设置为true,表示将整个sheet打印到pdf的一整夜,导致pdf不分页
            // 设置为false,又会有格式问题,暂未解决
            pdfSaveOptions.setOnePagePerSheet(true);
 
            *//*int[] autoDrawSheets = {1300};
            //当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
            autoDraw(wb,autoDrawSheets);*//*
 
            int[] showSheets = {0};
            //隐藏workbook中不需要的sheet页。
            printSheetPage(wb, showSheets);
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    *//**
     * 隐藏workbook中不需要的sheet页。
     *
     * @param wb
     * @param page 显示页的sheet数组
     *//*
    public static void printSheetPage(Workbook wb, int[] page) {
        for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
            wb.getWorksheets().get(i).setVisible(false);
        }
        if (null == page || page.length == 0) {
            wb.getWorksheets().get(0).setVisible(true);
        } else {
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).setVisible(true);
            }
        }
    }
 
    public void ExcelToPdf (String filePath,String desFilePathd) {
        convertToPdf(filePath, desFilePathd);
    }
*/}