main
李建辉 3 months ago
commit 6d2d9e6803

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/core-renderer.jar"/>
<classpathentry kind="lib" path="lib/itext-2.0.3.jar"/>
<classpathentry kind="lib" path="lib/iTextAsian.jar"/>
<classpathentry kind="lib" path="lib/iTextAsianCmaps.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="lib/iText-5.0.2.jar"/>
<classpathentry kind="lib" path="lib/waterPrint.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib/pdfbox-2.0.27.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.connor.pdf</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

2
bin/.gitignore vendored

@ -0,0 +1,2 @@
/com/
/log4j.properties

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,41 @@
package com.connor.jk.pdf.operate;
public class JFomPdfBean {
private Float w = 0.0f;
private Float h = 0.0f;
private Float wi = 0.0f;
private Float hi = 0.0f;
public Float getW() {
return w;
}
public void setW(Float w) {
this.w = w;
}
public Float getH() {
return h;
}
public void setH(Float h) {
this.h = h;
}
public Float getWi() {
return wi;
}
public void setWi(Float wi) {
this.wi = wi;
}
public Float getHi() {
return hi;
}
public void setHi(Float hi) {
this.hi = hi;
}
}

@ -0,0 +1,210 @@
package com.connor.jk.pdf.operate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
import java.io.InputStreamReader;
//import java.util.Properties;
//import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
//import com.itextpdf.text.pdf.PdfReader;
//import com.itextpdf.text.pdf.PdfStamper;
//import com.lowagie.text.DocumentException;
public class JFomPdfOperateMain {
public static 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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>1" + bean.getH());
System.out.println("bean.getW------------>1" + bean.getW());
System.out.println("sizef[0]------------>1" + sizef[0]);
System.out.println("sizef[1]------------>1" + sizef[1]);
return sizef;
}
/**
* Javatxt 1 2
* 3 4readline()
*
* @param filePath
*/
public static void readTxtFile(String filePath) {
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);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
// allInfomation=lineTxt;
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
*
* args[0]txt(txt)"\"应为"\\" args[1]为pdf的完整路径(包括pdf),路径中的"\"应为"\\"
* args[2]pdf(pdf)"\"应为"\\"
*
*/
public static void main(String[] args) {
System.out.println("jinlai2>>>>>>>>>>>>>>>>>>>>>>");
// args=new String[3];
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
// args[0]="C:\\Users\\hezhenquan\\Desktop\\电子签名\\signoff.txt";
// args[1]="C:\\Users\\hezhenquan\\Desktop\\电子签名\\A4-y.pdf";
// args[2]="C:\\Users\\hezhenquan\\Desktop\\电子签名\\qqq.pdf";
String allInfomation = "";
// String filePath = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\signoff.txt";
// "res/";
// readTxtFile(filePath);
// 读取txt的内容
try {
String encoding = "GBK";
// File file = new File(filePath);
File file = new File(args[0]);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
allInfomation = lineTxt;
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
allInfomation = allInfomation.replaceAll("\\\\", "\\\\\\\\");
String[] part = allInfomation.split("\\|");
for (int i = 0; i < part.length; i++) {
System.out.println(part[i]);
}
String[] imgsPath = new String[4];
String[] timeS = new String[4];
// int count=0;
// int count1=0;
for (int i = 0; i < part.length; i++) {
String name = part[i].split("=")[0];
System.out.println("name-------->" + name);
// System.out.println(i);
if (name.equals("批准者")) {
imgsPath[3] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
// count++;
}
if (name.equals("设计者")) {
imgsPath[0] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
// count++;
}
if (name.equals("审核者")) {
imgsPath[2] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
// count++;
}
if (name.equals("标准化")) {
imgsPath[1] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
// count++;
}
if (name.equals("设计时间")) {
timeS[0] = part[i].split("=")[1];
// count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
if (name.equals("标准化时间")) {
timeS[1] = part[i].split("=")[1];
// count1++;
}
if (name.equals("审核时间")) {
timeS[2] = part[i].split("=")[1];
// count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
if (name.equals("批准签名时间")) {
timeS[3] = part[i].split("=")[1];
// count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
}
for (int i = 0; i < timeS.length; i++) {
System.out.println(timeS[i]);
}
for (int i = 0; i < imgsPath.length; i++) {
System.out.println(imgsPath[i]);
}
// PDF 输入文件
// PdfReader reader = null;
// try {
// reader = new PdfReader("C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf", "PDF".getBytes());
// } catch (IOException e) {
// e.printStackTrace();
// }
// String[] imgs = new String[] { "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\白剑国.png",
// "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈超杰.png", "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈冬.png" };
// String[] time = new String[] { "20170612", "20170607", "20170909" };
// String ins = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf";
String ins = args[1];
// ins=ins.replaceAll("\\\\", "\\\\\\\\");
String outs = args[2];
// outs=outs.replaceAll("\\\\", "\\\\\\\\");
// String temp="";
int num = outs.lastIndexOf(".");
String temp = outs.substring(0, num);
// System.out.println(before);
temp = temp + "tempfile.pdf";
JFomPdfOperateMethodUtil.main(ins, temp, imgsPath);
// JFomPdfOperateMethodUtil.main(ins, imgsPath);
// 用来签入时间
try {
Test10.main(timeS, temp, outs);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,208 @@
package com.connor.jk.pdf.operate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
import java.io.InputStreamReader;
//import java.util.Properties;
//import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
//import com.itextpdf.text.pdf.PdfReader;
//import com.itextpdf.text.pdf.PdfStamper;
//import com.lowagie.text.DocumentException;
public class JFomPdfOperateMain_fei {
public static 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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>1" + bean.getH());
System.out.println("bean.getW------------>1" + bean.getW());
System.out.println("sizef[0]------------>1" + sizef[0]);
System.out.println("sizef[1]------------>1" + sizef[1]);
return sizef;
}
/**
* Javatxt 1 2
* 3 4readline()
*
* @param filePath
*/
public static void readTxtFile(String filePath) {
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);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
// allInfomation=lineTxt;
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
*
* args[0]txt(txt)"\"应为"\\" args[1]为pdf的完整路径(包括pdf),路径中的"\"应为"\\"
* args[2]pdf(pdf)"\"应为"\\"
*
*/
public static void main(String[] args) {
System.out.println("jinlai1>>>>>>>>>>>>>>>>>>>>>>");
// args=new String[3];//
System.out.println("11111" + args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
// args[0]="C:\\Users\\hezhenquan\\Desktop\\PDF\\signoff.txt";
// args[1]="C:\\Users\\hezhenquan\\Desktop\\PDF\\ww.pdf";
// args[2]="C:\\Users\\hezhenquan\\Desktop\\PDF\\ppp.pdf";
String allInfomation = "";
// String filePath = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\signoff.txt";
// "res/";
// readTxtFile(filePath);
// 读取txt的内容
try {
String encoding = "GBK";
// File file = new File(filePath);
File file = new File(args[0]);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
allInfomation = lineTxt;
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
// allInfomation=allInfomation.replaceAll("\\\\", "\\\\\\\\");
String[] part = allInfomation.split("\\|");
for (int i = 0; i < part.length; i++) {
System.out.println(part[i]);
}
String[] imgsPath = new String[4];
String[] timeS = new String[4];
int count = 0;
int count1 = 0;
for (int i = 0; i < part.length; i++) {
String name = part[i].split("=")[0];
System.out.println("name-------->" + name);
// System.out.println(i);
if (name.equals("设计者")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("标准化")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("审核者")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("批准者")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("设计时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
if (name.equals("标准化时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
}
if (name.equals("审核时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
if (name.equals("批准签名时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
}
for (int i = 0; i < timeS.length; i++) {
System.out.println(timeS[i]);
}
for (int i = 0; i < imgsPath.length; i++) {
System.out.println(imgsPath[i]);
}
// PDF 输入文件
// PdfReader reader = null;
// try {
// reader = new PdfReader("C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf", "PDF".getBytes());
// } catch (IOException e) {
// e.printStackTrace();
// }
// String[] imgs = new String[] { "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\白剑国.png",
// "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈超杰.png", "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈冬.png" };
// String[] time = new String[] { "20170612", "20170607", "20170909" };
// String ins = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf";
String ins = args[1];
// ins=ins.replaceAll("\\\\", "\\\\\\\\");
String outs = args[2];
// outs=outs.replaceAll("\\\\", "\\\\\\\\");
// String temp="";
// int num=outs.lastIndexOf(".");
// String temp=outs.substring(0, num);
// System.out.println(before);
// temp=temp+"tempfile.pdf";
JFomPdfOperateMethodUtil.main(ins, outs, imgsPath);
// JFomPdfOperateMethodUtil.main(ins, imgsPath);
// 用来签入时间
try {
Test10.main(timeS, outs, outs);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,385 @@
package com.connor.jk.pdf.operate;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class JFomPdfOperateMethodUtil {
public List<JFomPdfBean> beanList;
public static void main(String ins, String temp, String[] imgs) {
System.out.println("jinlai3>>>>>>>>>>>>>>>>>>>>>>");
if (ins == null || temp == null || imgs.length != 4) {
System.out.println("传入的参数有误");
return;
}
JFomPdfOperateMethodUtil util = new JFomPdfOperateMethodUtil();
try {
util.addPdfMark(ins, temp, imgs);
System.out.println(System.getenv("TEMP"));// 打印缓存路径
} catch (Exception e) {
e.printStackTrace();
}
}
public JFomPdfOperateMethodUtil() {
this.beanList = new ArrayList<JFomPdfBean>();
try {
// 获取配置文件中的信息
getBeanMsgList();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* properties
*
* @throws IOException
*/
public void getBeanMsgList() throws IOException {
Properties properties = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("com/connor/jk/pdf/operate/PDFInfo.properties");
properties.load(is);
// properties.getProperty("role_name");
for (int i = 0; true; i++) {
// System.out.println();
String str = properties.getProperty("A" + i);
if (str == null) {
break;
}
String[] values = str.split(",");
if (values != null && values.length == 4) {
JFomPdfBean bean = new JFomPdfBean();
Float fhValue = 0.0f;
Float fwValue = 0.0f;
Float fhiValue = 0.0f;
Float fwiValue = 0.0f;
try {
fhValue = Float.parseFloat(values[0]);
fwValue = Float.parseFloat(values[1]);
fhiValue = Float.parseFloat(values[2]);
fwiValue = Float.parseFloat(values[3]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
bean.setH(fhValue);
bean.setW(fwValue);
bean.setHi(fhiValue);
bean.setWi(fwiValue);
this.beanList.add(bean);
}
}
is.close();
}
// /**
// * 统一按照宽度压缩
// *
// * @param args
// */
// public static float getPercent(float h, float w) {
// float p2 = 10.0f;
// // p2 = 820 / w * 100;
// System.out.println(p2);
// return p2;
// }
/**
* pdf
*
* @param InPdfFile pdf
* @param outPdfFile
* @param markImagePath
* @param pageSize pdfexcelpdfexcel
* reader.getNumberOfPages()pdf
* @throws Exception
*/
public void addPdfMark(String InPdfFile, String temp, String[] markImagePath) throws Exception {
// 插入坐标
Properties properties = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("com/connor/jk/pdf/operate/PDFInfo.properties");
properties.load(is);
// PDF 输入文件
PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());
// PDF 输出文件
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(temp));
// 水印图片
Image img = null;
for (int j = 0; j < markImagePath.length; j++) {
img = Image.getInstance(markImagePath[j]);// 获取图片实例
img.scaleAbsolute(35, 20);// 缩放图片
// float imgW = img.getScaledWidth();// 获取图片的宽度
// float imgH = img.getScaledHeight();// 获取图片的高度
// 获取PDF的页数
int pageSize = reader.getNumberOfPages();// 获取输入文件的页数
for (int i = 1; i <= pageSize; i++) {
Rectangle rectangle = reader.getPageSize(i);
Float[] fposition = getSize(rectangle);
System.out.println("fposition[0]=======" + fposition[0]);
System.out.println("fposition[1]=======" + fposition[1]);
boolean isSizeOk = false;
// 设置水印的插入位置A0
if ((fposition[0] > 3300 && fposition[1] > 2300)) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A0");
String str = properties.getProperty("A0_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A0_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A0_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A0_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A1
if ((fposition[0] > 2300 && fposition[1] > 1600) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A1");
String str = properties.getProperty("A1_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A1_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A1_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A1_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1861, 66);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1974, 66);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1860, 29);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1860, 29);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A2
if ((fposition[1] > 1100 && fposition[0] > 1600) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A2");
String str = properties.getProperty("A2_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A2_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A2_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A2_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1160, 66);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1277, 66);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1160, 29);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1160, 29);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A3
if ((fposition[0] > 1100 && fposition[1] > 800) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A3");
String str = properties.getProperty("A3_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A3_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A3_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A3_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(681, 58);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(798, 58);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(682, 19);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(682, 19);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A4-X
if ((fposition[0] > 800 && fposition[1] > 500) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A4-X");
String str = properties.getProperty("A4X_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A4X_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A4X_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A4X_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(333, 58);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(449, 58);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(332, 19);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 19);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A4-Y
if ((fposition[1] > 800 && fposition[0] > 500) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A4-Y");
String str = properties.getProperty("A4Y_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A4Y_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A4Y_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A4Y_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 58);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(200, 58);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 19);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 19);// 设置水印左下角的坐标
}
}
// 向PDF输出文件中插入水印
PdfContentByte under = stamp.getOverContent(i);// stamp.getUnderContent(i);
PdfGState gs = new PdfGState();
gs.setFillOpacity(1.0f);// 设置透明度为0.2
under.setGState(gs);
under.addImage(img);
}
}
stamp.close();// 关闭
reader.close();// 关闭
// File tempfile = new File(InPdfFile);
// if (tempfile.exists()) {
// // 删除原文件
// // tempfile.delete();
// }
}
/**
* PDF
*
* @param rectangle
* @return
*/
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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>" + bean.getH());
System.out.println("bean.getW------------>" + bean.getW());
return sizef;
}
/**
* 2.8346
*
* @param bean
* @return
*/
public Float[] getPosization(JFomPdfBean bean) {
Float size = 2.8346f;// 1毫米=2.8346磅
Float h = (float) (bean.getH() / size);// 转换PDF的磅为毫米
Float w = (float) (bean.getW() / size);
System.out.println(h + " | " + w);
Float[] sizef = new Float[] { 0.0f, 0.0f };
if (this.beanList != null) {
for (int i = 0; i < this.beanList.size(); i++) {
JFomPdfBean beant = beanList.get(i);
// System.out.println(bean.getH() + " | "+ beant);
if ((beant.getH() - 10) < h && h < (beant.getH() + 10) && (beant.getW() - 10) < w
&& w < (beant.getW() + 10)) {
sizef[0] = beant.getHi() * size;
sizef[1] = beant.getWi() * size;
break;
}
if ((beant.getH() - 10) < w && w < (beant.getH() + 10) && (beant.getW() - 10) < h
&& h < (beant.getW() + 10)) {
sizef[0] = beant.getHi() * size;
sizef[1] = beant.getWi() * size;
break;
}
}
}
return sizef;
}
}

@ -0,0 +1,87 @@
#A4Y
#A4Y=210,297,159.9,202.9
A4Y_1=105, 74
A4Y_2=218, 74
A4Y_3=105, 35
A4Y_4=218, 15
A4Y_a=139, 65
A4Y_b=252, 65
A4Y_c=139,25
A4Y_d=252, 6
#A4X
#A4X=210,297,159.9,202.9
A4X_1=351, 74
A4X_2=465, 74
A4X_3=351, 35
A4X_4=465, 15
A4X_a=386, 65
A4X_b=499, 65
A4X_c=386, 25
A4X_d=499, 7
#the width and height of A4(mm)
#A3
#A3=297,420,280,287
#A3_1=681, 58
A3_1=700, 72
A3_2=813, 72
A3_3=700, 34
A3_4=813, 15
A3_a=735, 65
A3_b=848, 65
A3_c=735, 27
A3_d=848, 7
#A2
#A2=420,594,454,410
A2_1=1179, 89
A2_2=1292, 89
A2_3=1179, 49
A2_4=1292, 30
A2_a=1214, 80
A2_b=1327, 80
A2_c=1214, 40
A2_d=1327, 22
#A1
#A1=594,841,701,584
A1_1=1879,87
A1_2=1992,89
A1_3=1879,49
A1_4=1992,30
A1_a=1914,81
A1_b=2027,81
A1_c=1914,41
A1_d=2027,22
#A0
#A0=841,1189,1039,110
A0_1=2866,87
A0_2=2979,87
A0_3=2866,47
A0_4=2979,29
A0_a=2901,78
A0_b=3014,78
A0_c=2901,38
A0_d=3014,20
#2018.1.24\u65B0\u589E\u56FE\u5E45
#A0-H
#A01
A5=841,1682,1532,821
#A02
A6=841,2378,2228,821
#A03
A7=841,3567,3417,821
#A04
A8=841,4756,4606,821

@ -0,0 +1,86 @@
#A4Y
#A4Y=210,297,159.9,202.9
A4Y_1=88, 58
A4Y_2=200, 58
A4Y_3=88, 19
A4Y_4=200, -3
A4Y_a=123, 48
A4Y_b=240, 48
A4Y_c=123, 9
A4Y_d=240, -9
#A4X
#A4X=210,297,159.9,202.9
A4X_1=333, 58
A4X_2=449, 58
A4X_3=332, 19
A4X_4=449, -3
A4X_a=373, 48
A4X_b=484, 48
A4X_c=372, 9
A4X_d=484, -9
#the width and height of A4(mm)
#A3
#A3=297,420,280,287
A3_1=681, 58
A3_2=798, 58
A3_3=682, 19
A3_4=798, -3
A3_a=718, 48
A3_b=834, 48
A3_c=719, 9
A3_d=834, -9
#A2
#A2=420,594,454,410
A2_1=1160, 66
A2_2=1279, 66
A2_3=1160, 29
A2_4=1277, 7
A2_a=1197, 63
A2_b=1311, 63
A2_c=1197, 25
A2_d=1311, 0
#A1
#A1=594,841,701,584
A1_1=1861,66
A1_2=1974,66
A1_3=1860,29
A1_4=1974,8
A1_a=1900,63
A1_b=2014,63
A1_c=1900,25
A1_d=2014,1
#A0
#A0=841,1189,1039,110
A0_1=2848.1494,71.806
A0_2=2960.1494,71.806
A0_3=2848.1494,31.806
A0_4=2960.1494,7
A0_a=2884,60
A0_b=3000,60
A0_c=2884,20
A0_d=3000,1
#2018.1.24\u65B0\u589E\u56FE\u5E45
#A0-H
#A01
A5=841,1682,1532,821
#A02
A6=841,2378,2228,821
#A03
A7=841,3567,3417,821
#A04
A8=841,4756,4606,821

@ -0,0 +1,304 @@
package com.connor.jk.pdf.operate;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//import com.itextpdf.text.Rectangle;
import com.lowagie.text.DocumentException;
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 Test10 {
static Properties properties = new Properties();
public static void main(String[] args, String temp, String outs) throws DocumentException, IOException {
System.out.println("jinlai4>>>>>>>>>>>>>>>>>>>>>>");
Test10 test10 = new Test10();
test10.addInfo();
// 待加水印的文件
PdfReader reader = new PdfReader(temp);
// 加完水印的文件
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outs));
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
// 设置字体
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// 水印文字
String waterText = "2017.7.17";
// 获取PDF的页数
int pageSize = reader.getNumberOfPages();// 获取输入文件的页数
for (int k = 0; k < pageSize; k++) {
System.out.println("进入------------->");
// System.out.println(reader==null);
// System.out.println(reader.getPageSize(1)==null);
com.lowagie.text.Rectangle rectangle = reader.getPageSize(k + 1);
Float[] fposition = test10.getSize(rectangle);
for (int p = 0; p < args.length; p++) {
waterText = args[p];
char c = 0;
int high = 0;// 高度
// 循环对每页插入水印
int j = waterText.length(); // 文字长度
System.out.println("total---------------->" + total);
boolean isSizeOk = false;
for (int i = 1; i < total; i++) {
// 水印的起始
high = 500;
content = stamper.getUnderContent(i);
// 开始
content.beginText();
// 设置颜色
content.setColorFill(Color.GRAY);
// 设置字体及字号
content.setFontAndSize(base, 10);
// 设置起始位置A0
// System.out.println("===============================>"+str.split(",")[0]);
// System.out.println("===============================>"+str.split(",")[1]);
if ((fposition[1] > 2300 && fposition[0] > 3300)) {
isSizeOk = true;
// System.out.println(p);
if (p == 0) {
String str = properties.getProperty("A0_a");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 1) {
String str = properties.getProperty("A0_b");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 2) {
String str = properties.getProperty("A0_c");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 3) {
String str = properties.getProperty("A0_d");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
}
}
// 设置起始位置A1
if ((fposition[0] > 2300 && fposition[1] > 1600) && isSizeOk == false) {
isSizeOk = true;
if (p == 0) {
String str = properties.getProperty("A1_a");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 1) {
String str = properties.getProperty("A1_b");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 2) {
String str = properties.getProperty("A1_c");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 3) {
String str = properties.getProperty("A1_d");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
}
}
// 设置起始位置A2
if ((fposition[0] > 1600 && fposition[1] > 1100) && isSizeOk == false) {
isSizeOk = true;
if (p == 0) {
String str = properties.getProperty("A2_a");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 1) {
String str = properties.getProperty("A2_b");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 2) {
String str = properties.getProperty("A2_c");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 3) {
String str = properties.getProperty("A2_d");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
}
}
// 设置起始位置A3
if ((fposition[1] > 800 && fposition[0] > 1100) && isSizeOk == false) {
isSizeOk = true;
if (p == 0) {
String str = properties.getProperty("A3_a");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 1) {
String str = properties.getProperty("A3_b");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 2) {
String str = properties.getProperty("A3_c");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 3) {
String str = properties.getProperty("A3_d");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
}
}
// 设置起始位置A4-x
if ((fposition[1] > 500 && fposition[0] > 800) && isSizeOk == false) {
isSizeOk = true;
if (p == 0) {
String str = properties.getProperty("A4X_a");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 1) {
String str = properties.getProperty("A4X_b");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 2) {
String str = properties.getProperty("A4X_c");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 3) {
String str = properties.getProperty("A4X_d");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
}
}
// 设置起始位置A4-Y
if ((fposition[1] > 800 && fposition[0] > 500) && isSizeOk == false) {
isSizeOk = true;
if (p == 0) {
String str = properties.getProperty("A4Y_a");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 1) {
String str = properties.getProperty("A4Y_b");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 2) {
String str = properties.getProperty("A4Y_c");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
} else if (p == 3) {
String str = properties.getProperty("A4Y_d");
String[] values = str.split(",");
System.out.println("values[0]--------->" + values[0]);
System.out.println("values[1]--------->" + values[1]);
content.setTextMatrix(Float.valueOf(values[0]), Float.valueOf(values[1]));// x,y
}
}
// 开始写入水印
for (int q = 0; q < j; q++) {
content.setTextRise(14);
c = waterText.charAt(q);
// 将char转成字符串
// content.setTextRise(-90);//设置旋转
content.showText(c + "");
high -= 5;
}
content.endText();
}
}
}
stamper.close();
reader.close();// 关闭
File tempfile = new File(temp);
if (tempfile.exists()) {
// 删除原文件
tempfile.delete();
}
}
private void addInfo() throws IOException {
// 插入坐标
// Properties properties = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("com/connor/jk/pdf/operate/PDFInfo.properties");
properties.load(is);
// String str = properties.getProperty("A0_1");
// System.out.println("------------------->>>>>>>"+str.split(",")[0]);
}
/**
* PDF
*
* @param rectangle
* @return
*/
public Float[] getSize(com.lowagie.text.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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>................." + bean.getH());
System.out.println("bean.getW------------>................." + bean.getW());
return sizef;
}
}

@ -0,0 +1,26 @@
package com.connor.jk.pdf.operate;
import java.awt.Image;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
//import com.itextpdf.text.Image;
public class Test11 {
public static void main(String args) throws FileNotFoundException, IOException {
System.out.println("jinlai5>>>>>>>>>>>>>>>>>>>>>>");
Image srcImg = ImageIO.read(new FileInputStream("C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\白剑国.png"));// 取源图
int width = 60; // 假设要缩小到60点像素
int height = srcImg.getHeight(null) * 60 / srcImg.getWidth(null);// 按比例,将高度缩减
System.out.println("Width: " + srcImg.getWidth(null));// 这几行是调试用
System.out.println("Height: " + srcImg.getHeight(null));
System.out.println("Width2: " + width);
System.out.println("Height2: " + height);
Image smallImg = srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);// 缩小
}
}

@ -0,0 +1,41 @@
package com.connor.pdf;
public class JFomPdfBean {
private Float w = 0.0f;
private Float h = 0.0f;
private Float wi = 0.0f;
private Float hi = 0.0f;
public Float getW() {
return w;
}
public void setW(Float w) {
this.w = w;
}
public Float getH() {
return h;
}
public void setH(Float h) {
this.h = h;
}
public Float getWi() {
return wi;
}
public void setWi(Float wi) {
this.wi = wi;
}
public Float getHi() {
return hi;
}
public void setHi(Float hi) {
this.hi = hi;
}
}

@ -0,0 +1,297 @@
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.Image;
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 SignPdf2
{
/* public static void main(String[] args)
{
int pageCount = 1;
// String imagePath = "C:\\transpdf\\watermark.png";
//
// System.out.println(imagePath);
SignPdf2 signPdf = new SignPdf2();
String dataPath = args[0];
String pdfPath = args[1];
String outs = args[2];
// String dataPath = "C:\\Users\\wangc\\Desktop\\测试pdf签字\\text.txt";
// String pdfPath = "C:\\Users\\wangc\\Desktop\\测试pdf签字\\A0图纸.PDF";
// String outs = "C:\\Users\\wangc\\Desktop\\测试pdf签字\\001632-A4_idw2.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 y1 = 0.0F;
float y2 = 0.0F;
float y3 = 0.0F;
if(fposition[0] == 595.0 && fposition[1] == 842.0) {
x1 = 125.0F;
x2 = 213.0F;
y1 = 64.0F;
y2 = 43.0F;
y3 = 21.0F;
}else if(fposition[0] == 1191.0 && fposition[1] == 842.0) {
x1 = 720.0F;
x2 = 809.0F;
y1 = 64.0F;
y2 = 43.0F;
y3 = 21.0F;
}else if(fposition[0] == 1684.0 && fposition[1] == 1191.0) {
x1 = 1213.0F;
x2 = 1302.0F;
y1 = 64.0F;
y2 = 43.0F;
y3 = 21.0F;
}else if(fposition[0] == 2384.0 && fposition[1] == 1684.0) {
x1 = 1883.0F;
x2 = 1974.0F;
y1 = 92.0F;
y2 = 71.0F;
y3 = 49.0F;
}else if(fposition[0] == 3370.0 && fposition[1] == 2384.0) {
x1 = 2868.0F;
x2 = 2960.0F;
y1 = 92.0F;
y2 = 71.0F;
y3 = 49.0F;
}
// if (dataMap.containsKey("页数")) {
// String pageNum = (String)dataMap.get("页数");
// System.out.println("pageNum" + pageNum);
// pageCount = Integer.valueOf(pageNum).intValue();
// }
// if (dataMap.containsKey("会签")) {
// PdfContentByte content = stamper.getUnderContent(1);
// int high = 278;
// String value = (String)dataMap.get("会签");
// String[] dataList = null;
// if (value.split(",") != null)
// dataList = value.split(",");
// else {
// dataList[0] = "";
// }
// for (int i = 0; i < dataList.length; i++)
// {
// content.beginText();
//
// content.setColorFill(Color.BLACK);
//
// content.setFontAndSize(base, 10.0F);
//
// content.setTextRise(14.0F);
// content.setTextMatrix(Float.valueOf(10.0F).floatValue(), Float.valueOf(high).floatValue());
// content.showText(dataList[i]);
// content.endText();
// high -= 50;
// }
// }
//
// for (int i = 1; i <= pageCount; i++)
// {
// PdfContentByte under = stamper.getOverContent(i);
// Image img = Image.getInstance(imagePath);
// img.setAbsolutePosition(662.03998F, 569.32001F);
//
// under.addImage(img);
// }
if (dataMap.containsKey("设计")) {
String value = (String)dataMap.get("设计");
PdfContentByte content = stamper.getUnderContent(1);
content.beginText();
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 11.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, 11.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, 11.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, 11.0F);
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, 11.0F);
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, 11.0F);
content.setTextRise(14.0F);
content.setTextMatrix(Float.valueOf(x2).floatValue(), Float.valueOf(y3).floatValue());
content.showText(value);
content.endText();
}
stamper.close();
reader.close();
System.out.println("=====签字结束=====");
}
catch (Exception e) {
e.printStackTrace();
}
}
public Map<String, String> 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();
SignPdf2 pdf = new SignPdf2();
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();
}
}
}*/
}

@ -0,0 +1,211 @@
package com.connor.pdf;
import java.awt.FontMetrics;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.function.Consumer;
import javax.swing.JLabel;
import javax.swing.filechooser.FileSystemView;
import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import pdf_water.Getpdfwater;
public class SignPdf3 {
Boolean falg = false;
static Logger logger = Logger.getLogger(SignPdf3.class);
public static void main(String[] args) throws Exception {
System.out.println("进入程序开始运行》》》》》》》》》》");
FileSystemView fsv = FileSystemView.getFileSystemView();
logger.info("提示信息:打印图纸的日志文件");
StringBuilder build = new StringBuilder();
String pdfPath = null;
String outs = null;
String waterMark = null;
StringBuilder build1 = new StringBuilder();
try {
pdfPath = args[0];
outs = args[0];
waterMark = args[1];
/*
* pdfPath = "D://1.pdf"; outs = "D://2.pdf"; waterMark = "12323";
*/
String temppath = outs.substring(0, outs.lastIndexOf("."));
String type = outs.substring(outs.lastIndexOf("."), outs.length());
build1.append(temppath + "temp");
build1.append(type);
System.out.println("build1===="+build1.toString());
//build.append("接收到的第一个参数是:" + args[0] + "\r\n");
logger.info(build.toString());
} catch (Exception var26) {
logger.info("接收到的参数是:" + build.toString());
logger.error("异常:", var26);
var26.printStackTrace();
}
try {
removeWatermark(pdfPath);
logger.info("临时文件路径:" + build1.toString());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(build1.toString())));
if(waterMark.contains(":")) {
String[] split = waterMark.split(":");
setWatermark(bos, pdfPath, split[0], Integer.parseInt(split[1]), 150, 150, 0.1F);
}else {
setWatermark(bos, pdfPath, waterMark, 60, 150, 150, 0.1F);
}
bos.flush();
File f = new File(pdfPath);
f.delete();
InputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(build1.toString());
output = new FileOutputStream(pdfPath);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
if(input != null) {
input.close();
}
if(output != null) {
output.close();
}
}
} catch (Exception var28) {
var28.printStackTrace();
logger.error("水印异常信息", var28);
}
}
public static boolean removeWatermark(String path) {
try {
PDDocument document = PDDocument.load(new File(path));
PDPageTree pages = document.getPages();
Iterator var4 = pages.iterator();
while (var4.hasNext()) {
PDPage page = (PDPage) var4.next();
PDResources resources = page.getResources();
resources.getExtGStateNames().forEach((ext) -> {
String name = ext.getName();
// System.out.println("name==========="+name);
if(name.contains("Xi")) {
PDExtendedGraphicsState gState = resources.getExtGState(ext);
gState.setNonStrokingAlphaConstant(0.0F);
// System.out.println("1111111111");
}
});
}
document.save(path);
document.close();
return true;
} catch (IOException var6) {
var6.printStackTrace();
return false;
}
}
public static void setWatermark(BufferedOutputStream bos, String input, String waterMarkName, int size, int h,
int w, float f) throws DocumentException, IOException {
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
BaseFont base = null;
try {
base = BaseFont.createFont("/SimHei.ttf", "Identity-H", false);
System.out.println(base);
} catch (Exception var24) {
var24.printStackTrace();
}
JLabel label = new JLabel();
label.setText(waterMarkName);
FontMetrics metrics = label.getFontMetrics(label.getFont());
int textH = metrics.getHeight();
int textW = metrics.stringWidth(label.getText());
System.out.println("textH: " + textH);
System.out.println("textW: " + textW);
PdfGState gs = new PdfGState();
gs.setFillOpacity(f);
gs.setStrokeOpacity(f);
Rectangle pageSizeWithRotation = null;
PdfContentByte content = null;
for (int i = 1; i < total; ++i) {
content = stamper.getOverContent(i);
content.saveState();
content.setGState(gs);
content.beginText();
content.setFontAndSize(base, (float) size);
//根据不同的水印设置字体颜色
/*
* if(waterMarkName.equals("首样件用图")) { content.setRGBColorFill(255, 0, 0); }
* if(waterMarkName.equals("OTS用图")) { content.setRGBColorFill(0, 0, 255); }
* if(waterMarkName.equals("SOP用图")) { content.setRGBColorFill(0, 0, 0); }
*/
pageSizeWithRotation = reader.getPageSizeWithRotation(i);
float pageHeight = pageSizeWithRotation.getHeight();
float pageWidth = pageSizeWithRotation.getWidth();
float midH = pageHeight / 2.0F;
float midW = pageWidth / 2.0F;
/*
* content.showTextAligned(0, waterMarkName, midW - (float) w, midH - (float) h,
* 30.0F); content.showTextAligned(0, waterMarkName, midW - (float) w, midH +
* (float) h - (float) size, 30.0F); content.showTextAligned(0, waterMarkName,
* midW + (float) w - (float) size, midH - (float) h, 30.0F);
* content.showTextAligned(0, waterMarkName, midW + (float) w - (float) size,
* midH + (float) h - (float) size, 30.0F);
*/
content.showTextAligned(0, waterMarkName, midW, midH, 40.0F);
content.endText();
}
stamper.close();
reader.close();
}
}

@ -0,0 +1,7 @@
X0=\u8FDB\u673A\u53F7&0.21;0.905
X1=\u9875\u7801&0.95;-0.01
#X2=\u7F16\u5236&0.5;0.4
#X3=\u9636\u6BB5\u6807\u8BB0&0.91;0.905
#X4=\u7F16\u5236\u5355\u4F4D&0.3;0.5
#X5=\u6587\u4EF6\u4EE3\u53F7&0.48;0.63
#X6=\u6587\u4EF6\u540D\u79F0&0.48;0.76

@ -0,0 +1,34 @@
package com.connor.t5.pdf.ccgd;
public class PrefBean {
private String name;
private double hdou;
private double wdou;
private String val;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHdou() {
return hdou;
}
public void setHdou(double hdou) {
this.hdou = hdou;
}
public double getWdou() {
return wdou;
}
public void setWdou(double wdou) {
this.wdou = wdou;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
}

@ -0,0 +1,333 @@
package com.connor.t5.pdf.ccgd;
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.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.connor.jk.pdf.operate.JFomPdfBean;
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 SignPdf2 {
public static void main(String[] args) {
System.out.println("jinlai6>>>>>>>>>>>>>>>>>>>>>>");
// args=new String[3];
// args[0]="C:\\Users\\61666\\Desktop\\plugins\\signoff.txt";
// args[1]="C:\\Users\\61666\\Desktop\\plugins\\1.pdf";
// args[2]="C:\\Users\\61666\\Desktop\\plugins\\2.pdf";
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
String allInfomation = "";
// 读取txt的内容
try {
String encoding = "GBK";
File file = new File(args[0]);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
allInfomation = lineTxt;
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
allInfomation = allInfomation.replaceAll("\\\\", "\\\\\\\\");
String[] part = allInfomation.split("\\|");
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < part.length; i++) {
String name = part[i].split("=")[0];
String value = part[i].split("=")[1];
System.out.println("name-------->" + name);
System.out.println("value-------->" + value);
map.put(name, value);
}
// 页码&0.8;0.9
// 编制&0.5;0.9
// 进机号&0.1;0.2
List<PrefBean> listbean = new ArrayList<PrefBean>();
SignPdf2 signPdf = new SignPdf2();
try {
List<String> sites = signPdf.getBeanMsgList();
for (int i = 0; i < sites.size(); i++) {
System.out.println(i + "-----" + sites.get(i));
String[] split = sites.get(i).split("&");
System.out.println("split.len" + split.length);
if (split.length > 1) {
String[] split2 = split[1].split(";");
PrefBean bean = new PrefBean();
String val = "";
if (map.containsKey(split[0])) {
val = map.get(split[0]);
}
bean.setName(split[0]);
bean.setWdou(Double.parseDouble(split2[0]));
bean.setHdou(Double.parseDouble(split2[1]));
bean.setVal(val);
listbean.add(bean);
}
}
Signym(args[1], args[2], listbean);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static void Signym(String pdfPath, String outs, List<PrefBean> listbean) {
SignPdf2 signPdf = new SignPdf2();
try {
System.out.println("=====开始签字1=====");
PdfReader reader = new PdfReader(pdfPath);
int pages = reader.getNumberOfPages();
System.out.println("总共页面有:" + pages);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outs));
for (int ym = 1; ym < pages + 1; ym++) {
System.out.println("开始签第" + ym + "页");
Rectangle rectangle = reader.getPageSize(ym);
JFomPdfBean bean = signPdf.getSize2(rectangle);
System.out.println(
"bean=" + bean.getH() + "----" + bean.getHi() + "----" + bean.getW() + "----" + bean.getWi());
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);
if (ym == 1) {// 首页
for (int i = 0; i < listbean.size(); i++) {
PrefBean prefBean = listbean.get(i);
String name = prefBean.getName();
String val = prefBean.getVal();
double hdou = prefBean.getHdou();
double wdou = prefBean.getWdou();
if (name.equals("页码")) {
val = ym + "";
}
System.out.println("签入:" + name + "----" + val);
if (val.equals("")) {
continue;
}
float hh = bean.getH() + (float) ((bean.getHi() - bean.getH()) * hdou);
float ww = bean.getW() + (float) ((bean.getWi() - bean.getW()) * wdou);
// String value = ym + "";
System.out.println("hh=" + hh + "---ww=" + ww);
PdfContentByte content = stamper.getUnderContent(ym);
content.beginText();
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 15.0F);
content.setTextRise(15.0F);
content.setTextMatrix(ww, hh);
// content.setTextMatrix(Float.valueOf(812.0F).floatValue(),
// Float.valueOf(-13.0F).floatValue());
content.showText(val);
content.endText();
// signPdf.signBZ(pageCount, value, content, reader, stamper,
// Integer.valueOf(500));
}
} else {
for (int i = 0; i < listbean.size(); i++) {
PrefBean prefBean = listbean.get(i);
String name = prefBean.getName();
String val = prefBean.getVal();
double hdou = prefBean.getHdou();
double wdou = prefBean.getWdou();
if (name.equals("页码")) {
val = ym + "";
} else {
continue;
}
System.out.println("签入:" + name + "----" + val);
if (val.equals("")) {
continue;
}
float hh = bean.getH() + (float) ((bean.getHi() - bean.getH()) * hdou);
float ww = bean.getW() + (float) ((bean.getWi() - bean.getW()) * wdou);
System.out.println("hh=" + hh + "---ww=" + ww);
PdfContentByte content = stamper.getUnderContent(ym);
content.beginText();
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 15.0F);
content.setTextRise(15.0F);
content.setTextMatrix(ww, hh);
// content.setTextMatrix(Float.valueOf(812.0F).floatValue(),
// Float.valueOf(-13.0F).floatValue());
content.showText(val);
content.endText();
// signPdf.signBZ(pageCount, value, content, reader, stamper,
// Integer.valueOf(500));
}
}
}
stamper.close();
reader.close();
System.out.println("=====签字结束=====");
} catch (Exception e) {
e.printStackTrace();
}
}
public Map<String, String> 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 JFomPdfBean getSize2(Rectangle rectangle) {
float toleft = rectangle.getLeft();
float tobottom = rectangle.getBottom();
float toright = rectangle.getRight();
float totop = rectangle.getTop();
JFomPdfBean bean = new JFomPdfBean();
bean.setH(tobottom);
bean.setHi(totop);
bean.setW(toleft);
bean.setWi(toright);
return bean;
}
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();
SignPdf2 pdf = new SignPdf2();
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();
}
}
}
/**
* properties
*
* @throws IOException
*/
public List<String> getBeanMsgList() throws IOException {
List<String> list = new ArrayList<String>();
Properties properties = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("com/connor/t5/pdf/ccgd/PDFInfo.properties");
properties.load(is);
// properties.getProperty("role_name");
for (int i = 0; true; i++) {
// System.out.println();
String str = properties.getProperty("X" + i);
if (str == null) {
break;
}
list.add(str);
}
is.close();
return list;
}
}

@ -0,0 +1,41 @@
package com.connor.t5.pdf.operate;
public class JFomPdfBean {
private Float w = 0.0f;
private Float h = 0.0f;
private Float wi = 0.0f;
private Float hi = 0.0f;
public Float getW() {
return w;
}
public void setW(Float w) {
this.w = w;
}
public Float getH() {
return h;
}
public void setH(Float h) {
this.h = h;
}
public Float getWi() {
return wi;
}
public void setWi(Float wi) {
this.wi = wi;
}
public Float getHi() {
return hi;
}
public void setHi(Float hi) {
this.hi = hi;
}
}

@ -0,0 +1,186 @@
package com.connor.t5.pdf.operate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
import java.io.InputStreamReader;
//import java.util.Properties;
//import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
//import com.itextpdf.text.pdf.PdfReader;
//import com.itextpdf.text.pdf.PdfStamper;
//import com.lowagie.text.DocumentException;
public class JFomPdfOperateMain {
public static 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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>1" + bean.getH());
System.out.println("bean.getW------------>1" + bean.getW());
System.out.println("sizef[0]------------>1" + sizef[0]);
System.out.println("sizef[1]------------>1" + sizef[1]);
return sizef;
}
/**
* Javatxt 1 2
* 3 4readline()
*
* @param filePath
*/
public static void readTxtFile(String filePath) {
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);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
// allInfomation=lineTxt;
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
*
* args[0]txt(txt)"\"应为"\\" args[1]为pdf的完整路径(包括pdf),路径中的"\"应为"\\"
* args[2]pdf(pdf)"\"应为"\\"
*
*/
public static void main(String[] args) {
System.out.println("jinlai7>>>>>>>>>>>>>>>>>>>>>>");
// args=new String[3];
// args[0]="C:\\Users\\61666\\Desktop\\pdf签名\\signoff.txt";
// args[1]="C:\\Users\\61666\\Desktop\\pdf签名\\A2_V3非密.pdf";
// args[2]="C:\\Users\\61666\\Desktop\\pdf签名\\ttt.pdf";
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
String allInfomation = "";
// String filePath = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\signoff.txt";
// "res/";
// readTxtFile(filePath);
// 读取txt的内容
try {
String encoding = "GBK";
// File file = new File(filePath);
File file = new File(args[0]);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
allInfomation = lineTxt;
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
allInfomation = allInfomation.replaceAll("\\\\", "\\\\\\\\");
String[] part = allInfomation.split("\\|");
for (int i = 0; i < part.length; i++) {
System.out.println(part[i]);
}
String[] imgsPath = new String[0];
String[] timeS = new String[9];
// int count=0;
// int count1=0;
for (int i = 0; i < part.length; i++) {
String name = part[i].split("=")[0];
System.out.println("name-------->" + name);
if (name.equals("设计")) {
timeS[0] = part[i].split("=")[1];
}
if (name.equals("CAD")) {
timeS[1] = part[i].split("=")[1];
}
if (name.equals("校对")) {
timeS[2] = part[i].split("=")[1];
}
if (name.equals("审核")) {
timeS[3] = part[i].split("=")[1];
}
if (name.equals("工艺")) {
timeS[4] = part[i].split("=")[1];
}
if (name.equals("复审")) {
timeS[5] = part[i].split("=")[1];
}
if (name.equals("标审")) {
timeS[6] = part[i].split("=")[1];
}
if (name.equals("批准")) {
timeS[7] = part[i].split("=")[1];
}
if (name.equals("会签")) {
timeS[8] = part[i].split("=")[1];
}
}
for (int i = 0; i < timeS.length; i++) {
System.out.println(timeS[i]);
}
// PDF 输入文件
// PdfReader reader = null;
// try {
// reader = new PdfReader("C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf", "PDF".getBytes());
// } catch (IOException e) {
// e.printStackTrace();
// }
// String[] imgs = new String[] { "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\白剑国.png",
// "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈超杰.png", "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈冬.png" };
// String[] time = new String[] { "20170612", "20170607", "20170909" };
// String ins = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf";
String ins = args[1];
// ins=ins.replaceAll("\\\\", "\\\\\\\\");
String outs = args[2];
// outs=outs.replaceAll("\\\\", "\\\\\\\\");
// String temp="";
int num = outs.lastIndexOf(".");
String temp = outs.substring(0, num);
// System.out.println(before);
temp = temp + "tempfile.pdf";
JFomPdfOperateMethodUtil.main(ins, temp, imgsPath);
// 用来签入文字
try {
Test10.main(timeS, temp, outs);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,208 @@
package com.connor.t5.pdf.operate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
import java.io.InputStreamReader;
//import java.util.Properties;
//import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
//import com.itextpdf.text.pdf.PdfReader;
//import com.itextpdf.text.pdf.PdfStamper;
//import com.lowagie.text.DocumentException;
public class JFomPdfOperateMain_fei {
public static 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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>1" + bean.getH());
System.out.println("bean.getW------------>1" + bean.getW());
System.out.println("sizef[0]------------>1" + sizef[0]);
System.out.println("sizef[1]------------>1" + sizef[1]);
return sizef;
}
/**
* Javatxt 1 2
* 3 4readline()
*
* @param filePath
*/
public static void readTxtFile(String filePath) {
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);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
// allInfomation=lineTxt;
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
*
* args[0]txt(txt)"\"应为"\\" args[1]为pdf的完整路径(包括pdf),路径中的"\"应为"\\"
* args[2]pdf(pdf)"\"应为"\\"
*
*/
public static void main(String[] args) {
System.out.println("jinlai6>>>>>>>>>>>>>>>>>>>>>>");
// args=new String[3];//
System.out.println("11111" + args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
// args[0]="C:\\Users\\hezhenquan\\Desktop\\PDF\\signoff.txt";
// args[1]="C:\\Users\\hezhenquan\\Desktop\\PDF\\ww.pdf";
// args[2]="C:\\Users\\hezhenquan\\Desktop\\PDF\\ppp.pdf";
String allInfomation = "";
// String filePath = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\signoff.txt";
// "res/";
// readTxtFile(filePath);
// 读取txt的内容
try {
String encoding = "GBK";
// File file = new File(filePath);
File file = new File(args[0]);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
allInfomation = lineTxt;
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
// allInfomation=allInfomation.replaceAll("\\\\", "\\\\\\\\");
String[] part = allInfomation.split("\\|");
for (int i = 0; i < part.length; i++) {
System.out.println(part[i]);
}
String[] imgsPath = new String[4];
String[] timeS = new String[4];
int count = 0;
int count1 = 0;
for (int i = 0; i < part.length; i++) {
String name = part[i].split("=")[0];
System.out.println("name-------->" + name);
// System.out.println(i);
if (name.equals("设计者")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("标准化")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("审核者")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("批准者")) {
imgsPath[count] = part[i].split("=")[1];
// System.out.println(imgsPath[count]);
// System.out.println(count);
count++;
}
if (name.equals("设计时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
if (name.equals("标准化时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
}
if (name.equals("审核时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
if (name.equals("批准签名时间")) {
timeS[count1] = part[i].split("=")[1];
count1++;
// System.out.println(imgsPath[count]);
// System.out.println(count);
}
}
for (int i = 0; i < timeS.length; i++) {
System.out.println(timeS[i]);
}
for (int i = 0; i < imgsPath.length; i++) {
System.out.println(imgsPath[i]);
}
// PDF 输入文件
// PdfReader reader = null;
// try {
// reader = new PdfReader("C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf", "PDF".getBytes());
// } catch (IOException e) {
// e.printStackTrace();
// }
// String[] imgs = new String[] { "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\白剑国.png",
// "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈超杰.png", "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\陈冬.png" };
// String[] time = new String[] { "20170612", "20170607", "20170909" };
// String ins = "C:\\Users\\hezhenquan\\Desktop\\PDF插入水印\\ENGINEER.pdf";
String ins = args[1];
// ins=ins.replaceAll("\\\\", "\\\\\\\\");
String outs = args[2];
// outs=outs.replaceAll("\\\\", "\\\\\\\\");
// String temp="";
// int num=outs.lastIndexOf(".");
// String temp=outs.substring(0, num);
// System.out.println(before);
// temp=temp+"tempfile.pdf";
JFomPdfOperateMethodUtil.main(ins, outs, imgsPath);
// JFomPdfOperateMethodUtil.main(ins, imgsPath);
// 用来签入时间
try {
Test10.main(timeS, outs, outs);
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,382 @@
package com.connor.t5.pdf.operate;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class JFomPdfOperateMethodUtil {
public List<JFomPdfBean> beanList;
public static void main(String ins, String temp, String[] imgs) {
System.out.println("jinlai8>>>>>>>>>>>>>>>>>>>>>>");
JFomPdfOperateMethodUtil util = new JFomPdfOperateMethodUtil();
try {
util.addPdfMark(ins, temp, imgs);
System.out.println(System.getenv("TEMP"));// 打印缓存路径
} catch (Exception e) {
e.printStackTrace();
}
}
public JFomPdfOperateMethodUtil() {
this.beanList = new ArrayList<JFomPdfBean>();
try {
// 获取配置文件中的信息
getBeanMsgList();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* properties
*
* @throws IOException
*/
public void getBeanMsgList() throws IOException {
Properties properties = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("com/connor/t5/pdf/operate/PDFInfo.properties");
properties.load(is);
// properties.getProperty("role_name");
for (int i = 0; true; i++) {
// System.out.println();
String str = properties.getProperty("A" + i);
if (str == null) {
break;
}
String[] values = str.split(",");
if (values != null && values.length == 4) {
JFomPdfBean bean = new JFomPdfBean();
Float fhValue = 0.0f;
Float fwValue = 0.0f;
Float fhiValue = 0.0f;
Float fwiValue = 0.0f;
try {
fhValue = Float.parseFloat(values[0]);
fwValue = Float.parseFloat(values[1]);
fhiValue = Float.parseFloat(values[2]);
fwiValue = Float.parseFloat(values[3]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
bean.setH(fhValue);
bean.setW(fwValue);
bean.setHi(fhiValue);
bean.setWi(fwiValue);
this.beanList.add(bean);
}
}
is.close();
}
// /**
// * 统一按照宽度压缩
// *
// * @param args
// */
// public static float getPercent(float h, float w) {
// float p2 = 10.0f;
// // p2 = 820 / w * 100;
// System.out.println(p2);
// return p2;
// }
/**
* pdf
*
* @param InPdfFile pdf
* @param outPdfFile
* @param markImagePath
* @param pageSize pdfexcelpdfexcel
* reader.getNumberOfPages()pdf
* @throws Exception
*/
public void addPdfMark(String InPdfFile, String temp, String[] markImagePath) throws Exception {
// 插入坐标
Properties properties = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("com/connor/t5/pdf/operate/PDFInfo.properties");
properties.load(is);
// PDF 输入文件
PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());
// PDF 输出文件
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(temp));
// 水印图片
Image img = null;
for (int j = 0; j < markImagePath.length; j++) {
img = Image.getInstance(markImagePath[j]);// 获取图片实例
img.scaleAbsolute(35, 20);// 缩放图片
// float imgW = img.getScaledWidth();// 获取图片的宽度
// float imgH = img.getScaledHeight();// 获取图片的高度
// 获取PDF的页数
int pageSize = reader.getNumberOfPages();// 获取输入文件的页数
for (int i = 1; i <= pageSize; i++) {
Rectangle rectangle = reader.getPageSize(i);
Float[] fposition = getSize(rectangle);
System.out.println("fposition[0]=======" + fposition[0]);
System.out.println("fposition[1]=======" + fposition[1]);
boolean isSizeOk = false;
// 设置水印的插入位置A0
if ((fposition[0] > 3300 && fposition[1] > 2300)) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A0");
String str = properties.getProperty("A0_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A0_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A0_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A0_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A1
if ((fposition[0] > 2300 && fposition[1] > 1600) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A1");
String str = properties.getProperty("A1_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A1_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A1_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A1_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1861, 66);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1974, 66);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1860, 29);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1860, 29);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A2
if ((fposition[1] > 1100 && fposition[0] > 1600) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A2");
String str = properties.getProperty("A2_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A2_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A2_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A2_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1160, 66);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1277, 66);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1160, 29);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(1160, 29);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A3
if ((fposition[0] > 1100 && fposition[1] > 800) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A3");
String str = properties.getProperty("A3_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A3_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A3_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A3_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(681, 58);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(798, 58);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(682, 19);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(682, 19);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A4-X
if ((fposition[0] > 800 && fposition[1] > 500) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A4-X");
String str = properties.getProperty("A4X_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A4X_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A4X_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A4X_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(333, 58);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(449, 58);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(332, 19);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 19);// 设置水印左下角的坐标
}
}
// TODO 设置水印的插入位置A4-Y
if ((fposition[1] > 800 && fposition[0] > 500) && isSizeOk == false) {
isSizeOk = true;
System.out.println("进入设置水印的插入位置A4-Y");
String str = properties.getProperty("A4Y_1");
String[] values = str.split(",");
String str1 = properties.getProperty("A4Y_2");
String[] values1 = str1.split(",");
String str2 = properties.getProperty("A4Y_3");
String[] values2 = str2.split(",");
String str3 = properties.getProperty("A4Y_4");
String[] values3 = str3.split(",");
if (j == 0) {
img.setAbsolutePosition(Float.valueOf(values[0]), Float.valueOf(values[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 58);// 设置水印左下角的坐标
} else if (j == 1) {
img.setAbsolutePosition(Float.valueOf(values1[0]), Float.valueOf(values1[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(200, 58);// 设置水印左下角的坐标
} else if (j == 2) {
img.setAbsolutePosition(Float.valueOf(values2[0]), Float.valueOf(values2[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 19);// 设置水印左下角的坐标
} else if (j == 3) {
img.setAbsolutePosition(Float.valueOf(values3[0]), Float.valueOf(values3[1]));// 设置水印左下角的坐标
// img.setAbsolutePosition(88, 19);// 设置水印左下角的坐标
}
}
// 向PDF输出文件中插入水印
PdfContentByte under = stamp.getOverContent(i);// stamp.getUnderContent(i);
PdfGState gs = new PdfGState();
gs.setFillOpacity(1.0f);// 设置透明度为0.2
under.setGState(gs);
under.addImage(img);
}
}
stamp.close();// 关闭
reader.close();// 关闭
// File tempfile = new File(InPdfFile);
// if (tempfile.exists()) {
// // 删除原文件
// // tempfile.delete();
// }
}
/**
* PDF
*
* @param rectangle
* @return
*/
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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>" + bean.getH());
System.out.println("bean.getW------------>" + bean.getW());
return sizef;
}
/**
* 2.8346
*
* @param bean
* @return
*/
public Float[] getPosization(JFomPdfBean bean) {
Float size = 2.8346f;// 1毫米=2.8346磅
Float h = (float) (bean.getH() / size);// 转换PDF的磅为毫米
Float w = (float) (bean.getW() / size);
System.out.println(h + " | " + w);
Float[] sizef = new Float[] { 0.0f, 0.0f };
if (this.beanList != null) {
for (int i = 0; i < this.beanList.size(); i++) {
JFomPdfBean beant = beanList.get(i);
// System.out.println(bean.getH() + " | "+ beant);
if ((beant.getH() - 10) < h && h < (beant.getH() + 10) && (beant.getW() - 10) < w
&& w < (beant.getW() + 10)) {
sizef[0] = beant.getHi() * size;
sizef[1] = beant.getWi() * size;
break;
}
if ((beant.getH() - 10) < w && w < (beant.getH() + 10) && (beant.getW() - 10) < h
&& h < (beant.getW() + 10)) {
sizef[0] = beant.getHi() * size;
sizef[1] = beant.getWi() * size;
break;
}
}
}
return sizef;
}
}

@ -0,0 +1,89 @@
A5_0=292, 100
A5_1=292, 91
A5_2=292, 82
A5_3=292, 73
A5_4=345, 100
A5_5=345, 91
A5_6=345, 82
A5_7=345, 73
#A4Y
#A4Y=210,297,159.9,202.9
A4Y_0=105, 74
A4Y_1=218, 74
A4Y_2=105, 35
A4Y_3=218, 15
A4Y_4=139, 65
A4Y_5=252, 65
A4Y_6=139,25
A4Y_7=252, 6
A4Y_8=252, 25
#A4X
#A4X=210,297,159.9,202.9
A4X_0=500, 35
A4X_1=500, 24
A4X_2=500, 12
A4X_3=500, 1
A4X_4=573, 35
A4X_5=573, 24
A4X_6=573, 12
A4X_7=573, 1
A4X_8=12, 325
#the width and height of A4(mm)
#A3
#A3=297,420,280,287
#A3_1=681, 58
A3_0=500, 35
A3_1=500, 24
A3_2=500, 12
A3_3=500, 1
A3_4=573, 35
A3_5=573, 24
A3_6=573, 12
A3_7=573, 1
A3_8=12, 325
#A2
#A2=420,594,454,410
A2_0=1179, 89
A2_1=1292, 89
A2_2=1179, 49
A2_3=1292, 30
A2_4=1214, 80
A2_5=1327, 80
A2_6=1214, 40
A2_7=1327, 22
A2_8=1327, 120
#A1
#A1=594,841,701,584
A1_0=1879,120
A1_1=1879,87
A1_2=1992,89
A1_3=1879,49
A1_4=1992,30
A1_5=1914,81
A1_6=2027,81
A1_7=1914,41
A1_8=2027,22
#A0
#A0=841,1189,1039,110
A0_0=2866,187
A0_1=2866,87
A0_2=2979,87
A0_3=2866,47
A0_4=2979,29
A0_5=2901,78
A0_6=3014,78
A0_7=2901,38
A0_8=3014,20
#2018.1.24\u65B0\u589E\u56FE\u5E45
#A0-H
#A01
A5=841,1682,1532,821
#A02
A6=841,2378,2228,821
#A03
A7=841,3567,3417,821
#A04
A8=841,4756,4606,821

@ -0,0 +1,86 @@
#A4Y
#A4Y=210,297,159.9,202.9
A4Y_1=88, 58
A4Y_2=200, 58
A4Y_3=88, 19
A4Y_4=200, -3
A4Y_a=123, 48
A4Y_b=240, 48
A4Y_c=123, 9
A4Y_d=240, -9
#A4X
#A4X=210,297,159.9,202.9
A4X_1=333, 58
A4X_2=449, 58
A4X_3=332, 19
A4X_4=449, -3
A4X_a=373, 48
A4X_b=484, 48
A4X_c=372, 9
A4X_d=484, -9
#the width and height of A4(mm)
#A3
#A3=297,420,280,287
A3_1=681, 58
A3_2=798, 58
A3_3=682, 19
A3_4=798, -3
A3_a=718, 48
A3_b=834, 48
A3_c=719, 9
A3_d=834, -9
#A2
#A2=420,594,454,410
A2_1=1160, 66
A2_2=1279, 66
A2_3=1160, 29
A2_4=1277, 7
A2_a=1197, 63
A2_b=1311, 63
A2_c=1197, 25
A2_d=1311, 0
#A1
#A1=594,841,701,584
A1_1=1861,66
A1_2=1974,66
A1_3=1860,29
A1_4=1974,8
A1_a=1900,63
A1_b=2014,63
A1_c=1900,25
A1_d=2014,1
#A0
#A0=841,1189,1039,110
A0_1=2848.1494,71.806
A0_2=2960.1494,71.806
A0_3=2848.1494,31.806
A0_4=2960.1494,7
A0_a=2884,60
A0_b=3000,60
A0_c=2884,20
A0_d=3000,1
#2018.1.24\u65B0\u589E\u56FE\u5E45
#A0-H
#A01
A5=841,1682,1532,821
#A02
A6=841,2378,2228,821
#A03
A7=841,3567,3417,821
#A04
A8=841,4756,4606,821

@ -0,0 +1,185 @@
package com.connor.t5.pdf.operate;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Properties;
//import com.itextpdf.text.Rectangle;
import com.lowagie.text.DocumentException;
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 Test10 {
static Properties properties = new Properties();
public static void main(String[] args, String temp, String outs) throws DocumentException, IOException {
System.out.println("jinlai10>>>>>>>>>>>>>>>>>>>>>>");
Test10 test10 = new Test10();
test10.addInfo();
// 待加水印的文件
PdfReader reader = new PdfReader(temp);
// 加完水印的文件
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outs));
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
// 设置字体
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// 水印文字
String waterText = "";
// 获取PDF的页数
int pageSize = reader.getNumberOfPages();// 获取输入文件的页数
System.out.println("pageSize=" + pageSize);
for (int k = 0; k < pageSize; k++) {
System.out.println("进入------------->");
// System.out.println(reader==null);
// System.out.println(reader.getPageSize(1)==null);
com.lowagie.text.Rectangle rectangle = reader.getPageSize(k + 1);
Float[] fposition = test10.getSize(rectangle);
System.out.println("fposition[0]=" + fposition[0] + " fposition[1]=" + fposition[1]);
String tufu = "";
boolean isSizeOk = false;
if ((fposition[1] > 2300 && fposition[0] > 3300)) {
isSizeOk = true;
tufu = "A0";
}
if ((fposition[0] > 2300 && fposition[1] > 1600) && isSizeOk == false) {
isSizeOk = true;
tufu = "A1";
}
if ((fposition[0] > 1600 && fposition[1] > 1100) && isSizeOk == false) {
isSizeOk = true;
tufu = "A2";
}
if ((fposition[1] > 800 && fposition[0] > 1100) && isSizeOk == false) {
isSizeOk = true;
tufu = "A3";
}
if ((fposition[1] > 500 && fposition[0] > 800) && isSizeOk == false) {
isSizeOk = true;
tufu = "A4X";
}
if ((fposition[1] > 800 && fposition[0] > 500) && isSizeOk == false) {
isSizeOk = true;
tufu = "A4Y";
}
if ((fposition[1] > 600 && fposition[0] > 700) && isSizeOk == false) {
isSizeOk = true;
tufu = "A5";
}
System.out.println("图幅为" + tufu);
if (tufu.equals("")) {
System.out.println("未找到对应图幅!");
continue;
}
for (int p = 0; p < args.length; p++) {
waterText = args[p];
System.out.println("waterText=" + waterText);
if (waterText == null) {
continue;
}
String[] waterTsps = waterText.split(";");// 存在会签分割
for (int i = 0; i < waterTsps.length; i++) {
String watert = waterTsps[i];
char c = 0;
int high = 0;// 高度
// 循环对每页插入文字
int j = watert.length(); // 文字长度
// 文字的起始
high = 500;
content = stamper.getUnderContent(k + 1);
// 开始
content.beginText();
// 设置颜色
content.setColorFill(Color.GRAY);
// 设置字体及字号
content.setFontAndSize(base, 10);
// 设置起始位置A0
if (!tufu.equals("")) {
String key = tufu + "_" + p;
String str = properties.getProperty(key);
System.out.println(key + "---" + str);
if (str == null || !str.contains(",")) {
continue;
}
String[] values = str.split(",");
System.out.println("坐标为]--------->" + values[0] + "----" + values[1]);
BigDecimal b1 = BigDecimal.valueOf(Float.valueOf(values[1]));
BigDecimal b2 = BigDecimal.valueOf(12 * i);
// System.out.println(b1.add(b2).floatValue());
content.setTextMatrix(Float.valueOf(values[0]), b1.subtract(b2).floatValue());// x,y
}
// 开始写入文字
for (int q = 0; q < j; q++) {
content.setTextRise(14);
c = watert.charAt(q);
// 将char转成字符串
// content.setTextRise(-90);//设置旋转
content.showText(c + "");
high -= 5;
}
content.endText();
}
}
}
stamper.close();
reader.close();// 关闭
File tempfile = new File(temp);
if (tempfile.exists()) {
// 删除原文件
tempfile.delete();
}
}
private void addInfo() throws IOException {
// 插入坐标
// Properties properties = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("com/connor/t5/pdf/operate/PDFInfo.properties");
properties.load(is);
// String str = properties.getProperty("A0_1");
// System.out.println("------------------->>>>>>>"+str.split(",")[0]);
}
/**
* PDF
*
* @param rectangle
* @return
*/
public Float[] getSize(com.lowagie.text.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(toright - toleft);// PDF的宽
bean.setW(totop - tobottom);// PDF的高
sizef[0] = bean.getH();
sizef[1] = bean.getW();
System.out.println("bean.getH------------>................." + bean.getH());
System.out.println("bean.getW------------>................." + bean.getW());
return sizef;
}
}

@ -0,0 +1,13 @@
log4j.rootLogger=INFO,Console,File
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] %l%m%n
#File
log4j.appender.File=org.apache.log4j.FileAppender
log4j.appender.File.Append=false
log4j.appender.File.File=${WORKDIR}/print_PDF.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] %l%m%n
Loading…
Cancel
Save