package com.connor.pdf; import java.awt.Color; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import com.lowagie.text.DocumentException; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; public class SignPdfLao { public static void main(String[] args) { int pageCount = 1; SignPdfLao signPdf = new SignPdfLao(); String dataPath = args[0]; String pdfPath = args[1]; String outs = args[2]; // String dataPath = "C:\\Users\\infodba\\Desktop\\老世宝签字.txt"; // String pdfPath = "C:\\Users\\infodba\\Desktop\\老世宝PDF\\A4图纸模板.PDF"; // String outs = "C:\\Users\\infodba\\Desktop\\老世宝CAD输出\\A4图纸模板.PDF"; // String dataPath = "C:\\Users\\infodba\\Desktop\\老世宝签字.txt"; // String pdfPath = "C:\\Users\\infodba\\Desktop\\TC模板CATIAPDF\\SHIBAO_A4_页_1.PDF"; // String outs = "C:\\Users\\infodba\\Desktop\\老世宝CAD输出\\SHIBAO_A4_页_1.PDF"; // String dataPath = "C:\\Users\\infodba\\Desktop\\老世宝签字.txt"; // String pdfPath = "C:\\Users\\infodba\\Desktop\\TC模板CATIAPDF\\SHIBAO_SO_A4_页_1.PDF"; // String outs = "C:\\Users\\infodba\\Desktop\\老世宝CAD输出\\SHIBAO_SO_A4_页_1.PDF"; // String dataPath = "C:\\Users\\infodba\\Desktop\\老世宝签字.txt"; // String pdfPath = "C:\\Users\\infodba\\Desktop\\TC模板CATIAPDF\\AA.PDF"; // String outs = "C:\\Users\\infodba\\Desktop\\老世宝CAD输出\\AA.PDF"; // String dataPath = "C:\\Users\\infodba\\Desktop\\老世宝签字.txt"; // String pdfPath = "C:\\Users\\infodba\\Desktop\\TC模板CATIAPDF\\A4图纸模板.PDF"; // String outs = "C:\\Users\\infodba\\Desktop\\老世宝CAD输出\\A4图纸模板.PDF"; Map dataMap = signPdf.getData(dataPath); try { System.out.println("=====开始签字====="); PdfReader reader = new PdfReader(pdfPath); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outs)); Rectangle rectangle = reader.getPageSize(1); Float[] fposition = signPdf.getSize(rectangle); BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true); float x1 = 0.0F; float x2 = 0.0F; float x3 = 0.0F; float x4 = 0.0F; float y1 = 0.0F; float y2 = 0.0F; float y3 = 0.0F; float y4 = 0.0F; if (fposition[0] == 2384.0F && fposition[1] == 3370.0F) { // A0 x1 = 2870.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 2900.0F; x3 = 2971.0F; x4 = 3003.0F; } else if (fposition[0] == 2384.0F && fposition[1] == 1684.0F) { // A1 x1 = 1885.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 1913.0F; x3 = 1985.0F; x4 = 2016.0F; } else if (fposition[0] == 1684.0F && fposition[1] == 1191.0F) { // A2 x1 = 1183.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 1214.0F; x3 = 1285.0F; x4 = 1316.0F; } else if (fposition[0] == 1191.0F && fposition[1] == 842.0F) { // A3 x1 = 703.0F; y1 = 48.0F; y2 = 34.0F; y3 = 20.0F; y4 = 6.0F; x2 = 735.0F; x3 = 805.0F; x4 = 837.0F; } else if (fposition[0] == 842.0F && fposition[1] == 595.0F) { // A4 x1 = 110.0F; y1 = 48.0F; y2 = 34.0F; y3 = 20.0F; y4 = 6.0F; x2 = 142.0F; x3 = 213.0F; x4 = 243.0F; } else if (fposition[0] == 3370.4F && fposition[1] == 2383.94F) { // CATIA A0 x1 = 2870.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 2902.0F; x3 = 2971.0F; x4 = 3003.0F; } else if (fposition[0] == 2383.94F && fposition[1] == 1683.78F) { // CATIA A1 x1 = 1885.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 1914.0F; x3 = 1986.0F; x4 = 2017.0F; } else if (fposition[0] == 1683.78F && fposition[1] == 1190.56F) { // CATIA A2 x1 = 1183.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 1214.0F; x3 = 1285.0F; x4 = 1316.0F; } else if (fposition[0] == 1190.56F && fposition[1] == 841.89F) { // CATIA A3 x1 = 703.0F; y1 = 48.0F; y2 = 34.0F; y3 = 20.0F; y4 = 6.0F; x2 = 735.0F; x3 = 805.0F; x4 = 837.0F; } else if (fposition[0] == 595.28F && fposition[1] == 841.89F) { // CATIA A4 x1 = 110.0F; y1 = 48.0F; y2 = 34.0F; y3 = 20.0F; y4 = 6.0F; x2 = 142.0F; x3 = 213.0F; x4 = 243.0F; } else if (fposition[0] == 5054.18F && fposition[1] == 2383.94F) { // CATIA S0 A0 x1 = 4554.0F; y1 = 61.0F; y2 = 47.0F; y3 = 33.0F; y4 = 19.0F; x2 = 4585.5F; x3 = 4585.5F; x4 = 4585.5F; } else if (fposition[0] == 6740.79F && fposition[1] == 2383.94F) { // CATIA S0 A1 x1 = 6240.0F; y1 = 61.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 6272.0F; x3 = 6272.0F; x4 = 6272.0F; } else if (fposition[0] == 8424.57F && fposition[1] == 2383.94F) { // CATIA S0 A2 x1 = 7925.0F; y1 = 60.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 7957.0F; x3 = 7957.0F; x4 = 7957.0F; } else if (fposition[0] == 10111.19F && fposition[1] == 2383.94F) { // CATIA S0 A3 x1 = 9614.0F; y1 = 60.0F; y2 = 46.0F; y3 = 32.0F; y4 = 18.0F; x2 = 9643.0F; x3 = 9643.0F; x4 = 9643.0F; } else if (fposition[0] == 11794.97F && fposition[1] == 2383.94F) { // CATIA S0 A4 x1 = 11295.0F; y1 = 60.0F; y2 = 46.0F; y3 = 32.0F; y4 = 18.0F; x2 = 11327.0F; x3 = 11327.0F; x4 = 11327.0F; } else if (fposition[0] == 1684.0F && fposition[1] == 2384.0F) { // A1 x1 = 1885.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 1913.0F; x3 = 1985.0F; x4 = 2016.0F; } else if (fposition[0] == 1191.0F && fposition[1] == 1684.0F) { // A2 x1 = 1183.0F; y1 = 62.0F; y2 = 48.0F; y3 = 34.0F; y4 = 20.0F; x2 = 1214.0F; x3 = 1285.0F; x4 = 1316.0F; } else if (fposition[0] == 842.0F && fposition[1] == 1191.0F) { // A3 x1 = 703.0F; y1 = 48.0F; y2 = 34.0F; y3 = 20.0F; y4 = 6.0F; x2 = 735.0F; x3 = 805.0F; x4 = 837.0F; } else if (fposition[0] == 595.0F && fposition[1] == 842.0F) { // A4 x1 = 110.0F; y1 = 48.0F; y2 = 34.0F; y3 = 20.0F; y4 = 6.0F; x2 = 142.0F; x3 = 213.0F; x4 = 243.0F; } if (dataMap.containsKey("编制")) { String value = (String) dataMap.get("编制"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 9.0F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x1).floatValue(), Float.valueOf(y1).floatValue()); content.showText(value); content.endText(); // signPdf.signBZ(pageCount, value, content, reader, stamper, Integer.valueOf(275)); } if (dataMap.containsKey("校对")) { String value = (String) dataMap.get("校对"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 9.0F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x1).floatValue(), Float.valueOf(y2).floatValue()); content.showText(value); content.endText(); // signPdf.signBZ(pageCount, value, content, reader, stamper, Integer.valueOf(476)); } if (dataMap.containsKey("会签")) { String value = (String) dataMap.get("会签"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 9.0F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x1).floatValue(), Float.valueOf(y3).floatValue()); content.showText(value); content.endText(); } if (dataMap.containsKey("批准")) { String value = (String) dataMap.get("批准"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 9.0F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x1).floatValue(), Float.valueOf(y4).floatValue()); content.showText(value); content.endText(); } if (dataMap.containsKey("编制日期")) { String value = (String) dataMap.get("编制日期"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 6.2F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x2).floatValue(), Float.valueOf(y1).floatValue()); content.showText(value); content.endText(); } if (dataMap.containsKey("校对日期")) { String value = (String) dataMap.get("校对日期"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 6.2F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x2).floatValue(), Float.valueOf(y2).floatValue()); content.showText(value); content.endText(); } if (dataMap.containsKey("会签日期")) { String value = (String) dataMap.get("会签日期"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 6.2F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x2).floatValue(), Float.valueOf(y3).floatValue()); content.showText(value); content.endText(); } if (dataMap.containsKey("批准日期")) { String value = (String) dataMap.get("批准日期"); PdfContentByte content = stamper.getUnderContent(1); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 6.2F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(x2).floatValue(), Float.valueOf(y4).floatValue()); content.showText(value); content.endText(); } stamper.close(); reader.close(); System.out.println("=====签字结束====="); } catch (Exception e) { e.printStackTrace(); } } public Map getData(String filePath) { String lineTxt = ""; Map dataMap = new HashMap<>(); try { String encoding = "GBK"; File file = new File(filePath); if ((file.isFile()) && (file.exists())) { InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); BufferedReader bufferedReader = new BufferedReader(read); lineTxt = bufferedReader.readLine(); System.out.println("linttxt=======" + lineTxt); read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } String[] dataList = null; if ((lineTxt != null) && (lineTxt.length() > 0) && (lineTxt.split("\\|") != null)) dataList = lineTxt.split("\\|"); else { dataList[0] = ""; } for (int i = 0; i < dataList.length; i++) { System.out.println("dataList=======" + dataList[i]); String[] datas = null; datas = dataList[i].split("="); if ((datas != null) && (datas.length > 1)) { dataMap.put(datas[0], datas[1]); } } return dataMap; } public Float[] getSize(Rectangle rectangle) { Float[] sizef = new Float[2]; float toleft = rectangle.getLeft(); float tobottom = rectangle.getBottom(); float toright = rectangle.getRight(); float totop = rectangle.getTop(); JFomPdfBean bean = new JFomPdfBean(); bean.setH(Float.valueOf(toright - toleft)); bean.setW(Float.valueOf(totop - tobottom)); sizef[0] = bean.getH(); sizef[1] = bean.getW(); System.out.println("bean.getH------------>................." + bean.getH()); System.out.println("bean.getW------------>................." + bean.getW()); return sizef; } public void signBZ(int pageCount, String value, PdfContentByte content, PdfReader reader, PdfStamper stamper, Integer x) { for (int i = 2; i <= pageCount; i++) { int width = x.intValue(); SignPdfLao pdf = new SignPdfLao(); Rectangle rectangle = reader.getPageSize(i); System.out.println("=========第" + i + "页========="); Float[] fposition = pdf.getSize(rectangle); try { BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true); String[] values = value.split(" "); if (values != null) for (int j = 0; j < values.length; j++) { content = stamper.getUnderContent(i); content.beginText(); content.setColorFill(Color.BLACK); content.setFontAndSize(base, 11.0F); content.setTextRise(14.0F); content.setTextMatrix(Float.valueOf(width).floatValue(), Float.valueOf(30.0F).floatValue()); content.showText(values[j]); content.endText(); width += 63; } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }