From b4bdd67401d833076dd800f6454ea1af8ae916b2 Mon Sep 17 00:00:00 2001 From: zhaowenxuan <chacca165@163.com> Date: 星期一, 27 五月 2024 17:48:48 +0800 Subject: [PATCH] 修改了系统字体,增加QR码 --- ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java | 48 +++++++++++++++++++++++++++++++++++------------- 1 files changed, 35 insertions(+), 13 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 index bfb911b..ee3d5a4 100644 --- a/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java +++ b/ltkj-common/src/main/java/com/ltkj/common/utils/PDFDocumentUtil.java @@ -1,16 +1,17 @@ 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; @@ -31,7 +32,7 @@ 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(); @@ -40,13 +41,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 +89,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, 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) { // 浣撴鍙� @@ -188,15 +207,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(); -- Gitblit v1.8.0