您是第欢迎光临我的主页位访客
浩凡儿
天行健,君子以自强不息;地势坤,君子以厚德载物!
  1 调用直接这样用:
  2  //PDFHelper.AddImageWatermarkPDF(path, "D://my.pdf", Server.MapPath("/HtmlToPdf/Tools/sy.bmp"), 0, 0);
  3             string temp = DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString();
  4             Guid guid = new Guid();
  5             guid = Guid.NewGuid();
  6 
  7             string tempGUID = guid.ToString();
  8             File.Copy("E:\\1.pdf", "E:\\11.pdf", true);
  9             PDFHelper.AddWordWatermark2PDF("E:\\11.pdf", "E:\\1.pdf", "西安咸阳国际机场南三指廊航站楼工程施工及施工管理总承包" + "_" + temp + "_" + tempGUID + "_7.1");
 10             //PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST", "", "", 1);
 11 
 12 
 13 using iTextSharp.text;
 14 using iTextSharp.text.pdf;
 15 using System;
 16 using System.Collections.Generic;
 17 using System.IO;
 18 using System.Linq;
 19 using System.Text;
 20 using System.Threading.Tasks;
 21 
 22 namespace Test测试
 23 {
 24     public static class PDFHelper
 25     {
 26         
 27 
 28         /// <summary>
 29         /// 加图片水印
 30         /// </summary>
 31         /// <param name="inputfilepath"></param>
 32         /// <param name="outputfilepath"></param>
 33         /// <param name="ModelPicName"></param>
 34         /// <param name="top"></param>
 35         /// <param name="left"></param>
 36         /// <returns></returns>
 37         public static bool AddImageWatermarkPDF(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)
 38         {
 39             //throw new NotImplementedException();
 40             PdfReader pdfReader = null;
 41             PdfStamper pdfStamper = null;
 42             try
 43             {
 44                 pdfReader = new PdfReader(inputfilepath);
 45 
 46                 int numberOfPages = pdfReader.NumberOfPages;
 47 
 48                 iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
 49 
 50                 float width = psize.Width;
 51 
 52                 float height = psize.Height;
 53 
 54                 pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
 55 
 56                 PdfContentByte waterMarkContent;
 57 
 58                 iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);
 59 
 60                 image.GrayFill = 20;//透明度,灰色填充
 61                                     //image.Rotation//旋转
 62                                     //image.RotationDegrees//旋转角度
 63                                     //水印的位置
 64                 if (left < 0)
 65                 {
 66                     left = width / 2 - image.Width + left;
 67                 }
 68 
 69                 //image.SetAbsolutePosition(left, (height - image.Height) - top);
 70                 image.SetAbsolutePosition(left, (height / 2 - image.Height) - top);
 71 
 72 
 73                 //每一页加水印,也可以设置某一页加水印
 74                 for (int i = 1; i <= numberOfPages; i++)
 75                 {
 76                     //waterMarkContent = pdfStamper.GetUnderContent(i);//内容下层加水印
 77                     waterMarkContent = pdfStamper.GetOverContent(i);//内容上层加水印
 78 
 79                     waterMarkContent.AddImage(image);
 80                 }
 81                 //strMsg = "success";
 82                 return true;
 83             }
 84             catch (Exception ex)
 85             {
 86                 throw ex;
 87 
 88             }
 89             finally
 90             {
 91 
 92                 if (pdfStamper != null)
 93                     pdfStamper.Close();
 94 
 95                 if (pdfReader != null)
 96                     pdfReader.Close();
 97             }
 98         }
 99         /// <summary>
100         /// 添加普通偏转角度文字水印
101         /// </summary>
102         /// <param name="inputfilepath"></param>
103         /// <param name="outputfilepath"></param>
104         /// <param name="waterMarkName"></param>
105         /// <param name="permission"></param>
106         public static void AddWordWatermark2PDF(string inputfilepath, string outputfilepath, string waterMarkName)
107         {
108             PdfReader pdfReader = null;
109             PdfStamper pdfStamper = null;
110             try
111             {
112                 pdfReader = new PdfReader(inputfilepath);
113                 pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
114                 int total = pdfReader.NumberOfPages + 1;
115                 iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
116                 float width = psize.Width;
117                 float height = psize.Height;
118                 PdfContentByte content;
119                 BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
120                 PdfGState gs = new PdfGState();
121                 for (int i = 1; i < total; i++)
122                 {
123                     content = pdfStamper.GetOverContent(i);//在内容上方加水印
124                     //content = pdfStamper.GetUnderContent(i);//在内容下方加水印
125                     //透明度
126                     gs.FillOpacity = 0.5f;
127                     content.SetGState(gs);
128                     content.SetGrayFill(0.5f);
129                     //开始写入文本
130                     content.BeginText();
131                     //content.SetColorFill(BaseColor.LIGHT_GRAY);
132                     //content.SetFontAndSize(font, 100);
133                     //content.SetTextMatrix(0, 0);
134                     //content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55);
135                     content.SetColorFill(BaseColor.GRAY);
136                     content.SetFontAndSize(font, 16);
137                     content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55);
138                     content.EndText();
139                 }
140             }
141             catch (Exception ex)
142             {
143                 throw ex;
144             }
145             finally
146             {
147 
148                 if (pdfStamper != null)
149                     pdfStamper.Close();
150 
151                 if (pdfReader != null)
152                     pdfReader.Close();
153             }
154         }
155 
156 
157 
158         /// <summary>
159         /// 添加倾斜水印
160         /// </summary>
161         /// <param name="inputfilepath"></param>
162         /// <param name="outputfilepath"></param>
163         /// <param name="waterMarkName"></param>
164         /// <param name="userPassWord"></param>
165         /// <param name="ownerPassWord"></param>
166         /// <param name="permission"></param>
167         public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName, string userPassWord, string ownerPassWord, int permission)
168         {
169             PdfReader pdfReader = null;
170             PdfStamper pdfStamper = null;
171             try
172             {
173                 pdfReader = new PdfReader(inputfilepath);
174                 pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
175                 // 设置密码  
176                 //pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission);
177 
178                 int total = pdfReader.NumberOfPages + 1;
179                 PdfContentByte content;
180                 BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
181                 PdfGState gs = new PdfGState();
182                 gs.FillOpacity = 0.2f;//透明度
183 
184                 int j = waterMarkName.Length;
185                 char c;
186                 int rise = 0;
187                 for (int i = 1; i < total; i++)
188                 {
189                     rise = 500;
190                     content = pdfStamper.GetOverContent(i);//在内容上方加水印
191                                                            //content = pdfStamper.GetUnderContent(i);//在内容下方加水印
192 
193                     content.BeginText();
194                     content.SetColorFill(BaseColor.DARK_GRAY);
195                     content.SetFontAndSize(font, 50);
196                     // 设置水印文字字体倾斜 开始
197                     if (j >= 15)
198                     {
199                         content.SetTextMatrix(200, 120);
200                         for (int k = 0; k < j; k++)
201                         {
202                             content.SetTextRise(rise);
203                             c = waterMarkName[k];
204                             content.ShowText(c + "");
205                             rise -= 20;
206                         }
207                     }
208                     else
209                     {
210                         content.SetTextMatrix(180, 100);
211                         for (int k = 0; k < j; k++)
212                         {
213                             content.SetTextRise(rise);
214                             c = waterMarkName[k];
215                             content.ShowText(c + "");
216                             rise -= 18;
217                         }
218                     }
219                     // 字体设置结束
220                     content.EndText();
221                     // 画一个圆
222                     //content.Ellipse(250, 450, 350, 550);
223                     //content.SetLineWidth(1f);
224                     //content.Stroke();
225                 }
226 
227             }
228             catch (Exception ex)
229             {
230                 throw ex;
231             }
232             finally
233             {
234 
235                 if (pdfStamper != null)
236                     pdfStamper.Close();
237 
238                 if (pdfReader != null)
239                     pdfReader.Close();
240             }
241         }
242 
243 
244 
245 
246     }
247 
248 
249    
250 }

 

posted on 2018-04-10 15:55  浩凡儿  阅读(5055)  评论(0编辑  收藏  举报