@SneakyThrows
@Override
public List<LocalStorageWater> addWater(WaterVo waterVo) {
List<LocalStorageWater> localStorageWaters = new ArrayList<>();
for (Integer id : waterVo.getIds()) {
LocalStorage localStorage = localStorageService.getById(id);
// 创建一个Document对象(pdf文档) A4纸张大小
// Document document = new Document(PageSize.A4);
// 建立一个书写器(Writer)与document对象关联
// PdfWriter.getInstance(document, new FileOutputStream(localStorage.getPath()));
File file = new File(localStorage.getPath());
//输入文件
PdfReader pdfReader = new PdfReader(localStorage.getPath());
// PdfReader pdfReader = new PdfReader("https://eqmt.cdpll.cn/C20210000008870stp.pdf");
//获取文件的大小
// Rectangle pageSize = pdfReader.getPageSize(1);
// float height = pageSize.getHeight();
// float width = pageSize.getWidth();
// System.out.println("width = "+width+", height = "+height);
//获取当前时间,修改文件名,拼接输出文件路径
// String now = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
String oldFilename = FileUtil.getFileNameNoEx(localStorage.getRealName());
// String newFilename = oldFilename+"-pll"+now+"."+FileUtil.getExtensionName(localStorage.getRealName());
String url = file.getParent() + "/" + oldFilename;
//输出文件路径
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(url));
//字体设置,支持中文写法
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
//文本颜色,字符串转数组
String[] str = waterVo.getTextColor().split(",");
int textColor[] = Arrays.stream(str).mapToInt(Integer::parseInt).toArray();
PdfContentByte pdfContentByte;
//页数
int total = pdfReader.getNumberOfPages() + 1;
for (int i = 1; i < total; i++) {
Rectangle pageRect = pdfStamper.getReader().getPageSizeWithRotation(1);
// 计算水印每个单位步长X,Y
float width = pageRect.getWidth();
float height = pageRect.getHeight();
//水印的起始,获得PDF最顶层
pdfContentByte = pdfStamper.getOverContent(i);
pdfContentByte.beginText();
pdfContentByte.saveState();
//设置透明度
PdfGState gs = new PdfGState();
gs.setFillOpacity(waterVo.getTextOpacity());
pdfContentByte.setGState(gs);
//水印个数
int ty = waterVo.getTextYAmount();
//获取水印的长度
float sw = waterVo.getText().length();
//文本字体、文本大小
int sizes = waterVo.getTextSize();
String text = waterVo.getText();
String newText = "";
System.out.println("字体大小sizes====================================================================" + sizes);
//纸张宽度
System.out.println("纸张宽度width====================================================================" + width);
//纸张高度
System.out.println("纸张高度height====================================================================" + height);
//水印字数
System.out.println("水印长度sw====================================================================" + sw);
System.out.println("水印个数ty====================================================================" + ty);
System.out.println("文本内容===============================" + text);
// if (sw * ty > width) {
//
// pdfContentByte.setFontAndSize(baseFont, width / (ty + sw)-12 + sizes);
// } else {
// if (width < height) {
// pdfContentByte.setFontAndSize(baseFont, width / (ty + sw) -12 + sizes);
//
// } else {
// pdfContentByte.setFontAndSize(baseFont, height / (ty + sw) -12 + sizes);
//
// }
// }
//文本颜色
pdfContentByte.setColorFill(new BaseColor(textColor[0], textColor[1], textColor[2]));
pdfContentByte.setFontAndSize(baseFont, width / (ty + sw)-12 + sizes);
//开始写入水印,水印文字倾斜
for (int y = 0; y < ty; y++) {
//for (int x = 0; x < tx; x++) {
// rotation:倾斜角度
if (sw > 15) {
//长度 /2截取,保留小数首位
float v = sw / 2;
int a;
//强制类型转四舍五入
a = (int) (v + 0.5);
//第一排
String oneRow = text.substring(0, a);
//第二排
String twoRow = text.substring(a);
newText = oneRow + "\n" + twoRow;
System.out.println("新换行===================================" + newText);
//字数大于12,水印行数大于3,开始换行
if (ty > 1) {
pdfContentByte.setFontAndSize(baseFont, (width / (ty+sw)-11 + sizes));
for (int j = 0; j < 2; j++) {
pdfContentByte.showTextAligned(Element.ALIGN_CENTER, oneRow, width / (ty + 1) + y * width / (ty + 2), height - height / (ty + 1) - y * height / (ty + 1), waterVo.getTextRotation());
pdfContentByte.showTextAligned(Element.ALIGN_CENTER, twoRow, width / (ty + 1) + y * width / (ty + 2)+width / (ty + sw)-10 + sizes, height - height / (ty + 1) - y * height / (ty + 1)-width / (ty + sw) - 2, waterVo.getTextRotation());
}
//否则不换行
} else {
pdfContentByte.showTextAligned(Element.ALIGN_CENTER, waterVo.getText(), width / (ty + 1) + y * width / (ty + 2), height - height / (ty + 1) - y * height / (ty + 1), waterVo.getTextRotation());
}
//字数不大于12,不换行
} else {
pdfContentByte.showTextAligned(Element.ALIGN_CENTER, waterVo.getText(), width / (ty + 1) + y * width / (ty + 2), height - height / (ty + 1) - y * height / (ty + 1), waterVo.getTextRotation());
}
//}
}
//结束
pdfContentByte.endText();
pdfContentByte.setLineWidth(1f);
pdfContentByte.stroke();
}
//关闭输出流
//pdfReader.close();
pdfStamper.close();
//水印参数数据库存档
LocalStorageWater lsw = new LocalStorageWater();
lsw.setCate(localStorageCateService.getById(localStorage.getCate()).getName());
lsw.setContent(waterVo.getText());
lsw.setOperate(SecurityUtils.getUserDetails().getUsername());
lsw.setFilename(localStorage.getName());
lsw.setUrl(oldFilename);
waterVo.setName(localStorage.getName());
lsw.setWatervo(JSONObject.toJSONString(waterVo));
// lsw.setUse(waterVo.getUse());
// lsw.setIds(JSONObject.toJSONString(waterVo.getIds()));
localStorageWaterMapper.insert(lsw);
localStorageWaters.add(lsw);
}
return localStorageWaters;
}
超过指定长度换行