|
|
@ -234,11 +234,11 @@ public class PdfBaseUtil { |
|
|
private String signText; |
|
|
private String signText; |
|
|
private String headerText; |
|
|
private String headerText; |
|
|
private BaseFont baseFont; |
|
|
private BaseFont baseFont; |
|
|
private Boolean showHaderLine=true; |
|
|
|
|
|
|
|
|
private Boolean showHaderLine = true; |
|
|
private int totalPages = 0; |
|
|
private int totalPages = 0; |
|
|
private PdfTemplate total; |
|
|
private PdfTemplate total; |
|
|
|
|
|
|
|
|
public TextHeaderEvent(String signText, String headerText,Boolean showHaderLine) throws Exception { |
|
|
|
|
|
|
|
|
public TextHeaderEvent(String signText, String headerText, Boolean showHaderLine) throws Exception { |
|
|
try { |
|
|
try { |
|
|
this.showHaderLine = showHaderLine; |
|
|
this.showHaderLine = showHaderLine; |
|
|
this.signText = signText; |
|
|
this.signText = signText; |
|
|
@ -283,14 +283,14 @@ public class PdfBaseUtil { |
|
|
float pageHeight = pageSize.getHeight(); |
|
|
float pageHeight = pageSize.getHeight(); |
|
|
|
|
|
|
|
|
// 设置页眉参数 |
|
|
// 设置页眉参数 |
|
|
float topMargin = 30; // 顶部边距 |
|
|
|
|
|
|
|
|
float topMargin = 15; // 顶部边距 |
|
|
float fontSize = 12; // 字体大小 |
|
|
float fontSize = 12; // 字体大小 |
|
|
|
|
|
|
|
|
// 计算文字宽度(用于居中) |
|
|
// 计算文字宽度(用于居中) |
|
|
float textWidth = baseFont.getWidthPoint(signText, fontSize); |
|
|
float textWidth = baseFont.getWidthPoint(signText, fontSize); |
|
|
|
|
|
|
|
|
// 计算居中位置 |
|
|
// 计算居中位置 |
|
|
float textX = (pageWidth - textWidth) / 2; |
|
|
|
|
|
|
|
|
float textX = (pageSize.getLeft() + pageSize.getRight()) / 2; |
|
|
float textY = pageHeight - topMargin; |
|
|
float textY = pageHeight - topMargin; |
|
|
|
|
|
|
|
|
// 获取画布 |
|
|
// 获取画布 |
|
|
@ -300,7 +300,7 @@ public class PdfBaseUtil { |
|
|
float y = document.bottom() - 20; |
|
|
float y = document.bottom() - 20; |
|
|
|
|
|
|
|
|
// 在每一页都重新计算,确保位置准确 |
|
|
// 在每一页都重新计算,确保位置准确 |
|
|
String pageText = "第 " + currentPage + " 页 / 共 "; |
|
|
|
|
|
|
|
|
String pageText = signText+ " 第 " + currentPage + " 页 / 共 "; |
|
|
BaseFont baseFont = footerFont.getBaseFont(); |
|
|
BaseFont baseFont = footerFont.getBaseFont(); |
|
|
|
|
|
|
|
|
// 计算页面文本宽度 |
|
|
// 计算页面文本宽度 |
|
|
@ -326,38 +326,43 @@ public class PdfBaseUtil { |
|
|
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, |
|
|
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, |
|
|
new Phrase(" 页", footerFont), startX + pageTextWidth + totalWidth, y, 0); |
|
|
new Phrase(" 页", footerFont), startX + pageTextWidth + totalWidth, y, 0); |
|
|
|
|
|
|
|
|
PdfContentByte content = writer.getDirectContentUnder(); // 在水印层添加 |
|
|
|
|
|
|
|
|
|
|
|
// 设置透明度 |
|
|
|
|
|
PdfGState gs = new PdfGState(); |
|
|
|
|
|
float opacity = Convert.toFloat("0.3"); |
|
|
|
|
|
gs.setFillOpacity(opacity); |
|
|
|
|
|
content.setGState(gs); |
|
|
|
|
|
|
|
|
|
|
|
// 设置字体和颜色 |
|
|
|
|
|
content.setColorFill(BaseColor.RED); |
|
|
|
|
|
content.setFontAndSize(baseFont, fontSize); |
|
|
|
|
|
|
|
|
|
|
|
float width = pageSize.getWidth(); |
|
|
|
|
|
float height = pageSize.getHeight(); |
|
|
|
|
|
|
|
|
|
|
|
// 重复铺满水印 |
|
|
|
|
|
addRepeatedWatermark(content, width, height, signText); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PdfContentByte content = writer.getDirectContentUnder(); // 在水印层添加 |
|
|
|
|
|
// |
|
|
|
|
|
// // 设置透明度 |
|
|
|
|
|
// PdfGState gs = new PdfGState(); |
|
|
|
|
|
// float opacity = Convert.toFloat("0.3"); |
|
|
|
|
|
// gs.setFillOpacity(opacity); |
|
|
|
|
|
// content.setGState(gs); |
|
|
|
|
|
// |
|
|
|
|
|
// // 设置字体和颜色 |
|
|
|
|
|
// content.setColorFill(BaseColor.RED); |
|
|
|
|
|
// content.setFontAndSize(baseFont, fontSize); |
|
|
|
|
|
// |
|
|
|
|
|
// float width = pageSize.getWidth(); |
|
|
|
|
|
// float height = pageSize.getHeight(); |
|
|
|
|
|
// |
|
|
|
|
|
// // 重复铺满水印 |
|
|
|
|
|
// addRepeatedWatermark(content, width, height, signText); |
|
|
// 添加页眉文字 |
|
|
// 添加页眉文字 |
|
|
if(StringUtils.isNoneBlank(headerText)) { |
|
|
|
|
|
canvas.beginText(); |
|
|
|
|
|
canvas.setFontAndSize(baseFont, fontSize); |
|
|
|
|
|
canvas.setTextMatrix(textX, textY); |
|
|
|
|
|
canvas.showText(headerText); |
|
|
|
|
|
canvas.endText(); |
|
|
|
|
|
|
|
|
|
|
|
if(showHaderLine){ |
|
|
|
|
|
|
|
|
if (StringUtils.isNoneBlank(headerText)) { |
|
|
|
|
|
float pageTextWidth1 = baseFont.getWidthPoint(headerText, footerFont.getSize()); |
|
|
|
|
|
float startX1 = (document.getPageSize().getWidth() - pageTextWidth1) / 2; |
|
|
|
|
|
|
|
|
|
|
|
PdfContentByte cb2 = writer.getDirectContent(); |
|
|
|
|
|
ColumnText.showTextAligned(cb2, Element.ALIGN_LEFT, |
|
|
|
|
|
new Phrase(headerText, footerFont), startX1, textY, 0); |
|
|
|
|
|
// canvas.beginText(); |
|
|
|
|
|
// canvas.setFontAndSize(baseFont, fontSize); |
|
|
|
|
|
// canvas.setTextMatrix(textX, textY); |
|
|
|
|
|
// canvas.showText(headerText); |
|
|
|
|
|
// canvas.endText(); |
|
|
|
|
|
if (showHaderLine) { |
|
|
// 添加页眉分隔线(可选) |
|
|
// 添加页眉分隔线(可选) |
|
|
addHeaderLine(canvas, pageWidth, textY - 10); |
|
|
addHeaderLine(canvas, pageWidth, textY - 10); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
@ -374,7 +379,7 @@ public class PdfBaseUtil { |
|
|
private void addRepeatedWatermark(PdfContentByte content, float width, float height, String signText) { |
|
|
private void addRepeatedWatermark(PdfContentByte content, float width, float height, String signText) { |
|
|
// 计算文字尺寸 |
|
|
// 计算文字尺寸 |
|
|
float fontSize = Convert.toFloat("10"); |
|
|
float fontSize = Convert.toFloat("10"); |
|
|
float textWidth = baseFont.getWidthPoint(signText, fontSize)+50; |
|
|
|
|
|
|
|
|
float textWidth = baseFont.getWidthPoint(signText, fontSize) + 50; |
|
|
float textHeight = baseFont.getAscentPoint(signText, fontSize) - |
|
|
float textHeight = baseFont.getAscentPoint(signText, fontSize) - |
|
|
baseFont.getDescentPoint(signText, fontSize); |
|
|
baseFont.getDescentPoint(signText, fontSize); |
|
|
|
|
|
|
|
|
@ -383,7 +388,7 @@ public class PdfBaseUtil { |
|
|
float stepY = textHeight + 100; |
|
|
float stepY = textHeight + 100; |
|
|
|
|
|
|
|
|
// 添加重复水印 |
|
|
// 添加重复水印 |
|
|
Integer two=2; |
|
|
|
|
|
|
|
|
Integer two = 2; |
|
|
for (float x = textWidth / two; x < width; x += stepX) { |
|
|
for (float x = textWidth / two; x < width; x += stepX) { |
|
|
for (float y = textHeight / two; y < height; y += stepY) { |
|
|
for (float y = textHeight / two; y < height; y += stepY) { |
|
|
content.beginText(); |
|
|
content.beginText(); |
|
|
@ -408,8 +413,7 @@ public class PdfBaseUtil { |
|
|
*/ |
|
|
*/ |
|
|
private void addHeaderLine(PdfContentByte canvas, float pageWidth, float yPos) { |
|
|
private void addHeaderLine(PdfContentByte canvas, float pageWidth, float yPos) { |
|
|
canvas.setLineWidth(0.5f); |
|
|
canvas.setLineWidth(0.5f); |
|
|
canvas.moveTo(50, yPos); |
|
|
|
|
|
canvas.lineTo(pageWidth - 50, yPos); |
|
|
|
|
|
|
|
|
canvas.lineTo(pageWidth, yPos); |
|
|
canvas.stroke(); |
|
|
canvas.stroke(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |