zjh
2025-02-11 a5bbda30b568e9d72679da762166c40c9508b414
ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java
@@ -1,16 +1,19 @@
package com.ltkj.common.utils;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
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.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -31,7 +34,7 @@
    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();
@@ -40,13 +43,13 @@
        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));
//        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));
    }
    /**
@@ -88,10 +91,28 @@
                        cell.setBorder(Rectangle.NO_BORDER);
                        table1.addCell(cell);
                        // 条形码
                        cell = new PdfPCell(new Phrase(" ", font));
                        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, 500, 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) {
                        // 体检号
@@ -188,15 +209,18 @@
        PdfPCell cell;
        Font font;
        font = getFont();
        if (fontStyle != null)
        if (fontStyle != null) {
            font.setStyle(fontStyle);
        }
        // 文字居右
        cell = new PdfPCell(new Phrase(title, font));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        if (titleCol != null)
        if (titleCol != null) {
            cell.setColspan(titleCol);
        if (isCenter)
        }
        if (isCenter) {
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        }
        cell.setBorder(Rectangle.NO_BORDER);
        table1.addCell(cell);
        font = getFont();
@@ -219,7 +243,7 @@
     * @throws DocumentException
     * @throws IOException
     */
    public static void makeTjInfo(Document document,List<Map<String ,String>> data) throws DocumentException, IOException {
    public static void makeTjInfo(Document document,List<Map<String ,Object>> data) throws DocumentException, IOException {
        PdfPTable table1 = new PdfPTable(7);
        table1.setWidthPercentage(100);
        float[] columnWidths = {1.5f, 3, 3, 6, 1.5f, 3, 3};
@@ -272,4 +296,29 @@
        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();
    }
}