赵文轩
2025-01-08 1c30ab054c3d5c3585325d6123be7aaf84eeae20
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
package com.ltkj.common.utils;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.apache.commons.codec.binary.Base64;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zhaowenxuan
 * @Date: 2024/5/24 13:41
 */
public class PDFDocumentUtil {
    /**
     * 创建document文档对象
     *
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static Map<String ,Object> getDocument() throws IOException, DocumentException {
        // 创建一个Document对象,并指定A4大小的页面和边距
        Document document = new Document(PageSize.A4, 30, 30, 30, 30);
        // 本地测试文件 打开注释即可 两种都可存在
//        String PDF_FILE = "d:\\Users\\w\\Desktop\\test.pdf";
//        PdfWriter.getInstance(document, Files.newOutputStream(Paths.get(PDF_FILE)));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfWriter.getInstance(document,outputStream);
        document.open();
        HashMap<String, Object> map = new HashMap<>();
        map.put("document",document);
        map.put("stream",outputStream);
        return map;
    }
 
 
    public static Font getFont() throws DocumentException, IOException {
//        return new Font(BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED));
        return new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
    }
 
    /**
     * 导检单
     * 创建第一个表格为用户信息
     *
     * @param document
     * @param name
     * @param sex
     * @param tjNumber
     * @param phone
     * @param danwei
     * @param time
     * @throws IOException
     */
    public static void makeUserInfoTable(Document document, String name, String sex
            , String tjNumber, String phone, String danwei
            , String time) throws IOException {
        try {
            // 两个表格固定都是10列
            // 创建基础信息表格
            PdfPTable table1 = new PdfPTable(10);
            table1.setWidthPercentage(100);
            // 设置列宽
            float[] columnWidths = {1.5F, 2, 2, 1, 2, 3, 2, 1, 2, 2};
            table1.setWidths(columnWidths);
            PdfPCell cell;
            Font font;
            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 10; j++) {
                    if (i == 0 && j == 3) {
                        font = getFont();
                        font.setStyle(Font.BOLD);
                        font.setSize(24);
                        // 标题
                        cell = new PdfPCell(new Phrase("体检导检单", font));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setColspan(3);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        // 条形码
                        HashMap<EncodeHintType, Object> map = new HashMap<>();
                        map.put(EncodeHintType.CHARACTER_SET,"utf-8");
                        BitMatrix encode = null;
                        try {
                            encode = new MultiFormatWriter().encode(tjNumber, BarcodeFormat.CODE_128, 300, 100, map);
                        } catch (WriterException e) {
                            e.printStackTrace();
                        }
                        BufferedImage toBufferedImage = MatrixToImageWriter.toBufferedImage(encode);
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        ImageIO.write(toBufferedImage,"png",byteArrayOutputStream);
                        Image image = Image.getInstance(byteArrayOutputStream.toByteArray());
                        cell = new PdfPCell(image,true);
                        cell.setColspan(4);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        // 添加一行空值 拉开条形码和用户信息的距离
                        for (int k = 0;k<10;k++){
                            cell = new PdfPCell(new Phrase(" "));
                            cell.setBorder(Rectangle.NO_BORDER);
                            table1.addCell(cell);
                        }
                        break;
                    } else if (i == 1 && j == 5) {
                        // 体检号
                        addKVCell(table1, Font.BOLD, "体检号:", tjNumber, Rectangle.BOTTOM, null, 4, false);
                        break;
                    } else if (i == 2 && j == 0) {
                        addKVCell(table1, Font.BOLD, "姓名:", name, Rectangle.BOTTOM, null, null, false);
                        addKVCell(table1, Font.BOLD, "性别:", sex, Rectangle.BOTTOM, null, null, false);
                        cell = new PdfPCell(new Phrase(" ", getFont()));
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        addKVCell(table1, Font.BOLD, "手机号:", phone, Rectangle.BOTTOM, null, 3, false);
                        break;
                    } else if (i == 3 && j == 0) {
                        addKVCell(table1, Font.BOLD, "单位:", danwei, Rectangle.BOTTOM, null, 4, false);
                        addKVCell(table1, Font.BOLD, "登记日期:", time, Rectangle.BOTTOM, null, 3, false);
                        break;
                    } else if (i == 4 && j == 0) {
                        addKVCell(table1, null, "温馨提示:", " ", null, 2, null, true);
                        break;
                    } else if (i == 5 && j == 8) {
                        cell = new PdfPCell(new Phrase("1、采血,腹部彩超检查后方可用餐。", getFont()));
                        cell.setColspan(9);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        break;
                    } else if (i == 6 && j == 1) {
                        cell = new PdfPCell(new Phrase("2、近期若有生育计划及未成年人,不宜进行胸片、双能骨密度等放射检查。", getFont()));
                        cell.setColspan(9);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        break;
                    } else if (i == 7 && j == 1) {
                        cell = new PdfPCell(new Phrase("3、怀孕或可能已受孕的女士,还应避免妇科、放射科及阴式超声检查。", getFont()));
                        cell.setColspan(9);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        break;
                    } else if (i == 8 && j == 1) {
                        cell = new PdfPCell(new Phrase("4、经期女性不宜进行妇科及尿常规检查。", getFont()));
                        cell.setColspan(9);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        break;
                    } else if (i == 9 && j == 1) {
                        cell = new PdfPCell(new Phrase("5、若您在等待检查过程中有疑问,请及时联系导检护士。", getFont()));
                        cell.setColspan(9);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        break;
                    } else if (i == 10 && j == 1) {
                        cell = new PdfPCell(new Phrase("6、体检结束后请将导检单交还至前台。", getFont()));
                        cell.setColspan(9);
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        break;
                    }
                    font = getFont();
                    cell = new PdfPCell(new Phrase(" ", font));
                    cell.setBorder(Rectangle.NO_BORDER);
                    table1.addCell(cell);
                }
            }
            for (PdfPCell pdfPCell : table1.getRows().get(0).getCells()) {
                try {
                    pdfPCell.setFixedHeight(50);
                } catch (Exception e) {
 
                }
            }
            document.add(table1);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 导检单
     * 设置键值对
     * @param table1
     * @param fontStyle 字体样式
     * @param title     key
     * @param val       val
     * @param valBottom val是否有下划线
     * @param titleCol  key跨列
     * @param valCol    val跨列
     * @param isCenter  是否居中
     * @throws DocumentException
     * @throws IOException
     */
    public static void addKVCell(PdfPTable table1, Integer fontStyle
            , String title, String val, Integer valBottom, Integer titleCol, Integer valCol
            , boolean isCenter) throws DocumentException, IOException {
        PdfPCell cell;
        Font font;
        font = getFont();
        if (fontStyle != null) {
            font.setStyle(fontStyle);
        }
        // 文字居右
        cell = new PdfPCell(new Phrase(title, font));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        if (titleCol != null) {
            cell.setColspan(titleCol);
        }
        if (isCenter) {
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        }
        cell.setBorder(Rectangle.NO_BORDER);
        table1.addCell(cell);
        font = getFont();
        cell = new PdfPCell(new Phrase(val, font));
        if (valBottom != null) {
            cell.setBorder(Rectangle.BOTTOM);
        } else {
            cell.setBorder(Rectangle.NO_BORDER);
        }
        if (valCol != null) {
            cell.setColspan(4);
        }
        table1.addCell(cell);
    }
 
    /**
     * 设置导检单 主体信息
     * @param
     * @param
     * @throws DocumentException
     * @throws IOException
     */
    public static void makeTjInfo(Document document,List<Map<String ,String>> data) throws DocumentException, IOException {
        PdfPTable table1 = new PdfPTable(7);
        table1.setWidthPercentage(100);
        float[] columnWidths = {1.5f, 3, 3, 6, 1.5f, 3, 3};
        table1.setWidths(columnWidths);
        String[] titleArray = {"序号", "体检科室", "科室位置", "项目", "空腹", "医师签字", "弃检签字"};
        Font font = getFont();
        font.setStyle(Font.BOLD);
        for (int i = 0; i < 7; i++) {
            PdfPCell cell = new PdfPCell(new Phrase(titleArray[i], font));
            cell.setFixedHeight(30);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table1.addCell(cell);
        }
        font = getFont();
        font.setSize(10);
        for (int i = 0; i < data.size(); i++) {
            PdfPCell cell = new PdfPCell(new Phrase(String.valueOf(i+1), font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table1.addCell(cell);
            cell = new PdfPCell(new Phrase((String) data.get(i).get("ksmc"), font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table1.addCell(cell);
            cell = new PdfPCell(new Phrase((String) data.get(i).get("ksdz"), font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table1.addCell(cell);
            cell = new PdfPCell(new Phrase((String) data.get(i).get("tjxm"), font));
            table1.addCell(cell);
            cell = new PdfPCell(new Phrase(" ", font));
            table1.addCell(cell);
            cell = new PdfPCell(new Phrase(" ", font));
            table1.addCell(cell);
            cell = new PdfPCell(new Phrase(" ", font));
            table1.addCell(cell);
        }
        document.add(table1);
    }
 
    public static ByteArrayOutputStream execDj(List<List<Map<String ,Object>>> data) throws IOException, DocumentException {
        Map<String, Object> map = getDocument();
        Document document = (Document) map.get("document");
        makeUserInfoTable(document, "李四", "女", "189787998", "123456", "单位", "2022-04-21");
        List<Map<String, Object>> mapList = data.get(Integer.parseInt("tjInfo"));
//        makeTjInfo(document, mapList);
        // 多条则换页
        document.newPage();
        document.close();
        return (ByteArrayOutputStream) map.get("stream");
    }
 
    /**
     * 将多个base64pdf合并为一个
     * @param base64PdfList pdf集合
     * @return
     */
    public static byte[] mergePDFs(List<String > base64PdfList) throws DocumentException, IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, byteArrayOutputStream);
        document.open();
        for (String base64Pdf : base64PdfList) {
            byte[] pdfBytes = Base64.decodeBase64(base64Pdf);
            PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfBytes));
            int numberOfPages = reader.getNumberOfPages();
            for (int i = 1; i <= numberOfPages; i++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, i);
                copy.addPage(page);
            }
            reader.close();
        }
        document.close();
        return byteArrayOutputStream.toByteArray();
    }
}