引用
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
工具类
public static void stringWaterMark(byte[] bytes, FileOutputStream output, String waterMarkString, int xAmout, int yAmout, float opacity, float rotation, int waterMarkFontSize, BaseColor color) {
try {
PdfReader reader = new PdfReader(bytes);
PdfReader.unethicalreading = true;
PdfStamper stamper = new PdfStamper(reader, output);
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
int total = reader.getNumberOfPages() + 1;
PdfContentByte over;
for (int i = 1; i < total; i++) {
Rectangle pageRect = stamper.getReader().getPageSizeWithRotation(i);
xAmout += (waterMarkString.length() * 10);
float x = pageRect.getWidth() / xAmout;
float y = pageRect.getHeight() / yAmout;
over = stamper.getOverContent(i);
PdfGState gs = new PdfGState();
gs.setFillOpacity(opacity);
over.setGState(gs);
over.saveState();
over.beginText();
over.setColorFill(color);
over.setFontAndSize(baseFont, waterMarkFontSize);
for (int n = 0; n < x + 1; n++) {
for (int m = 0; m < y + 1; m++) {
BigDecimal xAmoutBig = new BigDecimal(xAmout);
BigDecimal yAmoutBig = new BigDecimal(yAmout);
BigDecimal mBig = new BigDecimal(m);
BigDecimal nBig = new BigDecimal(n);
Float x_afferent = xAmoutBig.multiply(nBig).floatValue();
Float y_afferent = yAmoutBig.multiply(mBig).floatValue();
over.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x_afferent, y_afferent, rotation);
}
}
over.endText();
}
stamper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void downFile(HttpServletResponse response, String fileName, String uploadPath) {
String filePath = uploadPath + File.separator + fileName;
File file = new File(filePath);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
FileInputStream fis = null;
OutputStream myout = null;
BufferedInputStream buff = null;
try {
if (file.exists()) {
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName).getBytes("UTF-8"), "ISO8859-1"));
response.setContentLength((int) file.length());
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setContentType("application/octet-stream");
fis = new FileInputStream(file);
buff = new BufferedInputStream(fis);
byte[] b = new byte[1024];
long k = 0;
myout = response.getOutputStream();
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (myout != null) {
try {
myout.flush();
myout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (buff != null) {
try {
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
if (file.delete()) {
LOGGER.info("删除单个文件" + filePath + "成功!");
return true;
} else {
LOGGER.error("删除单个文件" + filePath + "失败!");
return false;
}
} else {
LOGGER.error("删除单个文件失败:" + filePath + "不存在!");
return false;
}
}
调用类
public void queryView(HttpServletResponse response, String viewNo, String pageNo) {
String url = "http://....";
String outViewUrl = "D:/";
String fileName = "测试.pdf";
String outUrl = outViewUrl + File.separator + fileName;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
File file = new File(outUrl);
Boolean fileCreate = true;
if (!file.exists()) {
Files.createDirectories(Path.of(file.getParent()));
fileCreate = file.createNewFile();
}
if (fileCreate) {
fileOutputStream = new FileOutputStream(file);
URL urlConn = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConn.openConnection();
httpURLConnection.setRequestMethod("GET");
Integer code = httpURLConnection.getResponseCode();
if (code == 200) {
inputStream = httpURLConnection.getInputStream();
byte[] bytes = inputStream.readAllBytes();
HttpRequest.stringWaterMark(bytes, fileOutputStream, waterMarkString, 100, 100, 0.2f, 45, 20, new BaseColor(165, 165, 165));
HttpRequest.downFile(response, fileName, outViewUrl);
}
HttpRequest.deleteFile(outUrl);
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
if (inputStream != null){
try{
inputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
}