///
/// 设置水印
/// 引用组件itextsharp
///
///
///
///
///
///
///
private string setWatermark(string inputfilepath, string outputfiledir, string waterMarkName, string fontFamily, int fontSize)
{
try
{
using (PdfReader pdfReader = new PdfReader(inputfilepath))
{
var filename = Path.GetFileNameWithoutExtension(inputfilepath) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
var outputfilepathtemp = Path.Combine(outputfiledir, filename);
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepathtemp, FileMode.Create)))
{
int total = pdfReader.NumberOfPages;
PdfContentByte content;
BaseFont font = BaseFont.CreateFont(fontFamily, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i <= total; i++)
{
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(i);
float width = psize.Width;
float height = psize.Height;
content = pdfStamper.GetOverContent(i);//在内容上方加水印
//透明度
gs.FillOpacity = 0.3f;
content.SetGState(gs);
//content.SetGrayFill(0.3f);
//开始写入文本
content.BeginText();
content.SetColorFill(BaseColor.LIGHT_GRAY);
content.SetFontAndSize(font, fontSize);
content.SetTextMatrix(0, 0);
content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2, height / 2 - 45, 45);
content.EndText();
}
return outputfilepathtemp;
}
}
}
catch (Exception ex)
{
throw ex;
}
}