码迷,mamicode.com
首页 > 其他好文 > 详细

PDF打水印加密

时间:2016-11-17 13:39:30      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   ase   try   close   mod   html   water   生成   res   

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Diagnostics;
  5 using System.IO;
  6 using System.Linq;
  7 using System.Web;
  8 using iTextSharp.text;
  9 using iTextSharp.text.pdf;
 10 using iTextSharp.text.html;
 11 using iTextSharp.text.xml;
 12 using System.Configuration;
 13 
 14 namespace WorkFlow
 15 {
 16     public class ReportHelper
 17     {
 18         public static string  ConvertToPDF(string content)
 19         {
 20             string s = string.Empty;
 21             string sPwd = string.Empty;
 22             StringReader sr = new StringReader(content);
 23             //定义一个Document,并设置页面大小为A4,竖向
 24             Document document = new Document(PageSize.A4);
 25             string sMapPath = HttpContext.Current.Server.MapPath(GetWebMailKeyValue()) + @"\";
 26             string sFileName = "Report" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
 27 
 28             try
 29             {
 30                 DirectoryInfo info = new DirectoryInfo(sMapPath);
 31                 if (!info.Exists)
 32                 {
 33                     info.Create();
 34                 }
 35                 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sMapPath + sFileName, FileMode.Create));
 36                 #region 载入亚洲字体资源,无此操作的话,不能显示包括中文、日文、韩文等内容
 37                 BaseFont.AddToResourceSearch("iTextAsian.dll");
 38                 BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
 39                 BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
 40                 #endregion
 41 
 42 
 43                 //生成水印
 44                 writer.PageEvent = new PdfEventHanler("@JTSJMZT", bf);
 45 
 46                 //处理密码
 47                 if (string.IsNullOrEmpty(sPwd))
 48                 {
 49                     Random r = new Random();
 50                     sPwd = r.Next(0, 99999999).ToString();
 51                 }
 52                 //将密码存入数据库
 53                 //sPwd
 54 
 55                 //加密
 56                 writer.SetEncryption(PdfWriter.STRENGTH128BITS, sPwd, null, PdfWriter.AllowPrinting);
 57 
 58 
 59                 //打开document
 60                 document.Open();
 61 
 62                 //写入一个段落, Paragraph
 63                 //List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null).ToArray().ToList<IElement>();
 64 
 65                 //for (int k = 0; k < htmlarraylist.Count; k++)
 66                 //    document.Add(htmlarraylist[k]);
 67                
 68                 //Paragraph mypara = new Paragraph();
 69                 //document.Add(mypara);
 70 
 71                 document.Add(new Paragraph(content, new Font(bf, 12)));
 72 
 73                 //关闭document
 74                 document.Close();
 75 
 76                 s = sFileName;
 77                 //打开PDF,看效果
 78                 //Process.Start(s);
 79 
 80                 //Blob Storage
 81                 //var location = AppDomain.CurrentDomain.BaseDirectory;
 82                 //uploadPath = location + "/";
 83                 //doc.Save(uploadPath + sFileName);
 84                 //BlobClient.AddBlob(DANCode1Config.EmailFileContainer, "MailExportAttachment1/" + sFileName, uploadPath, sFileName);
 85 
 86                 //uploadPath = GetWebMailURL() + "MailExportAttachment1/" + sFileName;
 87 
 88                 //s = "{\"message\":true,\"value\":\"" + s + "\"}";
 89             }
 90             catch (Exception ex)
 91             {
 92                 s= ex.Message;
 93             }
 94             return s;
 95         }
 96 
 97         public static string GetWebMailURL()
 98         {
 99             string s = ConfigurationManager.AppSettings["MailBlobStorage"].ToString();
100             return s;
101         }
102 
103         public static string GetWebMailKeyValue()
104         {
105             string s = ConfigurationManager.AppSettings["PDFExport"].ToString();
106             return s;
107         }
108     }
109 
110     internal class PdfEventHanler : PdfPageEventHelper
111     {
112         string m_WatermaskText;
113         BaseFont m_WatermaskTextFont;
114 
115         /// <summary>
116         ///
117         /// </summary>
118         /// <param name="watermaskText">水印文字的内容</param>
119         /// <param name="watermaskTextFont">水印文字的字体</param>
120         public PdfEventHanler(string watermaskText,
121      BaseFont watermaskTextFont)
122         {
123             m_WatermaskText = watermaskText;
124             m_WatermaskTextFont = watermaskTextFont;
125         }
126 
127         /// <summary>
128         /// 在Page End事件中,添加页面水印,此事件在每页均会发生
129         /// </summary>
130         /// <param name="writer"></param>
131         /// <param name="document"></param>
132         public override void OnEndPage(PdfWriter writer,
133     Document document)
134         {
135             PdfGState gstate = new PdfGState();
136             gstate.FillOpacity = 0.2f; //设置灰度和透明度
137             gstate.StrokeOpacity = 0.2f;
138 
139             //水印内容,必须放置在底层
140             PdfContentByte wm = writer.DirectContentUnder;
141             wm.SaveState();
142             wm.SetGState(gstate);
143 
144             #region 给页面添加文字水印
145             wm.SetFontAndSize(m_WatermaskTextFont, 50);
146             wm.BeginText();
147             wm.ShowTextAligned(Element.ALIGN_CENTER, m_WatermaskText,
148     document.PageSize.Width / 2, document.PageSize.Height / 2, -35f);
149             wm.EndText();
150             #endregion
151 
152             wm.RestoreState();
153 
154             base.OnEndPage(writer, document);
155         }
156     }
157 }
View Code

 

PDF打水印加密

标签:style   ase   try   close   mod   html   water   生成   res   

原文地址:http://www.cnblogs.com/riusmary/p/6073057.html

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