zjh
2025-01-20 73a816fd2b1e29c25d615c85ce34b12b55c16ccf
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.ltkj.web.config.pdfutils;
 
import cn.hutool.core.io.FileUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.ltkj.hosp.domain.TjPdfVO;
import lombok.extern.slf4j.Slf4j;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.List;
 
import static com.itextpdf.text.Rectangle.NO_BORDER;
 
 
/**
 * @Author: 西安路泰科技有限公司/lige
 * @Date: 2022/12/8 9:31
 */
@Slf4j
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 final String FONT_PATH = "C:\\Windows\\Fonts\\";
//    private static final String FONT_PATH = "/Users/chacca/开发相关/代码/ltkj_peis/ltkj-admin/src/main/resources/Font/";
 
    /**
     * 纸张大小
     */
    private static Rectangle RECTANGLE = PageSize.A4;
 
    /**
     * 设置字体默认值
     *
     * @return
     * @throws DocumentException
     * @throws IOException
     */
    public 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;
//        log.info("字体路径 -> {}",font);
        return BaseFont.createFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    }
 
    public static void main(String[] args) {
        try {
            BaseFont simsun = createBaseFont(null);
            System.out.println("simsun = " + simsun);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 设置字体
     *
     * @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;
    }
 
    /**
     * 设置
     * 表格内容
     *
     * @param headFont
     * @param textFont
     * @param title
     * @param list
     * @return
     */
    public static PdfPTable setTable(Font headFont, Font textFont, String[] title, List<TjPdfVO> list,boolean flag) {
        //四列
        PdfPTable table = createTable(new float[]{120, 120, 120});
        //画标题
        for (String head : title) {
            table.addCell(createCell(head, headFont));
        }
        //画内容
        for (TjPdfVO tjPdfVO : list) {
            table.addCell(createCell(tjPdfVO.getProName(), textFont));
            table.addCell(createCell(tjPdfVO.getProResult(), textFont));
            table.addCell(createCell(tjPdfVO.getCompany(), textFont));
//            table.addCell(createCell(tjPdfVO.getStandardValue(), textFont));
            if ((tjPdfVO.getSj() != null || tjPdfVO.getTs() != null) && flag){
                PdfPCell cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase("检查所见:", textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
 
                cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase(tjPdfVO.getSj(), textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
 
                cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase("结论:", textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
 
                cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase(tjPdfVO.getTs(), textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
            }
        }
        return table;
    }
 
    /**
     * 设置
     * 表格内容
     *
     * @param headFont
     * @param textFont
     * @param title
     * @param list
     * @return
     */
    public static PdfPTable setTable1(Font headFont, Font textFont, String[] title, List<TjPdfVO> list) {
        //四列
        PdfPTable table = createTable(new float[]{240, 240});
        //画标题
        for (String head : title) {
            table.addCell(createCell(head, headFont));
        }
        //画内容
        for (TjPdfVO tjPdfVO : list) {
            table.addCell(createCell(tjPdfVO.getProName(), textFont));
            table.addCell(createCell(tjPdfVO.getProResult(), textFont));
        }
        return table;
    }
 
    /**
     * 设置
     * 表格内容
     *
     * @param headFont
     * @param textFont
     * @param title
     * @param list
     * @return
     */
    public static PdfPTable setTable2(Font headFont, Font textFont, String[] title, List<TjPdfVO> list,boolean flag,float[] titleWidth) {
        //四列
        PdfPTable table = createTable(titleWidth);
//        PdfPTable table = createTable(new float[]{240, 240,240});
        //画标题
        for (String head : title) {
            table.addCell(createCell(head, headFont));
        }
        //画内容
        for (TjPdfVO tjPdfVO : list) {
            table.addCell(createCell(tjPdfVO.getProName(), textFont));
            table.addCell(createCell(tjPdfVO.getProResult(), textFont));
            table.addCell(createCell(tjPdfVO.getCompany(), textFont));
            if ((tjPdfVO.getSj() != null || tjPdfVO.getTs() != null) && flag){
                PdfPCell cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase("检查所见:", textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
 
                cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase(tjPdfVO.getSj(), textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
 
                cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase("结论:", textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
 
                cell = new PdfPCell();
                // 水平、垂直居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                cell.setPhrase(new Phrase(tjPdfVO.getTs(), textFont));
                // 设置无边框
                cell.setBorder(Rectangle.NO_BORDER);
                cell.setColspan(3);
                table.addCell(cell);
            }
        }
        return table;
    }
 
 
    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_LEFT);
            // 边框
            //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_LEFT);
        cell.setPhrase(new Phrase(value, font));
 
        // 设置无边框
        cell.setBorder(Rectangle.NO_BORDER);
 
        // 设置高度
        //cell.setFixedHeight(20);
 
        return cell;
    }
 
    /**
     *
     * @param value
     * @param font
     * @param boder Rectangle类的枚举
     * @param txtAlign Element类的枚举
     * @return
     */
    public static PdfPCell createCell(String value, Font font,int boder,int txtAlign,Float paddingTop,Float paddingBottom) {
        PdfPCell cell = new PdfPCell();
        // 水平、垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(txtAlign);
        cell.setPhrase(new Phrase(value, font));
        if (paddingTop != null)
            cell.setPaddingTop(paddingTop);
        if (paddingBottom != null)
            cell.setPaddingBottom(paddingBottom);
        // 设置无边框
        cell.setBorder(boder);
        // 设置高度
        //cell.setFixedHeight(20);
 
        return cell;
    }
}