change - 打印工具类去除小黑点

master
yinq 4 months ago
parent 1baec40d07
commit 977d455677

@ -1,5 +1,10 @@
package com.hw.printer.utils; 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.common.core.exception.ServiceException;
import com.hw.printer.api.domain.vo.PrintContentVo; import com.hw.printer.api.domain.vo.PrintContentVo;
import com.itextpdf.text.Document; import com.itextpdf.text.Document;
@ -17,6 +22,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.print.PrintService; import javax.print.PrintService;
import javax.print.PrintServiceLookup; import javax.print.PrintServiceLookup;
import java.awt.image.BufferedImage;
import java.awt.print.*; import java.awt.print.*;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -24,6 +30,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
@ -189,22 +196,53 @@ public class HwPrintUtil {
return null; 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); 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); float fieldWidth = fieldPosition.position.getRight() - fieldPosition.position.getLeft();
// 生成二维码图像 float fieldHeight = fieldPosition.position.getTop() - fieldPosition.position.getBottom();
Image image128 = pdf417.getImage();
// 条码位置 // 生成二维码
image128.setAbsolutePosition(fieldPosition.position.getLeft() + (fieldPosition.position.getRight() - fieldPosition.position.getLeft() - width) / 2 QRCodeWriter qrCodeWriter = new QRCodeWriter();
, fieldPosition.position.getBottom()); Map<EncodeHintType, Object> 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的第一页 // 获取PDF的第一页
PdfContentByte cb = stamper.getOverContent(1); PdfContentByte cb = stamper.getOverContent(1);
cb.addImage(image128); 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 { private static void setCheckBox(AcroFields form, PdfStamper stamper, String fieldName) throws DocumentException, IOException {
// 获取复选框位置以便在旁边绘制勾号 // 获取复选框位置以便在旁边绘制勾号
Rectangle boxRect = form.getFieldPositions(fieldName).get(0).position; Rectangle boxRect = form.getFieldPositions(fieldName).get(0).position;
@ -296,10 +334,10 @@ public class HwPrintUtil {
//设置打印方向 PORTRAIT 竖向 REVERSE_LANDSCAPE 横向 //设置打印方向 PORTRAIT 竖向 REVERSE_LANDSCAPE 横向
pageFormat.setOrientation(PageFormat.PORTRAIT); pageFormat.setOrientation(PageFormat.PORTRAIT);
Paper paper = new Paper(); Paper paper = new Paper();
// 纸的大小 // TODO 纸的大小
paper.setSize(221, 200); paper.setSize(160, 140);
// Print 区域 // Print 区域
paper.setImageableArea(2, -15, 221, 200); paper.setImageableArea(2, -15, 160, 140);
pageFormat.setPaper(paper); pageFormat.setPaper(paper);
Book book = new Book(); Book book = new Book();
PDFPrintable printable = new PDFPrintable(document, Scaling.SCALE_TO_FIT); PDFPrintable printable = new PDFPrintable(document, Scaling.SCALE_TO_FIT);

Loading…
Cancel
Save