using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Configuration; namespace WorkFlow
{
public class ReportHelper
{
public static string ConvertToPDF(string content)
{
string s = string.Empty;
string sPwd = string.Empty;
StringReader sr = new StringReader(content);
//定义一个Document,并设置页面大小为A4,竖向
Document document = new Document(PageSize.A4);
string sMapPath = HttpContext.Current.Server.MapPath(GetWebMailKeyValue()) + @"\";
string sFileName = "Report" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; try
{
DirectoryInfo info = new DirectoryInfo(sMapPath);
if (!info.Exists)
{
info.Create();
}
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sMapPath + sFileName, FileMode.Create));
#region 载入亚洲字体资源,无此操作的话,不能显示包括中文、日文、韩文等内容
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
#endregion //生成水印
writer.PageEvent = new PdfEventHanler("@JTSJMZT", bf); //处理密码
if (string.IsNullOrEmpty(sPwd))
{
Random r = new Random();
sPwd = r.Next(, ).ToString();
}
//将密码存入数据库
//sPwd //加密
writer.SetEncryption(PdfWriter.STRENGTH128BITS, sPwd, null, PdfWriter.AllowPrinting); //打开document
document.Open(); //写入一个段落, Paragraph
//List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null).ToArray().ToList<IElement>(); //for (int k = 0; k < htmlarraylist.Count; k++)
// document.Add(htmlarraylist[k]); //Paragraph mypara = new Paragraph();
//document.Add(mypara); document.Add(new Paragraph(content, new Font(bf, ))); //关闭document
document.Close(); s = sFileName;
//打开PDF,看效果
//Process.Start(s); //Blob Storage
//var location = AppDomain.CurrentDomain.BaseDirectory;
//uploadPath = location + "/";
//doc.Save(uploadPath + sFileName);
//BlobClient.AddBlob(DANCode1Config.EmailFileContainer, "MailExportAttachment1/" + sFileName, uploadPath, sFileName); //uploadPath = GetWebMailURL() + "MailExportAttachment1/" + sFileName; //s = "{\"message\":true,\"value\":\"" + s + "\"}";
}
catch (Exception ex)
{
s= ex.Message;
}
return s;
} public static string GetWebMailURL()
{
string s = ConfigurationManager.AppSettings["MailBlobStorage"].ToString();
return s;
} public static string GetWebMailKeyValue()
{
string s = ConfigurationManager.AppSettings["PDFExport"].ToString();
return s;
}
} internal class PdfEventHanler : PdfPageEventHelper
{
string m_WatermaskText;
BaseFont m_WatermaskTextFont; /// <summary>
///
/// </summary>
/// <param name="watermaskText">水印文字的内容</param>
/// <param name="watermaskTextFont">水印文字的字体</param>
public PdfEventHanler(string watermaskText,
BaseFont watermaskTextFont)
{
m_WatermaskText = watermaskText;
m_WatermaskTextFont = watermaskTextFont;
} /// <summary>
/// 在Page End事件中,添加页面水印,此事件在每页均会发生
/// </summary>
/// <param name="writer"></param>
/// <param name="document"></param>
public override void OnEndPage(PdfWriter writer,
Document document)
{
PdfGState gstate = new PdfGState();
gstate.FillOpacity = 0.2f; //设置灰度和透明度
gstate.StrokeOpacity = 0.2f; //水印内容,必须放置在底层
PdfContentByte wm = writer.DirectContentUnder;
wm.SaveState();
wm.SetGState(gstate); #region 给页面添加文字水印
wm.SetFontAndSize(m_WatermaskTextFont, );
wm.BeginText();
wm.ShowTextAligned(Element.ALIGN_CENTER, m_WatermaskText,
document.PageSize.Width / , document.PageSize.Height / , -35f);
wm.EndText();
#endregion wm.RestoreState(); base.OnEndPage(writer, document);
}
}
}

PDF打水印加密的更多相关文章

  1. 快速解决PDF文档加密不能打印问题_百度经验

    快速解决PDF文档加密不能打印问题_百度经验     快速解决PDF文档加密不能打印问题         |        浏览:182        |        更新:2014-01-06 1 ...

  2. PDF的水印怎么去掉

    很多朋友私下都有问过PDF去除水印的方法,现在在网上下载一些PDF电子书,几乎页面内都会有水印的,而且有的水印还带有超链接,稍微不注意就会点开进入别的页面内,不仅影响了阅读效果还带给读者负面影响,那如 ...

  3. 怎么编辑PDF,如何给PDF加水印

    在使用PDF文件的时候,往往会用到PDF编辑器来修改,那么,在使用PDF编辑器修改文件的时候,想要在文件中添加水印,这该怎么操作呢,不会的小伙伴可以看看下面的文章了哦,说不定就会了. 1.打开运行PD ...

  4. java 实现 PDF 加水印功能

    使用java代码实现给PDF加水印的功能 首先导入所需要的依赖 <dependency> <groupId>com.itextpdf</groupId> <a ...

  5. pdf增加水印

    /// <summary> /// 为PDF添加水印或背景图片 /// </summary> /// <param name="strSourceFilePat ...

  6. 给现有的word和pdf加水印

    iTextSharp简单生成pdf和操作pdf添加水印 给word加水印,利用的是aspose.words

  7. pdf文件下载水印添加的中文与空格问题解决

    public static boolean waterMark(String inputFile, String outputFile, String waterMarkName)throws IOE ...

  8. 如何给PDF加水印

  9. springboot为导出的pdf和excel加水印

    相信很多小伙伴们在做导出pdf或者excel文件时会被要求在文件上加上水印,本篇博客就来讲讲如何为pdf和excel加水印. 导出pdf加水印 其实在导出pdf时加上水印并不难,因为itext提供了添 ...

随机推荐

  1. 通过Spark SQL关联查询两个HDFS上的文件操作

    order_created.txt   订单编号  订单创建时间 -- :: -- :: -- :: -- :: -- :: order_picked.txt   订单编号  订单提取时间 -- :: ...

  2. C++设计模式-Strategy策略模式

    Strategy策略模式作用:定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. UML图: Strategy模式将逻辑(算法)封装到一个类(Cont ...

  3. Python-2 print

    #1 print函数(python版本3.3.5): >>> help(print)Help on built-in function print in module builtin ...

  4. PullToRefreshListView加载更多定位的解决方法

    之前项目里,早起用到了PullToRefreshListView,后来一部分用到了RefreshListView,最近在做优化的时候,其中一个问题就是PullToRefreshlistView上拉加载 ...

  5. Spark Streaming源码解读之Driver容错安全性

    本期内容 : ReceivedBlockTracker容错安全性 DStreamGraph和JobGenerator容错安全性 Driver的安全性主要从Spark Streaming自己运行机制的角 ...

  6. IAR调节字体大小

    在主面板上点击tools->Options,然后点开Editor,选择下面的Colors and Fonts选项,最后选右上方的Font,选择要设置的字体就OK了.

  7. SpirentTestcenter测试仪的自动化

    SpirentTestcenter,美国思博伦公司的网络测试仪表,覆盖以太网L2~L7层,使用过的仪表中功能最强大的. 1.SpirentTestcenter的自动化测试场景 测试PC上的AT框架-- ...

  8. JAVA学习笔记(1-32)

    1:数据的输入中,也需要提前声明变量.java区分大小写,注意保留字的问题,注意分号. 2:用import调用一个类.用new新建一个对象,相当于c中的malloc. 3:用final定义一个宏,相当 ...

  9. 微服务实战系列--Nginx官网发布(转)

    这是Nginx官网写的一个系列,共七篇文章,如下 Introduction to Microservices (this article) Building Microservices: Using ...

  10. Python-any函数和all函数

    any函数: any(x),只要x中有一个不为空,0,false就返回True,否则返回False all(x)函数 必须x中的所有元素均不为空,0,false才会返回True,否则返回False & ...