|
|
|
|
@ -0,0 +1,231 @@
|
|
|
|
|
package com.connor.pdf;
|
|
|
|
|
|
|
|
|
|
import com.itextpdf.awt.geom.Rectangle2D;
|
|
|
|
|
import com.itextpdf.text.*;
|
|
|
|
|
import com.itextpdf.text.pdf.*;
|
|
|
|
|
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
|
|
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
|
|
|
|
|
|
import com.spire.pdf.graphics.PdfGraphicsUnit;
|
|
|
|
|
import com.spire.pdf.graphics.PdfUnitConvertor;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
|
|
public class pdfMain {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
System.out.println("进入程序开始运行》》》》》》》》》》");
|
|
|
|
|
File file = new File(args[0]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
pictureToPdf(args[3], file, args[1], args[2],args[4]);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
// File file = new File("D://a.pdf");
|
|
|
|
|
// pictureToPdf("900,600,1,100,700,15", file, "D://a.pdf", "D://a.png");
|
|
|
|
|
//图片x位置,图片y位置,图片比例,文字x位置,文字y位置,文字大小
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static File pictureToPdf(String locationAll, File sourceFile, String outPdfFileName, String picture,String color) {
|
|
|
|
|
if (!sourceFile.exists()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
File outPdfFile = null;
|
|
|
|
|
InputStream inputStreamPdf = null;
|
|
|
|
|
try {
|
|
|
|
|
inputStreamPdf = new FileInputStream(sourceFile);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
System.out.println("IN PDF=>" + sourceFile.getAbsolutePath());
|
|
|
|
|
|
|
|
|
|
String length = "";
|
|
|
|
|
String wide = "";
|
|
|
|
|
String type = "";
|
|
|
|
|
String page = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inputStreamPdf != null) {
|
|
|
|
|
PdfReader reader = null;
|
|
|
|
|
PdfStamper stamp = null;
|
|
|
|
|
try {
|
|
|
|
|
outPdfFile = new File(outPdfFileName);
|
|
|
|
|
reader = new PdfReader(inputStreamPdf, "PDF".getBytes());
|
|
|
|
|
stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));
|
|
|
|
|
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
|
|
|
|
PdfContentByte content = null;
|
|
|
|
|
// 水印图片
|
|
|
|
|
Image img = null;
|
|
|
|
|
int pageSize = reader.getNumberOfPages();// 获取输入文件的页数
|
|
|
|
|
page = pageSize+"";
|
|
|
|
|
if (pageSize < 1) {
|
|
|
|
|
System.out.println("PDF页数小于0,退出PDF签名");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
PdfDictionary pd = null;
|
|
|
|
|
// 新建一个PDF解析对象
|
|
|
|
|
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
|
|
|
|
|
int CeroWY = 0;
|
|
|
|
|
// if(signoffEntry.getIscero().equals("FROM iPEM") || signoffEntry.getIscero().equals("FROMiPEM")){
|
|
|
|
|
// CeroWY = 2;
|
|
|
|
|
// }
|
|
|
|
|
for (int index = 1; index <= pageSize; index++) {
|
|
|
|
|
|
|
|
|
|
Rectangle rectangle = reader.getPageSizeWithRotation(index);
|
|
|
|
|
Float[] fposition = getSize(rectangle);
|
|
|
|
|
float pdfW = fposition[0], pdfH = fposition[1];
|
|
|
|
|
System.out.println(String.format("pdfW=%f,pdfH=%f", pdfW, pdfH));
|
|
|
|
|
length = pdfH+"";
|
|
|
|
|
wide = pdfW+"";
|
|
|
|
|
Image image = Image.getInstance(picture);
|
|
|
|
|
String[] locations = locationAll.split(";");
|
|
|
|
|
int count = 0;
|
|
|
|
|
String[] location = null;
|
|
|
|
|
for (int i = 0; i < locations.length; i++) {
|
|
|
|
|
String[] split = locations[i].split(":");
|
|
|
|
|
String[] split2 = split[1].split(",");
|
|
|
|
|
if(pdfW > Float.parseFloat(split2[0]) && pdfW < Float.parseFloat(split2[1]) && pdfH > Float.parseFloat(split2[2]) && pdfH < Float.parseFloat(split2[3])) {
|
|
|
|
|
System.out.println(split[0]+"匹配---------------------");
|
|
|
|
|
type = split[0];
|
|
|
|
|
location = split[2].split(",");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(location == null || location.length <= 0) {
|
|
|
|
|
System.out.println(index+":PDF图幅大小未匹配");
|
|
|
|
|
continue ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content = stamp.getOverContent(index);
|
|
|
|
|
// PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
|
|
|
|
|
// float x = unitCvtr.convertUnits(pdfW, PdfGraphicsUnit.Point, PdfGraphicsUnit.Point);
|
|
|
|
|
// float y = unitCvtr.convertUnits(pdfH, PdfGraphicsUnit.Point, PdfGraphicsUnit.Point);
|
|
|
|
|
// System.out.println(String.format("x=%f,y=%f", x, y));
|
|
|
|
|
// if (x >= 2380 && x <= 2390) {
|
|
|
|
|
// float x1 = x;
|
|
|
|
|
// float y1 = y;
|
|
|
|
|
// x = y1;
|
|
|
|
|
// y = x1;
|
|
|
|
|
// }
|
|
|
|
|
// if (x >= 1680 && x <= 1690 && y >= 2380 && y <= 2390) {
|
|
|
|
|
// float x1 = x;
|
|
|
|
|
// float y1 = y;
|
|
|
|
|
// x = y1;
|
|
|
|
|
// y = x1;
|
|
|
|
|
// }
|
|
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
String dateValue = df.format(date);
|
|
|
|
|
|
|
|
|
|
image.setAbsolutePosition(Float.parseFloat(location[0]), Float.parseFloat(location[1]));
|
|
|
|
|
float originalWidth = image.getWidth();
|
|
|
|
|
float originalHeight = image.getHeight();
|
|
|
|
|
System.out.println(originalWidth);
|
|
|
|
|
image.scaleAbsolute(originalWidth * Float.parseFloat(location[2]), originalHeight * Float.parseFloat(location[2]));
|
|
|
|
|
|
|
|
|
|
image.setRotationDegrees(0);
|
|
|
|
|
content.addImage(image);
|
|
|
|
|
|
|
|
|
|
content.beginText();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(color+"====================color");
|
|
|
|
|
if(color != null && color.split(",").length == 3) {
|
|
|
|
|
String[] split = color.split(",");
|
|
|
|
|
System.out.println("split[0]=="+split[0]);
|
|
|
|
|
System.out.println("split[1]=="+split[1]);
|
|
|
|
|
System.out.println("split[2]=="+split[2]);
|
|
|
|
|
content.setColorFill(new BaseColor(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2])));
|
|
|
|
|
}else {
|
|
|
|
|
content.setColorFill(BaseColor.RED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("location[5]=="+location[5]);
|
|
|
|
|
System.out.println("location[4]=="+location[4]);
|
|
|
|
|
System.out.println("location[3]=="+location[3]);
|
|
|
|
|
content.setFontAndSize(base, Float.parseFloat(location[5]));
|
|
|
|
|
|
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, dateValue,
|
|
|
|
|
Float.parseFloat(String.valueOf(location[3])),
|
|
|
|
|
Float.parseFloat(String.valueOf(location[4])), 0);
|
|
|
|
|
|
|
|
|
|
content.setTextMatrix(70, 200);
|
|
|
|
|
content.endText();
|
|
|
|
|
}
|
|
|
|
|
stamp.close();
|
|
|
|
|
reader.close();// 关闭
|
|
|
|
|
|
|
|
|
|
String finalStr = length+";"+wide+";"+type+";"+page;
|
|
|
|
|
System.out.println("finalStr==="+finalStr);
|
|
|
|
|
writeStringToPdfMessageFile(finalStr);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
inputStreamPdf.close();
|
|
|
|
|
} catch (Exception ex1) {
|
|
|
|
|
ex1.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return outPdfFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
int rotation = rectangle.getRotation();
|
|
|
|
|
sizef[0] = Float.valueOf(toright - toleft);
|
|
|
|
|
sizef[1] = Float.valueOf(totop - tobottom);
|
|
|
|
|
|
|
|
|
|
// if (sizef[0].floatValue() < sizef[1].floatValue())
|
|
|
|
|
// {
|
|
|
|
|
// float cha = sizef[0].floatValue();
|
|
|
|
|
// sizef[0] = sizef[1];
|
|
|
|
|
// sizef[1] = Float.valueOf(cha);
|
|
|
|
|
// }
|
|
|
|
|
System.out.println(
|
|
|
|
|
"Left=" + toleft + "|Right=" + toright + "|Top=" + totop + "|Bottom=" + tobottom + "|RD=" + rotation);
|
|
|
|
|
System.out.println("W=" + sizef[0]);
|
|
|
|
|
System.out.println("H=" + sizef[1]);
|
|
|
|
|
return sizef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void writeStringToPdfMessageFile(String content) {
|
|
|
|
|
// 定义文件路径
|
|
|
|
|
File file = new File( System.getenv("TEMP")+"\\pdfMessage.txt");
|
|
|
|
|
|
|
|
|
|
// 如果文件存在,先删除
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
if (!file.delete()) {
|
|
|
|
|
System.err.println("无法删除原有文件: " + file.getAbsolutePath());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建新文件并写入内容
|
|
|
|
|
try (FileWriter writer = new FileWriter(file)) {
|
|
|
|
|
writer.write(content);
|
|
|
|
|
writer.close();
|
|
|
|
|
System.out.println("文件写入成功: " + file.getAbsolutePath());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("写入文件时发生错误: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|