一行水印
/**
* @param inputFile 你的PDF文件地址
* @param outputFile 添加水印后生成PDF存放的地址
* @param waterMarkName 你的水印
* @return
*/
public static boolean waterMark(String inputFile,String outputFile, String waterMarkName)
{
try {
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
//这里的字体设置比较关键,这个设置是支持中文的写法
BaseFont base = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
for (int i = 1; i < total; i++)
{
pageRect = stamper.getReader().getPageSizeWithRotation(i);
// 计算水印X,Y坐标
float x = pageRect.getWidth()/2;
float y = pageRect.getHeight()/2;
// 获得PDF最顶层
under = stamper.getOverContent(i);
under.saveState();
// set Transparency
PdfGState gs = new PdfGState();
// 设置透明度为0.2
gs.setFillOpacity(0.2f);
under.setGState(gs);
under.beginText();
under.setFontAndSize(base, 40);
under.setColorFill(BaseColor.GRAY);
// 水印文字成45度角倾斜
under.showTextAligned(Element.ALIGN_CENTER , waterMarkName, x,y, 55);
// 添加水印文字
under.endText();
under.setLineWidth(1f);
under.stroke();
under.restoreState();
}
stamper.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
两行多行水印可以这样
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
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;
/**
* app检测报告加水印
*
* @author: liuhy
* @param inputStream 输入流
* @param outputStream 输出流
* @param waterMarkName1 水印文字1
* @param waterMarkName2 水印文字2
* @return
*/
public static boolean apptReportWaterMark(InputStream inputStream,OutputStream outputStream, String waterMarkName1,String waterMarkName2)
{
try {
PdfReader reader = new PdfReader(inputStream);
PdfStamper stamper = new PdfStamper(reader, outputStream);
BaseFont base = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
//这里的字体设置比较关键,这个设置是支持中文的写法
// BaseFont base = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
for (int i = 1; i < total; i++)
{
pageRect = stamper.getReader().getPageSizeWithRotation(i);
// 计算水印X,Y坐标
float x = pageRect.getWidth()/2;
float y = pageRect.getHeight()/2;
// 获得PDF最顶层
under = stamper.getOverContent(i);
under.saveState();
// set Transparency
PdfGState gs = new PdfGState();
// 设置透明度为0.2
gs.setFillOpacity(0.2f);
under.setGState(gs);
//****第一行水印开始
under.beginText();
under.setFontAndSize(base, 60);
under.setColorFill(BaseColor.GRAY);
// 水印文字成45度角倾斜
under.showTextAligned(Element.ALIGN_CENTER , waterMarkName1, x,y+50, 20);
// 添加水印文字
under.endText();
//****第一行水印结束
//****第二行水印开始
under.beginText();
under.setFontAndSize(base, 36);
under.setColorFill(BaseColor.GRAY);
// 水印文字成45度角倾斜
under.showTextAligned(Element.ALIGN_CENTER , waterMarkName2, x,y, 20);
// 添加水印文字
under.endText();
//****第二行水印结束
under.setLineWidth(1f);
under.stroke();
under.restoreState();
}
stamper.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
maven
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>