|
|
@@ -25,25 +25,88 @@ public class PdfSignUtil {
|
|
|
private static final String SIGN_IMG = "d:/吴明华签字章.png";
|
|
|
private static final String OUT_PDF = "d:/new造价工程师证书.pdf";*/
|
|
|
|
|
|
- private static final float SIGN_WIDTH = 70;
|
|
|
- private static final float SIGN_HEIGHT = 25;
|
|
|
- private static final float SIGN_X = 180;
|
|
|
- private static final float SIGN_Y = 110;
|
|
|
+ //签字章位置
|
|
|
+ //private static final float SIGN_WIDTH = 70;
|
|
|
+ //private static final float SIGN_HEIGHT = 25;
|
|
|
+ //private static final float SIGN_X = 180;
|
|
|
+ //private static final float SIGN_Y = 110;
|
|
|
|
|
|
- //private static final String DATE_TEMP_PNG = "d:/date_temp.png";
|
|
|
- private static final float DATE_X = 180;
|
|
|
- private static final float DATE_Y = 80;
|
|
|
- private static final float DATE_WIDTH = 80;
|
|
|
- private static final float DATE_HEIGHT = 30;
|
|
|
+ //日期位置
|
|
|
+ //private static final float DATE_X = 180;
|
|
|
+ //private static final float DATE_Y = 80;
|
|
|
+ //private static final float DATE_WIDTH = 80;
|
|
|
+ //private static final float DATE_HEIGHT = 30;
|
|
|
|
|
|
// 字体文件放在当前类同级目录
|
|
|
private static final String HAND_FONT = PdfSignUtil.class.getResource("font.ttf").getPath();
|
|
|
|
|
|
+ // ==============================================
|
|
|
+ // 坐标配置类:根据 type 切换多套坐标
|
|
|
+ // type 161:一造,151:一建,181:监理
|
|
|
+ // ==============================================
|
|
|
+ private static class SignPosition {
|
|
|
+ // 签章
|
|
|
+ float signWidth;
|
|
|
+ float signHeight;
|
|
|
+ float signX;
|
|
|
+ float signY;
|
|
|
+ // 日期
|
|
|
+ float dateX;
|
|
|
+ float dateY;
|
|
|
+ float dateWidth;
|
|
|
+ float dateHeight;
|
|
|
+
|
|
|
+ public SignPosition(float signWidth, float signHeight, float signX, float signY,
|
|
|
+ float dateX, float dateY, float dateWidth, float dateHeight) {
|
|
|
+ this.signWidth = signWidth;
|
|
|
+ this.signHeight = signHeight;
|
|
|
+ this.signX = signX;
|
|
|
+ this.signY = signY;
|
|
|
+ this.dateX = dateX;
|
|
|
+ this.dateY = dateY;
|
|
|
+ this.dateWidth = dateWidth;
|
|
|
+ this.dateHeight = dateHeight;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 161 一造坐标(你原来默认的那套)
|
|
|
+ private static final SignPosition POS_161 = new SignPosition(
|
|
|
+ 70, // SIGN_WIDTH
|
|
|
+ 25, // SIGN_HEIGHT
|
|
|
+ 180, // SIGN_X
|
|
|
+ 110, // SIGN_Y
|
|
|
+ 180, // DATE_X
|
|
|
+ 80, // DATE_Y
|
|
|
+ 80, // DATE_WIDTH
|
|
|
+ 30 // DATE_HEIGHT
|
|
|
+ );
|
|
|
+
|
|
|
+ // 151 一建坐标(第二套,自行修改数值)
|
|
|
+ private static final SignPosition POS_151 = new SignPosition(
|
|
|
+ 70, 25, 250, 80,
|
|
|
+ 250, 50, 80, 30
|
|
|
+ );
|
|
|
+
|
|
|
+ // 181 监理坐标(第三套,自行修改数值)
|
|
|
+ private static final SignPosition POS_181 = new SignPosition(
|
|
|
+ 70, 25, 240, 100,
|
|
|
+ 240, 60, 80, 30
|
|
|
+ );
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
|
//disposePdfFile(new File(SRC_PDF), new File(SIGN_IMG), "d:/");
|
|
|
}
|
|
|
|
|
|
- public static Map<String,Object> disposePdfFile(File file, File signFile, String path) {
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @param signFile
|
|
|
+ * @param path
|
|
|
+ * @param type 处理的资质类型:161:一造,151:一建,181:监理
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String,Object> disposePdfFile(File file, File signFile, String path, String type) {
|
|
|
Map<String,Object> map = new HashMap<String,Object>();
|
|
|
PDDocument doc = null;
|
|
|
try {
|
|
|
@@ -92,6 +155,9 @@ public class PdfSignUtil {
|
|
|
map.put("certValidStart", certValidStart);
|
|
|
map.put("certValidEnd", certValidEnd);
|
|
|
|
|
|
+ // 根据类型获取对应坐标
|
|
|
+ SignPosition pos = getPositionByType(type);
|
|
|
+
|
|
|
// 下面原有逻辑全部不动
|
|
|
String today = new SimpleDateFormat("yyyy.MM.dd").format(new Date());
|
|
|
generateHandWritingDate(today, signFile, path + "/date_temp.png");
|
|
|
@@ -101,8 +167,8 @@ public class PdfSignUtil {
|
|
|
PDImageXObject dateImage = PDImageXObject.createFromFile(path + "/date_temp.png", doc);
|
|
|
|
|
|
PDPageContentStream cs = new PDPageContentStream(doc, page, true, true, true);
|
|
|
- cs.drawImage(signImage, SIGN_X, SIGN_Y, SIGN_WIDTH, SIGN_HEIGHT);
|
|
|
- cs.drawImage(dateImage, DATE_X, DATE_Y, DATE_WIDTH, DATE_HEIGHT);
|
|
|
+ cs.drawImage(signImage, pos.signX, pos.signY, pos.signWidth, pos.signHeight);
|
|
|
+ cs.drawImage(dateImage, pos.dateX, pos.dateY, pos.dateWidth, pos.dateHeight);
|
|
|
cs.close();
|
|
|
|
|
|
// 动态生成输出路径
|
|
|
@@ -125,6 +191,22 @@ public class PdfSignUtil {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据资质类型获取对应签章/日期坐标
|
|
|
+ */
|
|
|
+ private static SignPosition getPositionByType(String type) {
|
|
|
+ switch (type) {
|
|
|
+ case "161":
|
|
|
+ return POS_161;
|
|
|
+ case "151":
|
|
|
+ return POS_151;
|
|
|
+ case "181":
|
|
|
+ return POS_181;
|
|
|
+ default:
|
|
|
+ return POS_161; // 默认一造
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 生成日期:直采签字原色、多层叠加加深、防割裂
|
|
|
private static void generateHandWritingDate(String date, File signFile, String outPath) throws Exception {
|
|
|
BufferedImage signImg = ImageIO.read(signFile);
|
|
|
@@ -133,27 +215,34 @@ public class PdfSignUtil {
|
|
|
|
|
|
File fontFile = new File(HAND_FONT);
|
|
|
FileInputStream fontIn = new FileInputStream(fontFile);
|
|
|
- Font handFont = Font.createFont(Font.TRUETYPE_FONT, fontIn).deriveFont(Font.BOLD,22f);
|
|
|
+ // 字号你已经设为28,我就用28
|
|
|
+ Font handFont = Font.createFont(Font.TRUETYPE_FONT, fontIn).deriveFont(Font.BOLD, 22f);
|
|
|
fontIn.close();
|
|
|
|
|
|
- BufferedImage img = new BufferedImage(130, 30, BufferedImage.TYPE_INT_ARGB);
|
|
|
- Graphics2D g2d = img.createGraphics();
|
|
|
+ // 注意:这里宽度160是为了容纳加粗后的字体,防止截断
|
|
|
+ BufferedImage img = new BufferedImage(160, 40, BufferedImage.TYPE_INT_ARGB);
|
|
|
+ Graphics2D g2d = img.createGraphics(); // 修正:变量名统一为 g2d
|
|
|
|
|
|
g2d.setComposite(AlphaComposite.Clear);
|
|
|
- g2d.fillRect(0,0,130,30);
|
|
|
+ g2d.fillRect(0, 0, 160, 40);
|
|
|
g2d.setComposite(AlphaComposite.SrcOver);
|
|
|
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
|
|
|
- g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
|
|
|
|
|
|
g2d.setColor(realInkColor);
|
|
|
g2d.setFont(handFont);
|
|
|
|
|
|
- g2d.drawString(date, 2, 22);
|
|
|
- g2d.drawString(date, 2, 22);
|
|
|
- g2d.drawString(date, 2, 22);
|
|
|
+ // 核心修复:使用 g2d 变量,并添加偏移量实现超级加粗
|
|
|
+ // 左右上下偏移叠加,保证非常黑、非常粗
|
|
|
+ // 核心:只保留 3层叠加,粗细适中
|
|
|
+ g2d.drawString(date, 1, 25);
|
|
|
+ g2d.drawString(date, 2, 24);
|
|
|
+ g2d.drawString(date, 2, 25);
|
|
|
+
|
|
|
+ // 最后画一层主字,保证清晰
|
|
|
+ g2d.drawString(date, 2, 25);
|
|
|
|
|
|
g2d.dispose();
|
|
|
ImageIO.write(img, "png", new File(outPath));
|