码迷,mamicode.com
首页 > Windows程序 > 详细

C#添加文字水印

时间:2017-03-22 16:06:38      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:ndt   大小   param   throw   state   sha   倾斜   fonts   ++   

使用的是iTextSharp添加PDF水印,由于是接口动态生成PDF,所以采用的是全部是内存流的形式,而且水印是平铺是。iTextSharp版本是5.5

 1 /// <summary>
 2         /// 添加倾斜水印
 3         /// </summary>
 4         /// <param name="pdfStream">pdf文件流</param>
 5         /// <param name="waterMarkName">水印字符串</param>
 6         /// <param name="width">页面宽度</param>
 7         /// <param name="height">页面高度</param>
 8         public MemoryStream SetWaterMark(MemoryStream pdfStream, string waterMarkName, float width, float height)
 9         {
10             try
11             {
12                 int fontSize = 50;//设置字体大小
13                 int span = 40;//设置垂直位移
14                 MemoryStream outStream = new MemoryStream();
15                 PdfReader pdfReader = new PdfReader(pdfStream);
16                 PdfStamper pdfStamper = new PdfStamper(pdfReader, outStream);
17                 pdfStamper.Writer.CloseStream = false;
18                 int total = pdfReader.NumberOfPages + 1;
19                 PdfContentByte content;
20                 BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\STCAIYUN.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//华文云彩字体
21                 PdfGState gs = new PdfGState();
22                 gs.FillOpacity = 0.15f;//透明度
23                 int waterMarkNameLenth = waterMarkName.Length;
24                 char c;
25                 int rise = 0;
26                 string spanString = " ";//水平位移
27                 for (int i = 1; i < total; i++)
28                 {
29                     rise = waterMarkNameLenth * span;
30                     content = pdfStamper.GetOverContent(i);//在内容上方加水印
31                                                            //content = pdfStamper.GetUnderContent(i);//在内容下方加水印
32                     content.SetGState(gs);
33                     content.BeginText();
34                     content.SetColorFill(BaseColor.GREEN);
35                     content.SetFontAndSize(font, fontSize);
36                     int heightNumbert = (int)Math.Ceiling((decimal)height / (decimal)rise);//垂直重复的次数,进一发
37                     int panleWith = (fontSize + span) * waterMarkNameLenth;
38                     int widthNumber = (int)Math.Ceiling((decimal)width / (decimal)panleWith);//水平重复次数
39                    
40                     // 设置水印文字字体倾斜 开始 
41                     for (int w = 0; w < widthNumber; w++)
42                     {
43                         for (int h = 1; h <= heightNumbert; h++)
44                         {
45                             int yleng = rise * h;
46                             content.SetTextMatrix(w * panleWith, yleng);//x,y设置水印开始的绝对左边,以左下角为x,y轴的起点
47                             for (int k = 0; k < waterMarkNameLenth; k++)
48                             {
49                                 content.SetTextRise(yleng);//指定的y轴值处添加
50                                 c = waterMarkName[k];
51                                 content.ShowText(c + spanString);
52                                 yleng -= span;
53                             }
54                         }
55                     }
56                     content.EndText();
57                 }
58                 if (pdfStamper != null)
59                     pdfStamper.Close();
60 
61                 if (pdfReader != null)
62                     pdfReader.Close();
63 
64                 return outStream;
65             }
66             catch (Exception ex)
67             {
68                 throw ex;
69             }
70         }

 

C#添加文字水印

标签:ndt   大小   param   throw   state   sha   倾斜   fonts   ++   

原文地址:http://www.cnblogs.com/albertay/p/6600357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!