兴安pdf文件优化
This commit is contained in:
@@ -8,6 +8,8 @@ import com.itextpdf.text.pdf.PdfPageEventHelper;
|
||||
import com.itextpdf.text.pdf.PdfWriter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* 水印处理
|
||||
*/
|
||||
@@ -27,25 +29,25 @@ public class PdfWatermark extends PdfPageEventHelper {
|
||||
@Override
|
||||
public void onEndPage(PdfWriter writer, Document document) {
|
||||
try {
|
||||
// 加载公章图片
|
||||
Image seal = Image.getInstance(watermarkUrl);
|
||||
// 加载水印图片
|
||||
Image watermark = Image.getInstance(new URL(watermarkUrl));
|
||||
|
||||
// 设置水印位置(右下角)
|
||||
float x = document.right() - seal.getScaledWidth() - 50;
|
||||
float y = document.bottom() + 50;
|
||||
// 调整图片大小(保持比例)
|
||||
watermark.scaleToFit(100, 100);
|
||||
|
||||
// 计算位置(确保在页面内)
|
||||
float x = document.right() - watermark.getScaledWidth() - 20;
|
||||
float y = document.bottom() + watermark.getScaledHeight() - 50;
|
||||
|
||||
watermark.setAbsolutePosition(x, y);
|
||||
|
||||
// 设置透明度
|
||||
PdfContentByte canvas = writer.getDirectContentUnder();
|
||||
seal.setAbsolutePosition(x, y);
|
||||
seal.scaleAbsolute(100, 100); // 调整大小
|
||||
canvas.saveState();
|
||||
PdfGState gs = new PdfGState();
|
||||
gs.setFillOpacity(0.5f); // 设置透明度
|
||||
gs.setFillOpacity(1f); // 调整为半透明
|
||||
canvas.setGState(gs);
|
||||
canvas.addImage(seal);
|
||||
canvas.restoreState();
|
||||
canvas.addImage(watermark);
|
||||
} catch (Exception e) {
|
||||
log.error("添加水印异常:", e);
|
||||
log.error("添加水印失败: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,11 +56,13 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import com.itextpdf.text.Image;
|
||||
import com.itextpdf.text.pdf.BarcodeQRCode;
|
||||
import com.itextpdf.text.pdf.PdfContentByte;
|
||||
import com.itextpdf.text.pdf.PdfGState;
|
||||
import com.itextpdf.text.pdf.PdfPageEventHelper;
|
||||
@@ -587,29 +589,57 @@ public class VisCarryStuffController extends BaseController {
|
||||
Font contentFont = new Font(bfChinese, 10, Font.NORMAL);
|
||||
Font titleFont = new Font(bfChinese, 14, Font.BOLD);
|
||||
|
||||
// 上半部分:表单样式
|
||||
// 创建主表格(2列:左边表单,右边二维码)
|
||||
PdfPTable mainTable = new PdfPTable(2);
|
||||
mainTable.setWidthPercentage(100);
|
||||
mainTable.setSpacingBefore(10f);
|
||||
|
||||
// 左边列:表单数据
|
||||
PdfPTable leftColumn = new PdfPTable(1);
|
||||
leftColumn.setWidthPercentage(100);
|
||||
|
||||
// 添加标题
|
||||
Paragraph title = new Paragraph("出门证详情", titleFont);
|
||||
title.setAlignment(Element.ALIGN_CENTER);
|
||||
document.add(title);
|
||||
document.add(new Paragraph("\n"));
|
||||
leftColumn.addCell(createCell(title, Element.ALIGN_CENTER));
|
||||
leftColumn.addCell(createCell(new Paragraph("\n"), Element.ALIGN_CENTER));
|
||||
|
||||
// 创建表单表格(4列,每行两个字段)
|
||||
PdfPTable formTable = new PdfPTable(4);
|
||||
// 添加表单内容
|
||||
PdfPTable formTable = new PdfPTable(2);
|
||||
formTable.setWidthPercentage(90);
|
||||
formTable.setSpacingBefore(10f);
|
||||
formTable.setHorizontalAlignment(Element.ALIGN_CENTER);
|
||||
|
||||
// 添加表单内容(每行两个字段)
|
||||
addFormRow(formTable, "出门证编号:", visExitOutVo.getExitPermitNo(), contentFont);
|
||||
addFormRow(formTable, "申请人姓名:", visExitOutVo.getName(), contentFont);
|
||||
addFormRow(formTable, "申请单位:", visExitOutVo.getVisitingUnit(), contentFont);
|
||||
addFormRow(formTable, "申请日期:", DateUtil.format(visExitOutVo.getVisTime(), DatePattern.NORM_DATE_PATTERN), contentFont);
|
||||
addFormRow(formTable, "事由:", visExitOutVo.getReason(), contentFont);
|
||||
|
||||
document.add(formTable);
|
||||
leftColumn.addCell(createCell(formTable, Element.ALIGN_CENTER));
|
||||
leftColumn.addCell(createCell(new Paragraph("\n\n"), Element.ALIGN_CENTER));
|
||||
|
||||
// 右边列:二维码
|
||||
PdfPTable rightColumn = new PdfPTable(1);
|
||||
rightColumn.setWidthPercentage(100);
|
||||
|
||||
// 生成二维码图片
|
||||
BarcodeQRCode qrCode = new BarcodeQRCode(code, 100, 100, null);
|
||||
Image qrCodeImage = qrCode.getImage();
|
||||
qrCodeImage.scaleAbsolute(100, 100);
|
||||
qrCodeImage.setAlignment(Element.ALIGN_CENTER);
|
||||
|
||||
// 添加二维码到右边列
|
||||
rightColumn.addCell(createCell(qrCodeImage, Element.ALIGN_CENTER));
|
||||
rightColumn.addCell(createCell(new Paragraph("出门证二维码", contentFont), Element.ALIGN_CENTER));
|
||||
|
||||
// 将左右列添加到主表格
|
||||
mainTable.addCell(createCell(leftColumn, Element.ALIGN_LEFT));
|
||||
mainTable.addCell(createCell(rightColumn, Element.ALIGN_RIGHT));
|
||||
|
||||
document.add(mainTable);
|
||||
document.add(new Paragraph("\n\n"));
|
||||
|
||||
// 下半部分:表格数据
|
||||
// 下半部分:物资信息表格保持不变
|
||||
Paragraph tableTitle = new Paragraph("物资信息", titleFont);
|
||||
tableTitle.setAlignment(Element.ALIGN_CENTER);
|
||||
document.add(tableTitle);
|
||||
@@ -646,6 +676,32 @@ public class VisCarryStuffController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
// 创建带图片的单元格
|
||||
private PdfPCell createCell(Image image, int alignment) {
|
||||
PdfPCell cell = new PdfPCell(image);
|
||||
cell.setHorizontalAlignment(alignment);
|
||||
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
cell.setBorder(Rectangle.NO_BORDER);
|
||||
return cell;
|
||||
}
|
||||
|
||||
// 创建带对齐方式的单元格
|
||||
private PdfPCell createCell(Phrase phrase, int alignment) {
|
||||
PdfPCell cell = new PdfPCell(phrase);
|
||||
cell.setBorder(Rectangle.NO_BORDER);
|
||||
cell.setHorizontalAlignment(alignment);
|
||||
return cell;
|
||||
}
|
||||
|
||||
// 创建带对齐方式的表格单元格
|
||||
private PdfPCell createCell(PdfPTable table, int alignment) {
|
||||
PdfPCell cell = new PdfPCell(table);
|
||||
cell.setBorder(Rectangle.NO_BORDER);
|
||||
cell.setHorizontalAlignment(alignment);
|
||||
return cell;
|
||||
}
|
||||
|
||||
|
||||
// 添加表单行(两个字段)
|
||||
private void addFormRow(PdfPTable table, String label1, String value1, Font font) {
|
||||
// 第一个字段标签
|
||||
|
||||
Reference in New Issue
Block a user