1.使用NuGet下载iTextSharp Version=5.5.10.0
1 public class ConvertPDF 2 { 3 private static string fontPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"C:\WINDOWS\Fonts\SIMFANG.TTF"); 4 5 /// <summary> 6 /// 添加普通偏转角度文字水印保存到文件 7 /// </summary> 8 /// <param name="inputfilepath">文件地址</param> 9 /// <param name="outputfilepath"></param> 10 /// <param name="waterMarkName"></param> 11 public static void SetWatermarkToPath(string inputfilepath, string waterMarkName, string outputfilepath, string fontS = "25", string bigWaterMarkName = "") 12 { 13 //if (string.IsNullOrEmpty(waterMarkName)) 14 //{ 15 // try { File.Delete(inputfilepath); } catch (Exception e) { } 16 // throw new Exception("水印文字不能为空"); 17 //} 18 19 PdfReader pdfReader = null; 20 PdfStamper pdfStamper = null; 21 try 22 { 23 pdfReader = new PdfReader(inputfilepath); 24 pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create)); 25 int total = pdfReader.NumberOfPages + 1; 26 iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1); 27 float width = psize.Width; 28 float height = psize.Height; 29 PdfContentByte content; 30 BaseFont font = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 31 PdfGState gs = new PdfGState() 32 { 33 FillOpacity = 0.8f//透明度 34 }; 35 //for (int i = 1; i < total; i++) 36 //{ 37 // content = pdfStamper.GetOverContent(i);//在内容上方加水印 38 // content = pdfStamper.GetUnderContent(i);//在内容下方加水印 39 // //透明度 40 // gs.FillOpacity = 0.3f; 41 // content.SetGState(gs); 42 // content.SetGrayFill(0.3f); 43 // //开始写入文本 44 // content.BeginText(); 45 // content.SetColorFill(BaseColor.LightGray); 46 // content.SetFontAndSize(font, 100); 47 // content.SetTextMatrix(0, 0); 48 // content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55); 49 // content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 +50, height / 2+50, 55); 50 // //content.SetColorFill(BaseColor.BLACK); 51 // //content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0); 52 // content.EndText(); 53 //} 54 int span = 1;//设置垂直位移 55 int spanHeight = new Random().Next(120, 200);//垂直间距 56 int fontSize = !string.IsNullOrEmpty(fontS) ? Convert.ToInt32(fontS) : 25;//设置字体大小 57 int waterMarkNameLenth = waterMarkName.Length; 58 char c; 59 int rise = 0; 60 string spanString = "";//字符间距 61 int spanWidth = 4;//水平位移 62 int spanWaterMarkNameWidth = new Random().Next(100, 150);//水印文字位移宽度 63 for (int i = 1; i < total; i++) 64 { 65 rise = waterMarkNameLenth * span + spanHeight; 66 content = pdfStamper.GetOverContent(i);//在内容上方加水印 67 //content = pdfStamper.GetUnderContent(i);//在内容下方加水印 68 content.SetGState(gs); 69 content.BeginText(); 70 content.SetColorFill(BaseColor.LIGHT_GRAY); 71 content.SetFontAndSize(font, fontSize); 72 73 74 int heightNumbert = (int)Math.Ceiling((decimal)height / (decimal)rise);//垂直重复的次数,进一发 75 int panleWith = fontSize * waterMarkNameLenth / 2 + spanWaterMarkNameWidth; 76 int widthNumber = (int)Math.Ceiling((decimal)width / (decimal)panleWith);//水平重复次数 77 78 for (int w = 0; w < widthNumber; w++) 79 { 80 for (int h = 1, f = heightNumbert - 1; h <= heightNumbert; h++, f--) 81 { 82 int yleng = rise * h; 83 int wleng = (w * panleWith) + (spanWidth * f); 84 content.SetTextMatrix(wleng, yleng);//x,y设置水印开始的绝对左边,以左下角为x,y轴的起点 85 86 content.ShowTextAligned(Element.ALIGN_JUSTIFIED_ALL, waterMarkName, wleng, yleng, 5); 87 88 //content.ShowText(spanString); 89 //for (int k = 0; k < waterMarkNameLenth; k++) 90 //{ 91 // content.SetTextRise(yleng);//指定的y轴值处添加 92 // c = waterMarkName[k]; 93 // content.ShowText(c + spanString); 94 // yleng += span; 95 //} 96 } 97 } 98 99 content.EndText(); 100 } 101 } 102 catch (Exception ex) 103 { 104 throw ex; 105 } 106 finally 107 { 108 109 if (pdfStamper != null) 110 pdfStamper.Close(); 111 112 if (pdfReader != null) 113 pdfReader.Close(); 114 } 115 } 116 117 118 /// <summary> 119 /// 添加普通偏转角度文字水印保存到文件 120 /// </summary> 121 /// <param name="isp">文件流</param> 122 /// <param name="outputfilepath"></param> 123 /// <param name="waterMarkName"></param> 124 /// <param name="permission"></param> 125 public static void SetWatermarkToPath(Stream isp, string waterMarkName, string outputfilepath, string fontS = "25", string bigWaterMarkName = "") 126 { 127 //if (string.IsNullOrEmpty(waterMarkName)) 128 //{ 129 // return; 130 //} 131 132 PdfReader pdfReader = null; 133 PdfStamper pdfStamper = null; 134 try 135 { 136 pdfReader = new PdfReader(isp); 137 pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create)); 138 int total = pdfReader.NumberOfPages + 1; 139 iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1); 140 float width = psize.Width; 141 float height = psize.Height; 142 PdfContentByte content; 143 BaseFont font = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 144 PdfGState gs = new PdfGState() 145 { 146 FillOpacity = 0.4f//透明度 147 }; 148 //for (int i = 1; i < total; i++) 149 //{ 150 // content = pdfStamper.GetOverContent(i);//在内容上方加水印 151 // content = pdfStamper.GetUnderContent(i);//在内容下方加水印 152 // //透明度 153 // gs.FillOpacity = 0.3f; 154 // content.SetGState(gs); 155 // content.SetGrayFill(0.3f); 156 // //开始写入文本 157 // content.BeginText(); 158 // content.SetColorFill(BaseColor.LightGray); 159 // content.SetFontAndSize(font, 100); 160 // content.SetTextMatrix(0, 0); 161 // content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55); 162 // content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 +50, height / 2+50, 55); 163 // //content.SetColorFill(BaseColor.BLACK); 164 // //content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0); 165 // content.EndText(); 166 //} 167 int span = 1;//设置垂直位移 168 int spanHeight = new Random().Next(60, 100);//垂直间距 169 int fontSize = !string.IsNullOrEmpty(fontS) ? Convert.ToInt32(fontS) : 25;//设置字体大小 170 int waterMarkNameLenth = waterMarkName.Length; 171 char c; 172 int rise = 0; 173 string spanString = "";//字符间距 174 int spanWidth = 4;//水平位移 175 int spanWaterMarkNameWidth = 90;//水印文字位移宽度 176 for (int i = 1; i < total; i++) 177 { 178 rise = waterMarkNameLenth * span + spanHeight; 179 content = pdfStamper.GetOverContent(i);//在内容上方加水印 180 //content = pdfStamper.GetUnderContent(i);//在内容下方加水印 181 content.SetGState(gs); 182 content.BeginText(); 183 content.SetColorFill(BaseColor.LIGHT_GRAY); 184 content.SetFontAndSize(font, fontSize); 185 186 187 int heightNumbert = (int)Math.Ceiling((decimal)height / (decimal)rise);//垂直重复的次数,进一发 188 int panleWith = fontSize * waterMarkNameLenth / 2 + spanWaterMarkNameWidth; 189 int widthNumber = (int)Math.Ceiling((decimal)width / (decimal)panleWith);//水平重复次数 190 191 for (int w = 0; w < widthNumber; w++) 192 { 193 for (int h = 1, f = heightNumbert - 1; h <= heightNumbert; h++, f--) 194 { 195 int yleng = rise * h; 196 int wleng = (w * panleWith) + (spanWidth * f); 197 content.SetTextMatrix(wleng, yleng);//x,y设置水印开始的绝对左边,以左下角为x,y轴的起点 198 199 content.ShowTextAligned(Element.ALIGN_JUSTIFIED_ALL, waterMarkName, wleng, yleng, 5); 200 201 //content.ShowText(spanString); 202 //for (int k = 0; k < waterMarkNameLenth; k++) 203 //{ 204 // content.SetTextRise(yleng);//指定的y轴值处添加 205 // c = waterMarkName[k]; 206 // content.ShowText(c + spanString); 207 // yleng += span; 208 //} 209 } 210 } 211 212 content.EndText(); 213 } 214 } 215 catch (Exception ex) 216 { 217 throw ex; 218 } 219 finally 220 { 221 222 if (pdfStamper != null) 223 pdfStamper.Close(); 224 225 if (pdfReader != null) 226 pdfReader.Close(); 227 } 228 } 229 230 /// <summary> 231 /// 添加普通偏转角度文字水印(保存到内存) 232 /// </summary> 233 /// <param name="inStream"></param> 234 /// <param name="waterMarkName"></param> 235 /// <param name="fontS"></param> 236 /// <param name="bigWaterMarkName"></param> 237 /// <returns></returns> 238 public static MemoryStream SetWatermark(Stream inStream, string waterMarkName, string fontS = "25", string bigWaterMarkName = "") 239 { 240 MemoryStream outStream = new MemoryStream(); 241 242 if (string.IsNullOrEmpty(waterMarkName)) 243 { 244 inStream.CopyTo(outStream); 245 return outStream; 246 } 247 248 249 PdfReader pdfReader = null; 250 PdfStamper pdfStamper = null; 251 try 252 { 253 pdfReader = new PdfReader(inStream); 254 pdfStamper = new PdfStamper(pdfReader, outStream); 255 int pages = pdfReader.NumberOfPages + 1; 256 iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1); 257 float width = psize.Width; 258 float height = psize.Height; 259 PdfContentByte content; 260 BaseFont font = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 261 PdfGState gs = new PdfGState() 262 { 263 FillOpacity = 0.4f//透明度 264 }; 265 //for (int i = 1; i < total; i++) 266 //{ 267 // content = pdfStamper.GetOverContent(i);//在内容上方加水印 268 // content = pdfStamper.GetUnderContent(i);//在内容下方加水印 269 // //透明度 270 // gs.FillOpacity = 0.3f; 271 // content.SetGState(gs); 272 // content.SetGrayFill(0.3f); 273 // //开始写入文本 274 // content.BeginText(); 275 // content.SetColorFill(BaseColor.LightGray); 276 // content.SetFontAndSize(font, 100); 277 // content.SetTextMatrix(0, 0); 278 // content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55); 279 // content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 +50, height / 2+50, 55); 280 // //content.SetColorFill(BaseColor.BLACK); 281 // //content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0); 282 // content.EndText(); 283 //} 284 int span = 1;//设置垂直位移 285 int spanHeight = new Random().Next(60, 100);//垂直间距 286 int fontSize = !string.IsNullOrEmpty(fontS) ? Convert.ToInt32(fontS) : 25;//设置字体大小 287 int waterMarkNameLenth = waterMarkName.Length; 288 char c; 289 int rise = 0; 290 string spanString = "";//字符间距 291 int spanWidth = 4;//水平位移 292 int spanWaterMarkNameWidth = 90;//水印文字位移宽度 293 for (int i = 1; i < pages; i++) 294 { 295 rise = waterMarkNameLenth * span + spanHeight; 296 content = pdfStamper.GetOverContent(i);//在内容上方加水印 297 //content = pdfStamper.GetUnderContent(i);//在内容下方加水印 298 content.SetGState(gs); 299 content.BeginText(); 300 content.SetColorFill(BaseColor.LIGHT_GRAY); 301 content.SetFontAndSize(font, fontSize); 302 303 304 int heightNumbert = (int)Math.Ceiling((decimal)height / (decimal)rise);//垂直重复的次数,进一发 305 int panleWith = fontSize * waterMarkNameLenth / 2 + spanWaterMarkNameWidth; 306 int widthNumber = (int)Math.Ceiling((decimal)width / (decimal)panleWith);//水平重复次数 307 308 for (int w = 0; w < widthNumber; w++) 309 { 310 for (int h = 1, f = heightNumbert - 1; h <= heightNumbert; h++, f--) 311 { 312 int yleng = rise * h; 313 int wleng = (w * panleWith) + (spanWidth * f); 314 content.SetTextMatrix(wleng, yleng);//x,y设置水印开始的绝对左边,以左下角为x,y轴的起点 315 316 content.ShowTextAligned(Element.ALIGN_JUSTIFIED_ALL, waterMarkName, wleng, yleng, 5); 317 318 //content.ShowText(spanString); 319 //for (int k = 0; k < waterMarkNameLenth; k++) 320 //{ 321 // content.SetTextRise(yleng);//指定的y轴值处添加 322 // c = waterMarkName[k]; 323 // content.ShowText(c + spanString); 324 // yleng += span; 325 //} 326 } 327 } 328 329 content.EndText(); 330 } 331 } 332 catch (Exception ex) 333 { 334 335 336 337 throw ex; 338 } 339 finally 340 { 341 if (pdfStamper != null) 342 pdfStamper.Close(); 343 344 if (pdfReader != null) 345 pdfReader.Close(); 346 } 347 348 return outStream; 349 } 350 351 352 //public static string GetMapPath(string strPath) 353 //{ 354 // if (HttpContext.Current != null) 355 // { 356 // return HttpContext.Current.Server.MapPath(strPath); 357 // } 358 // else //非web程序引用 359 // { 360 // return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); 361 // } 362 //} 363 }