iTextSharp水印平铺
/// <summary> /// 加平铺图片水印 /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="ModelPicName"></param> /// <returns></returns> public static bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName) { //throw new NotImplementedException(); PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); int numberOfPages = pdfReader.NumberOfPages; iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1); float width = psize.Width; float height = psize.Height; pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create)); PdfContentByte waterMarkContent; PdfGState gs = new PdfGState(); gs.FillOpacity = 0.2f; iTextSharp.text.Image imgTemp = iTextSharp.text.Image.GetInstance(ModelPicName); float imgWidth = (float) Math.Cos(Math.PI / 4) * imgTemp.Width + 100; float imgHeight = (float) Math.Sin(Math.PI / 4) * imgTemp.Width + 100; //每一页加水印,也可以设置某一页加水印 for (int i = 1; i <= numberOfPages; i++) { //waterMarkContent = pdfStamper.GetUnderContent(i);//内容下层加水印 waterMarkContent = pdfStamper.GetOverContent(i);//内容上层加水印 waterMarkContent.SetGState(gs); for (float left = 0; left < width; left += imgWidth) { for (float top = 0; top < height; top += imgHeight) { iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName); image.GrayFill = 100;//透明度,灰色填充 //image.Rotation = 45;//旋转 image.RotationDegrees = 45;//旋转角度 image.SetAbsolutePosition(left, height - image.Height - top); Console.WriteLine(left +":"+ (height - image.Height - top)); waterMarkContent.AddImage(image); } } } //strMsg = "success"; return true; } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) pdfStamper.Close(); if (pdfReader != null) pdfReader.Close(); } }