消息
This commit is contained in:
@ -224,6 +224,21 @@ public class PdfBoxQrCodeGenerator {
|
||||
// 二维码大小比例(页面宽高中较小值的20%)
|
||||
private static final float QR_SIZE_RATIO = 0.2f;
|
||||
|
||||
// 二维码大小比例(页面宽高中较小值的20%)
|
||||
private static final float QR_SIZE = 60.0f;
|
||||
|
||||
// 竖坐标
|
||||
private static final float V_QR_X = 232.0f;
|
||||
|
||||
// 竖坐标
|
||||
private static final float V_QR_Y = 679.5f;
|
||||
|
||||
// 横坐标
|
||||
private static final float H_QR_X = 1116.5f;
|
||||
|
||||
// 横坐标
|
||||
private static final float H_QR_Y = 34.0f;
|
||||
|
||||
public static ByteArrayOutputStream addQRCodeToPDFOnAllPages(String srcPdf, byte[] qrCodeBytes, boolean isChangeFile)
|
||||
throws IOException {
|
||||
|
||||
@ -256,6 +271,10 @@ public class PdfBoxQrCodeGenerator {
|
||||
int numberOfPages = pdfDoc.getNumberOfPages();
|
||||
|
||||
for (int pageNum = 1; pageNum <= numberOfPages; pageNum++) {
|
||||
if (pageNum == 1 && isChangeFile) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PdfPage page = pdfDoc.getPage(pageNum);
|
||||
|
||||
// 获取页面的所有边界框信息
|
||||
@ -266,25 +285,33 @@ public class PdfBoxQrCodeGenerator {
|
||||
Rectangle visibleArea = (cropBox != null) ? cropBox : mediaBox;
|
||||
|
||||
// 计算页面宽高中较小的值
|
||||
float minDimension = Math.min(visibleArea.getWidth(), visibleArea.getHeight());
|
||||
// float minDimension = Math.min(visibleArea.getWidth(), visibleArea.getHeight());
|
||||
|
||||
// 动态计算二维码大小(页面宽高中较小值的20%)
|
||||
float qrSize = minDimension * QR_SIZE_RATIO;
|
||||
// float qrSize = minDimension * QR_SIZE_RATIO;
|
||||
|
||||
// 输出页面尺寸信息
|
||||
System.out.println("页面 " + pageNum + " 尺寸: " + visibleArea.getWidth() + "x" + visibleArea.getHeight());
|
||||
System.out.println("页面 " + pageNum + " 二维码大小 (点): " + qrSize);
|
||||
// System.out.println("页面 " + pageNum + " 尺寸: " + visibleArea.getWidth() + "x" + visibleArea.getHeight());
|
||||
// System.out.println("页面 " + pageNum + " 二维码大小 (点): " + qrSize);
|
||||
|
||||
// 计算左上角的位置(PDF坐标系:原点在左下角)
|
||||
float qrX = visibleArea.getLeft() + MARGIN;
|
||||
float qrY = visibleArea.getTop() - qrSize - MARGIN;
|
||||
// float qrX = visibleArea.getLeft() + MARGIN;
|
||||
// float qrY = visibleArea.getTop() - qrSize - MARGIN;
|
||||
|
||||
// 打印二维码位置信息
|
||||
System.out.println("页面 " + pageNum + " 左上角二维码位置: x=" + qrX + ", y=" + qrY);
|
||||
|
||||
// System.out.println("页面 " + pageNum + " 左上角二维码位置: x=" + qrX + ", y=" + qrY);
|
||||
float qrX;
|
||||
float qrY;
|
||||
if (visibleArea.getWidth() > visibleArea.getHeight()) {
|
||||
qrX = H_QR_X;
|
||||
qrY = H_QR_Y;
|
||||
} else {
|
||||
qrX = V_QR_X;
|
||||
qrY = V_QR_Y;
|
||||
}
|
||||
try {
|
||||
// 使用Canvas API添加左上角二维码
|
||||
addQRCodeWithCanvas(pdfDoc, page, qrCodeBytes, qrX, qrY, qrSize);
|
||||
addQRCodeWithCanvas(pdfDoc, page, qrCodeBytes, qrX, qrY, QR_SIZE);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("在页面 " + pageNum + " 添加二维码时出错: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user