From 977d455677e15c81340dd6c83f346f33905af305 Mon Sep 17 00:00:00 2001 From: yinq Date: Fri, 2 Aug 2024 09:13:20 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E6=89=93=E5=8D=B0=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E5=8E=BB=E9=99=A4=E5=B0=8F=E9=BB=91=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hw/printer/utils/HwPrintUtil.java | 64 +++++++++++++++---- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java index cac5a99..f13331e 100644 --- a/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java @@ -1,5 +1,10 @@ package com.hw.printer.utils; +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.QRCodeWriter; import com.hw.common.core.exception.ServiceException; import com.hw.printer.api.domain.vo.PrintContentVo; import com.itextpdf.text.Document; @@ -17,6 +22,7 @@ import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.print.PrintService; import javax.print.PrintServiceLookup; +import java.awt.image.BufferedImage; import java.awt.print.*; import java.io.ByteArrayOutputStream; import java.io.File; @@ -24,6 +30,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.List; +import java.util.Map; /** @@ -189,22 +196,53 @@ public class HwPrintUtil { return null; } - private static void setImage(AcroFields form, PdfStamper stamper, String fieldName, String fieldValue) throws DocumentException { - //获取位置(左上右下) + private static void setImage(AcroFields form, PdfStamper stamper, String fieldName, String fieldValue) throws DocumentException, WriterException, IOException { + // 获取位置(左上右下) AcroFields.FieldPosition fieldPosition = form.getFieldPositions(fieldName).get(0); - // 绘制二维码 - float width = fieldPosition.position.getTop() - fieldPosition.position.getBottom(); - BarcodeQRCode pdf417 = new BarcodeQRCode(fieldValue, (int) width, (int) width, null); - // 生成二维码图像 - Image image128 = pdf417.getImage(); - // 条码位置 - image128.setAbsolutePosition(fieldPosition.position.getLeft() + (fieldPosition.position.getRight() - fieldPosition.position.getLeft() - width) / 2 - , fieldPosition.position.getBottom()); + + // 计算字段的宽度和高度 + float fieldWidth = fieldPosition.position.getRight() - fieldPosition.position.getLeft(); + float fieldHeight = fieldPosition.position.getTop() - fieldPosition.position.getBottom(); + + // 生成二维码 + QRCodeWriter qrCodeWriter = new QRCodeWriter(); + Map hints = new HashMap<>(); + hints.put(EncodeHintType.MARGIN, 0); + BitMatrix bitMatrix = qrCodeWriter.encode(fieldValue, BarcodeFormat.QR_CODE, (int) fieldWidth, (int) fieldHeight, hints); + + // 将 BitMatrix 转换为 BufferedImage + BufferedImage bufferedImage = toBufferedImage(bitMatrix); + + // 将 BufferedImage 转换为 iText Image + Image image128 = Image.getInstance(bufferedImage, null); + + // 扩大图像尺寸为原来的两倍 + float imageWidth = image128.getWidth(); + float imageHeight = image128.getHeight(); + image128.scaleAbsolute(imageWidth, imageHeight); + + // 设置图像的位置 + float posX = fieldPosition.position.getLeft() + (fieldWidth - imageWidth) / 2; + float posY = fieldPosition.position.getBottom() - (imageHeight - fieldHeight) / 2; + image128.setAbsolutePosition(posX, posY); + // 获取PDF的第一页 PdfContentByte cb = stamper.getOverContent(1); cb.addImage(image128); } + private static BufferedImage toBufferedImage(BitMatrix matrix) { + int width = matrix.getWidth(); + int height = matrix.getHeight(); + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); + } + } + return image; + } + private static void setCheckBox(AcroFields form, PdfStamper stamper, String fieldName) throws DocumentException, IOException { // 获取复选框位置以便在旁边绘制勾号 Rectangle boxRect = form.getFieldPositions(fieldName).get(0).position; @@ -296,10 +334,10 @@ public class HwPrintUtil { //设置打印方向 PORTRAIT 竖向 REVERSE_LANDSCAPE 横向 pageFormat.setOrientation(PageFormat.PORTRAIT); Paper paper = new Paper(); - // 纸的大小 - paper.setSize(221, 200); + // TODO 纸的大小 + paper.setSize(160, 140); // Print 区域 - paper.setImageableArea(2, -15, 221, 200); + paper.setImageableArea(2, -15, 160, 140); pageFormat.setPaper(paper); Book book = new Book(); PDFPrintable printable = new PDFPrintable(document, Scaling.SCALE_TO_FIT);