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);
}
}
}