zjh
2025-01-06 1ce606a5cd06b859a57eabf8b12c4fb3704168b4
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.ltkj.common.utils.pdfutils;
 
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
 
 
/**
 * @Author: 西安路泰科技有限公司/lige
 * @Date: 2022/12/8 9:31
 */
public class PdfUtils {
 
    /**
     * 字体存放的跟路径,默认为'C:\Windows\Fonts\'
     */
    private static final String FONT_PATH = System.getProperty("user.dir") + File.separator + "ltkj-admin"+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"Font"+File.separator;
 
    /**
     * 纸张大小
     */
    private static Rectangle RECTANGLE = PageSize.A4;
 
    /**
     * 设置字体默认值
     *
     * @return
     * @throws DocumentException
     * @throws IOException
     */
    private static BaseFont createBaseFont(String fontName) throws DocumentException, IOException {
        // 默认为宋体
        if (fontName == null) {
            fontName = "simsun.ttc";
        }
        String fontPre = fontName.substring(fontName.lastIndexOf(".") + 1);
        if (fontPre.equals("ttc")) {
            // ttc格式的字体需要加上后缀
            fontName = fontName + ",0";
        }
        String font = FONT_PATH + fontName;
        return BaseFont.createFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    }
 
    /**
     * 设置字体
     *
     * @return
     */
    public static Font setFont() {
        return setFont(null, null, null, null);
    }
 
    public static Font setFont(Integer fontSize) {
        return setFont(null, fontSize, null, null);
    }
 
    public static Font setFont(Integer fontSize, BaseColor fontColor) {
        return setFont(null, fontSize, null, fontColor);
    }
 
    /**
     * @param fontName  字体名称 默认宋体
     * @param fontSize  字体大小 默认12号
     * @param fontStyle 字体样式
     * @param fontColor 字体颜色 默认黑色
     * @return
     */
    public static Font setFont(String fontName, Integer fontSize, Integer fontStyle, BaseColor fontColor) {
        try {
            BaseFont baseFont = createBaseFont(fontName);
            Font font = new Font(baseFont);
            if (fontSize != null) {
                font.setSize(fontSize);
            }
            if (fontStyle != null) {
                font.setStyle(fontStyle);
            }
            if (fontColor != null) {
                font.setColor(fontColor);
            }
            return font;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("设置字体失败!"+FONT_PATH);
        }
    }
 
    /**
     * 创建pdf文档
     *
     * @param response
     * @param fileName
     * @return
     */
    public static Document createDocument(HttpServletResponse response, String fileName) {
        try {
            response.reset();
            response.setHeader("Content-Type", "application/pdf-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        // 设置大小
        RECTANGLE = PageSize.A4;
        return new Document(RECTANGLE, 50, 50, 50, 50);
    }
 
    /**
     * 绘制标题
     *
     * @param font
     * @param titleName
     * @return
     */
    public static Paragraph setParagraph(Font font, String titleName) {
        Paragraph paragraph = new Paragraph(titleName, font);
        //设置文字居中
        paragraph.setAlignment(Element.ALIGN_LEFT);
        //行间距
        paragraph.setLeading(5f);
        //设置段落上空白
        paragraph.setSpacingBefore(10f);
        //设置段落下空白
        paragraph.setSpacingAfter(10f);
        return paragraph;
    }
 
    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            // 设置表格大小
            table.setTotalWidth(RECTANGLE.getWidth() - 100);
            //锁住宽度
            table.setLockedWidth(true);
            //table.setSpacingBefore(3f);
            // 设置表格下面空白宽度
            //table.setSpacingAfter(3f);
 
            // 居中
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            // 边框
            //table.getDefaultCell().setBorder(1);
            table.getDefaultCell().setBorder(0);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
 
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
 
    public static PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        // 水平、垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
 
        // 设置无边框
        cell.setBorder(Rectangle.NO_BORDER);
 
        // 设置高度
        //cell.setFixedHeight(20);
 
        return cell;
    }
}