Commit 8e4417e9 authored by 徐俊's avatar 徐俊

xujun

parent eedbce92
...@@ -5,6 +5,7 @@ import com.itextpdf.kernel.font.PdfFontFactory; ...@@ -5,6 +5,7 @@ import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.text.*; import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.*;
import com.yiboshi.science.entity.SystemParameter; import com.yiboshi.science.entity.SystemParameter;
import com.yiboshi.science.enumeration.CommonEnum;
import com.yiboshi.science.param.dto.*; import com.yiboshi.science.param.dto.*;
import java.io.*; import java.io.*;
...@@ -23,8 +24,12 @@ public class ProjectInfoToPDF { ...@@ -23,8 +24,12 @@ public class ProjectInfoToPDF {
public static void generateProjectPDF(ComProjectDTO project, String outputPath, List<SystemParameter> projAttributeList) throws DocumentException, IOException { public static void generateProjectPDF(ComProjectDTO project, String outputPath, List<SystemParameter> projAttributeList) throws DocumentException, IOException {
Document document = new Document(PageSize.A4); Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPath)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPath));
document.open(); // 文件水印
String watermark = project.getAppNo();
if (project.getProjState() >= CommonEnum.projState.pass.getCode()) {
watermark = project.getProjNo();
}
// 宋体 // 宋体
BaseFont bfChinese = loadChineseFont("/fonts/simsun.ttc"); BaseFont bfChinese = loadChineseFont("/fonts/simsun.ttc");
// 仿宋 // 仿宋
...@@ -32,6 +37,13 @@ public class ProjectInfoToPDF { ...@@ -32,6 +37,13 @@ public class ProjectInfoToPDF {
// WINGDNG2 // WINGDNG2
BaseFont WINGDNG2 = loadChineseFont("/fonts/WINGDNG2.ttf"); BaseFont WINGDNG2 = loadChineseFont("/fonts/WINGDNG2.ttf");
// 添加水印
writer.setPageEvent(new WatermarkPageEvent(watermark, fsChinese));
document.open();
Font normalFont = new Font(bfChinese, 12, Font.NORMAL); Font normalFont = new Font(bfChinese, 12, Font.NORMAL);
Font boldFont = new Font(bfChinese, 12, Font.BOLD); Font boldFont = new Font(bfChinese, 12, Font.BOLD);
...@@ -854,4 +866,74 @@ public class ProjectInfoToPDF { ...@@ -854,4 +866,74 @@ public class ProjectInfoToPDF {
unitSignature.setSpacingBefore(30); unitSignature.setSpacingBefore(30);
document.add(unitSignature); document.add(unitSignature);
} }
/**
* 水印页面事件
*/
private static class WatermarkPageEvent extends PdfPageEventHelper {
private String watermarkText;
private BaseFont baseFont;
public WatermarkPageEvent(String watermarkText, BaseFont baseFont) {
this.watermarkText = watermarkText;
this.baseFont = baseFont;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
PdfContentByte canvas = writer.getDirectContentUnder();
Rectangle pageSize = document.getPageSize();
float width = pageSize.getWidth();
float height = pageSize.getHeight();
// 设置水印字体
canvas.saveState();
canvas.beginText();
canvas.setFontAndSize(baseFont, 30); // 减小字体大小
canvas.setGrayFill(0.9f);
// 计算水印间距
float xStep = width / 2; // 水平间距
float yStep = height / 3; // 垂直间距
// 在页面上添加多个水印
for (float y = yStep/2; y < height; y += yStep) {
for (float x = xStep/2; x < width; x += xStep) {
canvas.showTextAligned(Element.ALIGN_CENTER,
watermarkText,
x,
y,
45);
}
}
canvas.endText();
canvas.restoreState();
// 添加页码
PdfContentByte canvasOver = writer.getDirectContent();
canvasOver.saveState();
canvasOver.beginText();
canvasOver.setFontAndSize(baseFont, 12); // 设置页码字体大小
canvasOver.setColorFill(BaseColor.BLACK);
// 页码文本
String text = String.format("第 %d 页", writer.getPageNumber());
// 在页面底部居中添加页码
canvasOver.showTextAligned(Element.ALIGN_CENTER,
text,
width / 2, // 页面中心
15, // 距离底部15单位,降低页码位置
0); // 不旋转
canvasOver.endText();
canvasOver.restoreState();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment