package com.hand.hcm.dwp.controllers;
import java.awt.FontMetrics;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JLabel;
import com.hand.hap.system.dto.ResponseData;
import com.hand.ocbm.constants.Constants;
import com.hand.ocbm.utils.PdfUtil;
import com.itextpdf.text.Element;
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 com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/***
*@Name zjx
*@data 2019/5/13 9:54
*@version:1.0
*/
@Controller
public class TestwaterMark {
@RequestMapping(value = "/dwp/pdf")
public ResponseData pdf(HttpServletResponse response){
waterMark("E://Teleforwarding.pdf", null, "CONTRACT",response);
return new ResponseData(true);
}
private static int interval = -5;
public static void waterMark(String inputFile,
String outputFile, String waterMarkName,HttpServletResponse response) {
PdfReader reader = null;
PdfStamper stamper = null;
FileOutputStream os = null;
InputStream is = null;
try {
is = new FileInputStream(inputFile);
reader = new PdfReader(is);
os = new FileOutputStream(inputFile);
stamper = new PdfStamper(reader, os);
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
Rectangle pageRect = null;
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.3f);
gs.setStrokeOpacity(0.4f);
int total = reader.getNumberOfPages() + 1;
JLabel label = new JLabel();
FontMetrics metrics;
int textH = 0;
int textW = 0;
label.setText(waterMarkName);
metrics = label.getFontMetrics(label.getFont());
textH = metrics.getHeight();
textW = metrics.stringWidth(label.getText());
PdfContentByte under;
for (int i = 1; i < total; i++) {
pageRect = reader.getPageSizeWithRotation(i);
under = stamper.getOverContent(i);
under.saveState();
under.setGState(gs);
under.beginText();
under.setFontAndSize(base, 20);
// 水印文字成30度角倾斜
//你可以随心所欲的改你自己想要的角度
for (int height = 0; height <4; ++ height) {
for (int width = interval + textW; width < pageRect.getWidth() + textW;
width = width + textW*2) {
under.showTextAligned(1
, waterMarkName, width,
height - textH, 30);
}
}
/* for (int height = interval + textH; height < pageRect.getHeight();
height = height + textH*3) {
for (int width = interval + textW; width < pageRect.getWidth() + textW;
width = width + textW*2) {
under.showTextAligned(Element.ALIGN_LEFT
, waterMarkName, width - textW,
height - textH, 30);
}
}*/
for (int l = 0; l < 4; ++l) {
for (int j = 0; j < 10; ++j) {
under.showTextAligned(0, waterMarkName, 50.5F + (float) (l * 300), 40.0F + (float) (j * 250), 30);
}
}
// 添加水印文字
under.endText();
}
stamper.close();
reader.close();
/*response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment;filename="+"232");
response.setCharacterEncoding(Constants.ENCODED_UTF_8);
byte[]buff=new byte[1024*10];//可以自己 指定缓冲区的大小
int len=0;
OutputStream output = response.getOutputStream();
while((len=is.read(buff))>-1)
{
output.write(buff,0,len);
}*/
//说三遍
//一定不要忘记关闭流
//一定不要忘记关闭流
//一定不要忘记关闭流
/* is.close();
output.close();*/
} catch (Exception e) {
e.printStackTrace();
}finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
}
/*
public static void main(String[] args) {
waterMark("E/Teleforwarding+PCR001-For+Tunisia+TFN.pdf", "E:/te.pdf", "我是黑豆");
}*/
}
https://blog.csdn.net/duan1309/article/details/81232300