给已经存在的PDF文件加水印
1 static public bool WatermarkPDF(string SourcePdfPath, string OutputPdfPath, string WatermarkPath, int positionX, int positionY, int WatermarkHeight, int WatermarkWidth, out string msg) 2 { 3 try 4 { 5 PdfReader reader = new PdfReader(SourcePdfPath); 6 PdfStamper stamp = new PdfStamper(reader, new FileStream(OutputPdfPath, FileMode.Create)); 7 int n = reader.NumberOfPages; 8 int i = 0; 9 PdfContentByte under; 10 iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(WatermarkPath); 11 im.SetAbsolutePosition(positionX, positionY); 12 im.ScaleAbsolute(WatermarkWidth, WatermarkHeight); 13 14 while (i < n) 15 { 16 i++; 17 under = stamp.GetUnderContent(i); 18 under.AddImage(im, true); 19 20 } 21 stamp.Close(); 22 reader.Close(); 23 } 24 catch (Exception ex) 25 { 26 msg = ex.Message; 27 return false; 28 } 29 msg = "加水印成功!"; 30 return true; 31 }
调用方法
WatermarkPDF("N.pdf", "N1.pdf", "Signer.JPG", 200, 200, 100, 200, out msg)