You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
382 lines
10 KiB
382 lines
10 KiB
package com.archive.common.archiveUtil;
|
|
|
|
import com.itextpdf.text.Image;
|
|
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 java.awt.Font;
|
|
import java.awt.FontMetrics;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import sun.font.FontDesignMetrics;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PdfWaterMarkUtil
|
|
{
|
|
private static int interval = 20;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void waterMarkforWriting(String inputFile, String outputFile, String waterMarkName, Integer degree, float alpha, int fontSize) {
|
|
try {
|
|
File file = new File(outputFile);
|
|
if (!file.getParentFile().exists()) {
|
|
file.getParentFile().mkdirs();
|
|
}
|
|
PdfReader reader = new PdfReader(inputFile);
|
|
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
|
|
|
|
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);
|
|
Rectangle pageRect = null;
|
|
PdfGState gs = new PdfGState();
|
|
gs.setFillOpacity(alpha);
|
|
|
|
int total = reader.getNumberOfPages() + 1;
|
|
FontMetrics metrics = FontDesignMetrics.getMetrics(new Font("宋体", 0, fontSize));
|
|
int textH = metrics.getHeight();
|
|
int textW = metrics.stringWidth(waterMarkName);
|
|
|
|
for (int i = 1; i < total; i++) {
|
|
pageRect = reader.getPageSizeWithRotation(i);
|
|
PdfContentByte under = stamper.getOverContent(i);
|
|
under.saveState();
|
|
under.setGState(gs);
|
|
under.beginText();
|
|
under.setFontAndSize(base, fontSize);
|
|
int height;
|
|
for (height = interval + textH; height < pageRect.getHeight(); height += textH * 3) {
|
|
int width; for (width = interval + textW; width < pageRect.getWidth() + textW; width += textW * 2) {
|
|
under.showTextAligned(0, waterMarkName, height, width, degree.intValue());
|
|
}
|
|
}
|
|
|
|
under.endText();
|
|
}
|
|
stamper.close();
|
|
reader.close();
|
|
System.out.println("**********PDF完成添加文字水印工作**********");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void waterMarkforWriting(String inputFile, String outputFile, String waterMarkName, Integer degree, float alpha, int fontSize, String watermarkPosition, Integer left, Integer top) {
|
|
try {
|
|
File file = new File(outputFile);
|
|
if (!file.getParentFile().exists()) {
|
|
file.getParentFile().mkdirs();
|
|
}
|
|
PdfReader reader = new PdfReader(inputFile);
|
|
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
|
|
|
|
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);
|
|
Rectangle pageRect = null;
|
|
PdfGState gs = new PdfGState();
|
|
gs.setFillOpacity(alpha);
|
|
|
|
int total = reader.getNumberOfPages() + 1;
|
|
|
|
FontMetrics metrics = FontDesignMetrics.getMetrics(new Font("宋体", 0, fontSize));
|
|
int textH = metrics.getHeight();
|
|
int textW = metrics.stringWidth(waterMarkName);
|
|
|
|
for (int i = 1; i < total; i++) {
|
|
pageRect = reader.getPageSizeWithRotation(i);
|
|
PdfContentByte under = stamper.getOverContent(i);
|
|
under.saveState();
|
|
under.setGState(gs);
|
|
under.beginText();
|
|
under.setFontAndSize(base, fontSize);
|
|
int pdfWidth = (int)pageRect.getWidth();
|
|
int pdfHeight = (int)pageRect.getHeight();
|
|
|
|
int x = left.intValue();
|
|
int y = top.intValue();
|
|
if (x == 0 && y == 0) {
|
|
switch (watermarkPosition) {
|
|
case "bottomLeft":
|
|
x = 0;
|
|
y = 3;
|
|
break;
|
|
case "bottomRight":
|
|
x = pdfWidth - textW;
|
|
y = 3;
|
|
break;
|
|
case "bottomCenter":
|
|
x = (pdfWidth - textW) / 2;
|
|
y = 3;
|
|
break;
|
|
case "center":
|
|
x = (pdfWidth - textW) / 2;
|
|
y = (pdfHeight - textH) / 2;
|
|
break;
|
|
case "centerLeft":
|
|
x = 0;
|
|
y = (pdfHeight - textH) / 2;
|
|
break;
|
|
case "centerRight":
|
|
x = pdfWidth - textW;
|
|
y = (pdfHeight - textH) / 2;
|
|
break;
|
|
case "topLeft":
|
|
x = 0;
|
|
y = pdfHeight - textH;
|
|
break;
|
|
case "topCenter":
|
|
x = (pdfWidth - textW) / 2;
|
|
y = pdfHeight - textH;
|
|
break;
|
|
case "topRight":
|
|
x = pdfWidth - textW;
|
|
y = pdfHeight - textH;
|
|
break;
|
|
default:
|
|
x = pdfWidth - textW;
|
|
y = 3;
|
|
break;
|
|
}
|
|
}
|
|
under.showTextAligned(0, waterMarkName, x, y, degree.intValue());
|
|
|
|
under.endText();
|
|
}
|
|
stamper.close();
|
|
reader.close();
|
|
System.out.println("**********PDF完成添加文字水印工作**********");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void pressImageWater(String srcPath, String targetPath, String imgPath, int x, int y) {
|
|
PdfReader reader = null;
|
|
PdfStamper stamp = null;
|
|
|
|
try {
|
|
reader = new PdfReader(srcPath);
|
|
|
|
File targetFile = new File(targetPath);
|
|
if (!targetFile.getParentFile().exists()) {
|
|
targetFile.getParentFile().mkdirs();
|
|
}
|
|
|
|
stamp = new PdfStamper(reader, new FileOutputStream(targetFile));
|
|
Image img = Image.getInstance(imgPath);
|
|
|
|
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
|
|
|
|
PdfContentByte over = stamp.getOverContent(i);
|
|
img.setAbsolutePosition(x, y);
|
|
|
|
over.addImage(img);
|
|
}
|
|
stamp.close();
|
|
reader.close();
|
|
System.out.println("**********PDF完成添加图片水印工作**********");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void pressImageWater(String srcPath, String targetPath, String imgPath, int x, int y, int r) {
|
|
PdfReader reader = null;
|
|
PdfStamper stamp = null;
|
|
|
|
try {
|
|
reader = new PdfReader(srcPath);
|
|
|
|
File targetFile = new File(targetPath);
|
|
if (!targetFile.getParentFile().exists()) {
|
|
targetFile.getParentFile().mkdirs();
|
|
}
|
|
|
|
stamp = new PdfStamper(reader, new FileOutputStream(targetFile));
|
|
Image img = Image.getInstance(imgPath);
|
|
|
|
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
|
|
|
|
PdfContentByte over = stamp.getOverContent(i);
|
|
img.setAbsolutePosition(x, y);
|
|
|
|
img.setRotationDegrees(r);
|
|
|
|
over.addImage(img);
|
|
}
|
|
stamp.close();
|
|
reader.close();
|
|
System.out.println("**********PDF完成添加图片水印工作**********");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void pressImageWater(String srcPath, String targetPath, String imgPath, String position, Integer left, Integer top) {
|
|
PdfReader reader = null;
|
|
PdfStamper stamp = null;
|
|
|
|
try {
|
|
reader = new PdfReader(srcPath);
|
|
|
|
File targetFile = new File(targetPath);
|
|
if (!targetFile.getParentFile().exists()) {
|
|
targetFile.getParentFile().mkdirs();
|
|
}
|
|
|
|
stamp = new PdfStamper(reader, new FileOutputStream(targetFile));
|
|
Image img = Image.getInstance(imgPath);
|
|
|
|
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
|
|
|
|
PdfContentByte over = stamp.getOverContent(i);
|
|
|
|
Rectangle pageSize = reader.getPageSize(i);
|
|
int srcWth = (int)pageSize.getWidth();
|
|
int srcHht = (int)pageSize.getHeight();
|
|
int imgWth = (int)img.getWidth();
|
|
int imgHht = (int)img.getHeight();
|
|
|
|
int x = left.intValue();
|
|
int y = top.intValue();
|
|
if (x == 0 && y == 0) {
|
|
switch (position) {
|
|
case "topLeft":
|
|
x = 0;
|
|
y = srcHht - imgHht;
|
|
break;
|
|
case "topCenter":
|
|
x = (srcWth - imgWth) / 2;
|
|
y = srcHht - imgHht;
|
|
break;
|
|
case "topRight":
|
|
x = srcWth - imgWth;
|
|
y = srcHht - imgHht;
|
|
break;
|
|
case "center":
|
|
x = (srcWth - imgWth) / 2;
|
|
y = (srcHht - imgHht) / 2;
|
|
break;
|
|
case "centerLeft":
|
|
x = 0;
|
|
y = (srcHht - imgHht) / 2;
|
|
break;
|
|
case "centerRight":
|
|
x = srcWth - imgWth;
|
|
y = (srcHht - imgHht) / 2;
|
|
break;
|
|
case "bottomLeft":
|
|
x = 0;
|
|
y = 0;
|
|
break;
|
|
case "bottomRight":
|
|
x = srcWth - imgWth;
|
|
y = 0;
|
|
break;
|
|
case "bottomCenter":
|
|
x = (srcWth - imgWth) / 2;
|
|
y = 0;
|
|
break;
|
|
default:
|
|
x = srcWth - imgWth;
|
|
y = 0;
|
|
break;
|
|
}
|
|
}
|
|
img.setAbsolutePosition(x, y);
|
|
|
|
over.addImage(img);
|
|
}
|
|
stamp.close();
|
|
reader.close();
|
|
System.out.println("**********PDF完成添加文字水印工作**********");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
String iconPath = "C:\\Users\\18291\\Desktop\\watermark\\watermark2.png";
|
|
String srcPdfPath = "C:\\Users\\18291\\Desktop\\watermark\\0101-2001-长期-办公室-0005.pdf";
|
|
String outPdfPath = "C:\\Users\\18291\\Desktop\\watermark\\1\\2\\w3.pdf";
|
|
String contont = "Font类是用于设置图形用户界面上的字体样式";
|
|
}
|
|
}
|
|
|
|
|
|
/* Location: C:\Users\Administrator\Desktop\extracted.zip!\extracted\BOOT-INF\classes\com\archive\common\archiveUtil\PdfWaterMarkUtil.class
|
|
* Java compiler version: 8 (52.0)
|
|
* JD-Core Version: 1.1.3
|
|
*/ |