From e763f8274af31b5240776d699d7d6077bf9a320e Mon Sep 17 00:00:00 2001
From: zhaowenxuan <chacca165@163.com>
Date: 星期五, 24 五月 2024 18:11:39 +0800
Subject: [PATCH] 导检单生产工具类

---
 ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java |  275 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 275 insertions(+), 0 deletions(-)

diff --git a/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java b/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java
new file mode 100644
index 0000000..bfb911b
--- /dev/null
+++ b/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java
@@ -0,0 +1,275 @@
+package com.ltkj.common.utils;
+
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.itextpdf.text.*;
+import com.itextpdf.text.pdf.*;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+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 {
+        // 鍒涘缓涓�涓狣ocument瀵硅薄锛屽苟鎸囧畾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);
+//        map.put("stream",null);
+        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));
+    }
+
+    /**
+     * 瀵兼鍗�
+     * 鍒涘缓绗竴涓〃鏍间负鐢ㄦ埛淇℃伅
+     *
+     * @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);
+                        // 鏉″舰鐮�
+                        cell = new PdfPCell(new Phrase(" ", font));
+                        cell.setColspan(4);
+                        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");
+    }
+}

--
Gitblit v1.8.0