|
|
@ -77,22 +77,25 @@ public class PdfModelUitl { |
|
|
|
if(null!=field) { |
|
|
|
Rectangle rtl = field.getWidgets().get(0).getRectangle().toRectangle(); // 获取表单域的xy坐标
|
|
|
|
field.setFont(defaultFont()); |
|
|
|
field.setFontSize(12); |
|
|
|
if(Optional.ofNullable(field.getFontSize()).isEmpty()){ |
|
|
|
field.setFontSize(16); |
|
|
|
} |
|
|
|
if ("QRIMG".equals(item.getParamType())) { |
|
|
|
//渲染Qr图片
|
|
|
|
if(!value.isEmpty() && !value.isBlank()) { |
|
|
|
try { |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(Optional.ofNullable(item.getParamDesc()).orElse("{\"width\":\"172\",\"height\":\"172\",\"type\":\"png\"}")); |
|
|
|
int width = 172; |
|
|
|
int height = 172; |
|
|
|
String type = "png"; |
|
|
|
if (null != jsonObject) { |
|
|
|
width = Integer.valueOf(Optional.ofNullable(jsonObject.getString("width")).orElse("172")); |
|
|
|
height = Integer.valueOf(Optional.ofNullable(jsonObject.getString("height")).orElse("172")); |
|
|
|
type = Optional.ofNullable(jsonObject.getString("type")).orElse("png"); |
|
|
|
} |
|
|
|
BufferedImage bufferedImage = qrCodeService.generateQRCode(value, width, height); |
|
|
|
com.itextpdf.layout.element.Image image = getImage(field, bufferedImage, rtl); |
|
|
|
// JSONObject jsonObject = JSONObject.parseObject(Optional.ofNullable(item.getParamDesc()).orElse("{\"width\":\"172\",\"height\":\"172\",\"type\":\"png\"}"));
|
|
|
|
// int width = 172;
|
|
|
|
// int height = 172;
|
|
|
|
// String type = "png";
|
|
|
|
// if (null != jsonObject) {
|
|
|
|
// width = Integer.valueOf(Optional.ofNullable(jsonObject.getString("width")).orElse("172"));
|
|
|
|
// height = Integer.valueOf(Optional.ofNullable(jsonObject.getString("height")).orElse("172"));
|
|
|
|
// type = Optional.ofNullable(jsonObject.getString("type")).orElse("png");
|
|
|
|
// }
|
|
|
|
BufferedImage bufferedImage = qrCodeService.generateQRCode(value, (int)rtl.getWidth(), (int)rtl.getHeight()); |
|
|
|
byte[] imgBytes = qrCodeService.getImgBuffer("png", bufferedImage); |
|
|
|
com.itextpdf.layout.element.Image image =new com.itextpdf.layout.element.Image(ImageDataFactory.create(imgBytes));//getImage(field, bufferedImage, rtl);
|
|
|
|
image.setFixedPosition(rtl.getX(), rtl.getY(), rtl.getWidth()); // 设置图片固定位置
|
|
|
|
document.add(image); |
|
|
|
} catch (IOException e) { |
|
|
@ -201,19 +204,20 @@ public class PdfModelUitl { |
|
|
|
} |
|
|
|
} |
|
|
|
///*pdf 模版图片压缩处理*/
|
|
|
|
private com.itextpdf.layout.element.Image getImage(PdfFormField formField, BufferedImage bufferedImage,Rectangle rtl) throws IOException { |
|
|
|
byte[] imgBytes = qrCodeService.getImgBuffer("jpeg", bufferedImage); |
|
|
|
private com.itextpdf.layout.element.Image getImage( BufferedImage bufferedImage,Rectangle rtl) throws IOException { |
|
|
|
byte[] imgBytes = qrCodeService.getImgBuffer("png", bufferedImage); |
|
|
|
if (Float.compare(bufferedImage.getWidth(), rtl.getWidth()) <= 0 && Float.compare(bufferedImage.getHeight(), rtl.getHeight()) <= 0) {// 不处理
|
|
|
|
return new com.itextpdf.layout.element.Image(ImageDataFactory.create(imgBytes)); |
|
|
|
} else { |
|
|
|
// 压缩图片。计算得到图片放缩的最大比例
|
|
|
|
float scale = Math.max(bufferedImage.getWidth() / rtl.getWidth(), bufferedImage.getHeight() / rtl.getHeight()); |
|
|
|
float scale = Math.min(rtl.getWidth()/bufferedImage.getWidth() , rtl.getHeight() / bufferedImage.getHeight() ); |
|
|
|
// 压缩图片
|
|
|
|
Image scale1 = ImgUtil.scale(ImgUtil.toImage(imgBytes), scale); |
|
|
|
return new com.itextpdf.layout.element.Image(ImageDataFactory.create(ImgUtil.toBytes(scale1, ImgUtil.IMAGE_TYPE_PNG))); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据表单域关键字查找当前关键字所在页对象(PdfPage) |
|
|
|
* |
|
|
|