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. Java值传递以及引用的传递、数组的传递!!

    转(http://blog.csdn.net/niuniu20008/article/details/2953785) 许多编程语言都有2种方法将参数传递给方法------按值传递和按引用传递. 与其 ...

  2. c 语言 运算符 优先级

    C 语言 运算法优先级 从高 到 低 优先级 运算符 功能 适用范围 结合性 15 () [] . -> 括号 下标 存取成员 存取成员 表达式 数组 结构联合 结构联合 → (左→右) 14 ...

  3. 谈谈Java面向对象的三大特性

    Java面向对象的三大特性就是指封装.继承.多态了. 一.封装: 概念:封装是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. (举例:笔记本电脑就是一个封装体,Java语言中最小的封装体就是函数 ...

  4. iptables--简单的防火墙

    iptables--简单的防火墙 如果你执行iptables --list你将看到防火墙上的可用规则.下例说明当前系统没有定义防火墙,你可以看到,它显示了默认的filter表,以及表内默认的input ...

  5. Ms sql行转列。汇总

    SQL行转列汇总 PIVOT 用于将列值旋转为列名(即行转列),在 SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT 的一般语法是:PIVOT(聚合函数(列) FOR 列 ...

  6. ARCGIS如何进行可视域分析

    可视域分析在不同的领域有着广泛的应用,如火灾监控点的设定,观察哨所的设定等等.军事领域是可视域分析技术应用最广的领域.例如为了设计巡航导弹的航线,就必须对发射点到目标的地形进行分析,包括地形特征优劣分 ...

  7. Appium UI自动化的那些梗

    @作者 彭海波 转载请注明出处 前言 由于需求的快速迭代和敏捷测试的要求,在测试过程中引入自动化成为必不可少的手段.作为一个互联网测试团队,我们自然也引入了自动化测试这个环节.在众多的测试框架中,我们 ...

  8. 使用axi_datamover完成ZYNQ片内PS与PL间的数据传输

    分享下PS与PL之间数据传输比较另类的实现方式,实现目标是: 1.传输时数据不能滞留在一端,无论是1个字节还是1K字节都能立即发送: 2.PL端接口为FIFO接口: PS到PL的数据传输流程: PS到 ...

  9. zend framework2 下载及安装

    1.安装XAMPP 2.安装zend studio 3.在GITHUB上下载一个zendframework模板,插入到IDE中 4.将下载的zend framework2文件夹解压放在vendor文件 ...

  10. KBMMW 4.92.00 发布

    We are happy to announce the release of kbmMW Professional and Enterprise Edition. Yet again kbmMW c ...