C#根据html生成PDF

使用iTextSharp 控件

iTextSharp包括几个dll。

主要iTextSharp版本包含:
——itextsharp。dll:核心库
——itextsharp.xtra。dll:额外的功能(PDF 2 !)
——itextsharp.pdfa。dll:PDF /相关的功能
这个项目是托管在http://sourceforge.net/projects/itextsharp/
你可以在这里找到的最新版本:
http://sourceforge.net/projects/itextsharp/files/itextsharp/

在某些情况下,您将需要额外的dll。
这些dll是可用的:
http://sourceforge.net/projects/itextsharp/files/extras/

对于XML和HTML功能,您需要这个dll:
——itextsharp.xmlworker.dll
这是可以在http://sourceforge.net/projects/itextsharp/files/xmlworker/

最后,我们也有一个Java工具,可以帮助您调试pdf文档:
——itext-rups-x.y.z.jar
这个项目是托管在http://sourceforge.net/projects/itextrups/

下列代码需要导入两个.dll

itextsharp.dll:核心库

itextsharp.xmlworker.dll

using System;
using System.Collections.Generic; using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.tool.xml;
using System.Text;
using System.Net; namespace PDFTools
{
public partial class CreatePdf : System.Web.UI.Page
{
string INPATH = System.Web.HttpContext.Current.Server.MapPath("~/bin/temp.html"); protected void Page_Load(object sender, EventArgs e)
{
string path = System.Web.HttpContext.Current.Server.MapPath("~/UploadPdf");
if (!Directory.Exists(path))
Directory.CreateDirectory(path); WebClient wc = new WebClient();
//从网址下载Html字串
wc.Encoding = System.Text.Encoding.UTF8;
string htmlText = getWebContent();
byte[] pdfFile = this.ConvertHtmlTextToPDF(htmlText); string fileId = "/file_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
System.IO.File.WriteAllBytes(path + fileId, pdfFile);
} /// <summary>
/// 判断是否有乱码
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public bool isMessyCode(string txt)
{
var bytes = Encoding.UTF8.GetBytes(txt); //239 191 189
for (var i = ; i < bytes.Length; i++)
{
if (i < bytes.Length - )
if (bytes[i] == && bytes[i + ] == && bytes[i + ] == )
{
return true;
}
}
return false;
}
/// <summary>
/// 获取网站内容,包含了 HTML+CSS+JS
/// </summary>
/// <returns>String返回网页信息</returns>
public string getWebContent()
{
try
{
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;
//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData(INPATH);
//从指定网站下载数据
string pageHtml = Encoding.UTF8.GetString(pageData);
//如果获取网站页面采用的是GB2312,则使用这句
bool isBool = isMessyCode(pageHtml);//判断使用哪种编码 读取网页信息
if (!isBool)
{
string pageHtml1 = Encoding.UTF8.GetString(pageData);
pageHtml = pageHtml1;
}
else
{
string pageHtml2 = Encoding.Default.GetString(pageData);
pageHtml = pageHtml2;
}
return pageHtml;
} catch (WebException webEx)
{
Console.WriteLine(webEx.Message.ToString());
return webEx.Message;
}
} /// <summary>
/// 将Html文字 输出到PDF档里
/// </summary>
/// <param name="htmlText"></param>
/// <returns></returns>
public byte[] ConvertHtmlTextToPDF(string htmlText)
{
if (string.IsNullOrEmpty(htmlText))
{
return null;
}
//避免当htmlText无任何html tag标签的纯文字时,转PDF时会挂掉,所以一律加上<p>标签
htmlText = "<p>" + htmlText + "</p>"; MemoryStream outputStream = new MemoryStream();//要把PDF写到哪个串流
byte[] data = Encoding.UTF8.GetBytes(htmlText);//字串转成byte[]
MemoryStream msInput = new MemoryStream(data);
Document doc = new Document();//要写PDF的文件,建构子没填的话预设直式A4
PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
//指定文件预设开档时的缩放为100% PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, , doc.PageSize.Height, 1f);
//开启Document文件
doc.Open(); //使用XMLWorkerHelper把Html parse到PDF档里
// XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8); //将pdfDest设定的资料写到PDF档
PdfAction action = PdfAction.GotoLocalPage(, pdfDest, writer);
writer.SetOpenAction(action);
doc.Close();
msInput.Close();
outputStream.Close();
//回传PDF档案
return outputStream.ToArray(); } //设置字体类
public class UnicodeFontFactory : FontFactoryImp
{
private static readonly string arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),
"arialuni.ttf");//arial unicode MS是完整的unicode字型。
private static readonly string 标楷体Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),
"KAIU.TTF");//标楷体 public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
{
BaseFont bfChiness = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//可用Arial或标楷体,自己选一个
BaseFont baseFont = BaseFont.CreateFont(标楷体Path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
return new Font(bfChiness, size, style, color);
}
}
}
}
上一篇:Objective-C的可变是如何实现的?


下一篇:MFC CEdit 自动换行功能