fei
1 天以前 e899d2709f21b322232fb64778fef296233be3f0
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
package com.ruoyi.service.impl;
 
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
 
@Service
public class pdfGenerateService {
    @Autowired
    private BarcodeService barcodeService;
    //生产二维码
    public byte[] createQrCodeN(String content, int width, int height) throws IOException {
        QrConfig config = new QrConfig(width, height);
 
        config.setMargin(3);
        // 高纠错级别
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        // 设置前景色,既二维码颜色(自行选择颜色修改)
        config.setForeColor(new Color(11, 11, 11).getRGB());
// 设置背景色(灰色)需要背景色时候打开链接
        config.setBackColor(new Color(242,242,242).getRGB());
 
 
        BufferedImage bufferedImage = QrCodeUtil.generate(//
                content, //二维码内容
                config
        );
        System.out.println(bufferedImage);
        System.out.println("---02340230949394");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", os);
 
 
//        ImageIO.write(bufferedImage, "jpg", new File("outputImage.jpg"));
 
        return os.toByteArray();
    }
 
 
    public void generatePdf(String pdfPath) throws IOException, DocumentException {
 
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
        document.open();
 
        String [] tits = {"档        号:","档案馆(室)号:","缩  微  号: ","发  文  号:",
                "案  卷  题  名:","编  制  日  期:","编  制  单  位:","保  管  期  限:","密     级:"};
        String [] cons = {"D3.4.1-05-2024-0002","","","穗规划资源建证〔2024〕2033号","广州市自来水有限公司白云区江高镇江高镇政府西侧、江府路北侧白云区江高镇江高镇政府西侧、江府路北侧新装供水管工程","2024-04-23","广州市规划和自然资源局白云区分局","永久",""};
 
        //把图片加入到pdf当中
        Image img = Image.getInstance(createQrCodeN(cons[0], 80, 80));
        // 设置图片在PDF中的位置(可选)
//        img.setAbsolutePosition(100, 100);
        // 将图片添加到PDF文档中
        PdfPCell pdfPCell = new PdfPCell(img);
        pdfPCell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框
 
        pdfPCell.setMinimumHeight(80);
        pdfPCell.setUseAscender(true); // 设置可以居中
        pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // 设置水平居中
        pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
        pdfPCell.setColspan(2);
        pdfPCell.setPaddingBottom(30);
 
// 创建表格并设置列宽比例
        float[] columnWidths = {35f, 65f}; // 第一列30%,第二列70%
        PdfPTable table = new PdfPTable(columnWidths);
//        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(80); // 增大表格宽度百分比
        table.setHorizontalAlignment(Element.ALIGN_LEFT); // 设置表格整体居中
        table.setSpacingBefore(30f); // 设置表格前间距
        table.setSpacingAfter(90f); // 设置表格后间距
 
 
//        PdfPTable table1 = new PdfPTable(1);
//        table1.setWidthPercentage(90); // 增大表格宽度百分比
//        table1.setHorizontalAlignment(Element.ALIGN_LEFT); // 设置表格整体居中
//        table1.setSpacingBefore(10f); // 设置表格前间距
//        table1.setSpacingAfter(10f); // 设置表格后间距
        table.addCell(pdfPCell);
//        document.add(table1);
        // 设置中文字体
 
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font chineseFont = new Font(bfChinese, 16, Font.BOLD);
        Font chineseFont1 = new Font(bfChinese, 10, Font.BOLD);
 
        // 添加档号
        // 添加带下划线的档号
 
 
 
        for(int i = 0; i < tits.length; i++) {
            // 使用PdfPTable实现精确对齐
 
            // 第一行:档号
            PdfPCell labelCell1 = new PdfPCell(new Phrase(tits[i], chineseFont));
            labelCell1.setHorizontalAlignment(Element.ALIGN_RIGHT); // 设置右对齐
 
            labelCell1.setBorder(Rectangle.NO_BORDER);
//            PdfPCell valueCell1 = new PdfPCell(new Phrase(cons[i], chineseFont));
//            valueCell1.setBorder(Rectangle.NO_BORDER);
            labelCell1.setMinimumHeight(30);
            labelCell1.setPaddingTop(10f);    // 上内边距10单位
            labelCell1.setPaddingBottom(10f); // 下内边距10单位
            labelCell1.setPaddingLeft(15f);   // 左内边距15单位
            labelCell1.setPaddingRight(15f);  // 右内边距15单位
 
 
            PdfPCell valueCell1 = new PdfPCell(new Phrase(cons[i], chineseFont));
            valueCell1.setBorder(Rectangle.NO_BORDER);
            valueCell1.setUseBorderPadding(true);
            valueCell1.setBorderWidthBottom(0.5f); // 设置底部边框作为下划线
            valueCell1.setMinimumHeight(30);
            valueCell1.setPaddingTop(10f);    // 上内边距10单位
            valueCell1.setPaddingBottom(10f); // 下内边距10单位
            valueCell1.setPaddingLeft(15f);   // 左内边距15单位
            valueCell1.setPaddingRight(15f);  // 右内边距15单位
            table.addCell(labelCell1);
            table.addCell(valueCell1);
 
            if(i==3)
            {
                //增加那个条形码
                byte []  bas = barcodeService.generateBarcodeImage(cons[0]);
 
                Image img1= Image.getInstance(bas);
 
                PdfPCell pdfPCell1 = new PdfPCell(img1);
                pdfPCell1.setBorder(Rectangle.NO_BORDER); // 移除单元格边框
 
                pdfPCell1.setMinimumHeight(40);
                pdfPCell1.setUseAscender(true); // 设置可以居中
                pdfPCell1.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // 设置水平居中
                pdfPCell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
                pdfPCell1.setColspan(2);
                pdfPCell1.setPaddingBottom(20);
                pdfPCell1.setPaddingTop(20);
                table.addCell(pdfPCell1);
            }
 
 
        }
        document.add(table); // 空行
 
        // 关闭文档
        document.close();
    }
}