| | |
| | | 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 javax.imageio.ImageIO; |
| | | import java.awt.image.BufferedImage; |
| | | 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; |
| | |
| | | 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(); |
| | |
| | | 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)); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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, 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) { |
| | | // 体检号 |
| | |
| | | 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(); |