Asp.net中实现HtmltoPdf功能,非常方便实用,且功能强大
第一种:
用itextsharp组件,此方法稍微比较复杂,但比较灵活,相关资料可以百度或者园子里搜索到,本文略.
第二种:
用WebSupergoo.ABCpdf.DotNET组件,官方网址是http://www.websupergoo.com/。
先下载WebSupergoo.ABCpdf.DotNET.v7.0.1.1版本,经测试绝对可用的序列号:
Standard License :341-639-358
Professional License:719-253-057
ABCpdf.DotNET简易使用教程:
http://www.cnblogs.com/Icebird/archive/2009/06/22/386714.html
示例源码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using WebSupergoo.ABCpdf7;
public partial class HtmltoPdf : System.Web.UI.Page
{
private string Stype = "";
private string CompanyId = "";
private string CompanyName = "";
private string TempUrl = "";
private string theURL = "";
private string TemptheURL = "";
private string FileName = "";
private Dictionary<string, string> dickey = new Dictionary<string, string>();
protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = -1000;
if (!IsPostBack)
{
Doc theDoc = new Doc();
theDoc.Clear();
bool pagedOutput = true;
theDoc.HtmlOptions.PageCacheEnabled = false;
theDoc.HtmlOptions.AddForms = true;
theDoc.HtmlOptions.AddLinks = true;
theDoc.HtmlOptions.UseJava = true;
theDoc.HtmlOptions.UseActiveX = true;
theDoc.HtmlOptions.UseScript = true;
theDoc.HtmlOptions.Timeout = 15000;
theDoc.HtmlOptions.TargetLinks = true;
theDoc.MediaBox.String = "0 0 740 1000";//设置添加新页面时,页面的大小
theDoc.Rect.String = "14 7 722 986";//当前输出区间
#region 可能会用到的代码
//theDoc.SetInfo(0, "License", "cd9b5c07db69df2bf57c0a04d9bca58b10c44889c9fb197984e592f49addfce5ec5fe85d7b9205bc"); //导入注册信息
//theDoc.FrameRect();//为当前rect添加边框
//theDoc.HtmlOptions.FontEmbed = Request.Form["EmbedFonts"] == "on";
//theDoc.HtmlOptions.HideBackground = Request.Form["HideBackground"] == "on";
//theDoc.HtmlOptions.LogonName = Request.Form["UserName"];
//theDoc.HtmlOptions.LogonPassword = Request.Form["Password"];
//theDoc.HtmlOptions.BrowserWidth = 0;
//theDoc.HtmlOptions.ImageQuality = 101;
//theDoc.FontSize = 12;
//theDoc.Font = theDoc.AddFont("宋体", "ChineseS");
//theDoc.SetInfo(theDoc.Page, "/MediaBox:Rect", "0 0 1024 2000");
//double w = 960;
//double h = 772;
//double x = 8;
//double y = 8;
//theDoc.Rect.SetRect(x, y, w, h);
//theDoc.Rect.Width=815;
#endregion
#region 根据信息类型生成对应的报告
TempUrl = "/companies/CompanyBasicInfo.aspx?cID=" + CompanyId;
FileName = HttpUtility.UrlEncode("CompanyBasicInfo_By_" + CompanyName);
dickey.Add("企业基本信息", "/companies/CompanyBasicInfo.aspx?cID=" + CompanyId);
#endregion
foreach (KeyValuePair<string, string> kvp in dickey)
{
theURL = "http://127.0.0.1" + kvp.Value;//theURL = "http://" + System.Web.HttpContext.Current.Request.Url.Host + TempUrl;
TemptheURL = theURL;
AddToPdf(theDoc, TemptheURL, pagedOutput, theDoc.PageCount + 1, kvp.Key.ToString());
}
try
{
//theDoc.Save(Server.MapPath("/uploadfile/MyPDF.PDF"));
//弹出保存到本地的对话框
byte[] theData = theDoc.GetData();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", theData.Length.ToString());
//if (Request.QueryString["attachment"] != null)
Response.AddHeader("content-disposition", "attachment; filename=" + FileName + ".PDF");
//else
// Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF");
Response.BinaryWrite(theData);
Response.End();
// }
theDoc.PageNumber = 1;
}
catch
{
Session["warning"] = "<p>Web page is inaccessible. Please try another URL.</p>";
Response.End();
}
}
}
public void AddToPdf(Doc NowDoc, string NowUrl, bool pagedOutput, int CurrPage, string BookMarkName)
{
int theID;
NowDoc.Page = NowDoc.AddPage();
NowDoc.AddBookmark(BookMarkName, true);
theID = NowDoc.AddImageUrl(NowUrl);
if (pagedOutput)
{
///如果内容过长下面的程序就会实现自动分页
while (true)
{
if (!NowDoc.Chainable(theID))
break;
NowDoc.Page = NowDoc.AddPage();
theID = NowDoc.AddImageToChain(theID);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using WebSupergoo.ABCpdf7;
public partial class HtmltoPdf : System.Web.UI.Page
{
private string Stype = "";
private string CompanyId = "";
private string CompanyName = "";
private string TempUrl = "";
private string theURL = "";
private string TemptheURL = "";
private string FileName = "";
private Dictionary<string, string> dickey = new Dictionary<string, string>();
protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = -1000;
if (!IsPostBack)
{
Doc theDoc = new Doc();
theDoc.Clear();
bool pagedOutput = true;
theDoc.HtmlOptions.PageCacheEnabled = false;
theDoc.HtmlOptions.AddForms = true;
theDoc.HtmlOptions.AddLinks = true;
theDoc.HtmlOptions.UseJava = true;
theDoc.HtmlOptions.UseActiveX = true;
theDoc.HtmlOptions.UseScript = true;
theDoc.HtmlOptions.Timeout = 15000;
theDoc.HtmlOptions.TargetLinks = true;
theDoc.MediaBox.String = "0 0 740 1000";//设置添加新页面时,页面的大小
theDoc.Rect.String = "14 7 722 986";//当前输出区间
#region 可能会用到的代码
//theDoc.SetInfo(0, "License", "cd9b5c07db69df2bf57c0a04d9bca58b10c44889c9fb197984e592f49addfce5ec5fe85d7b9205bc"); //导入注册信息
//theDoc.FrameRect();//为当前rect添加边框
//theDoc.HtmlOptions.FontEmbed = Request.Form["EmbedFonts"] == "on";
//theDoc.HtmlOptions.HideBackground = Request.Form["HideBackground"] == "on";
//theDoc.HtmlOptions.LogonName = Request.Form["UserName"];
//theDoc.HtmlOptions.LogonPassword = Request.Form["Password"];
//theDoc.HtmlOptions.BrowserWidth = 0;
//theDoc.HtmlOptions.ImageQuality = 101;
//theDoc.FontSize = 12;
//theDoc.Font = theDoc.AddFont("宋体", "ChineseS");
//theDoc.SetInfo(theDoc.Page, "/MediaBox:Rect", "0 0 1024 2000");
//double w = 960;
//double h = 772;
//double x = 8;
//double y = 8;
//theDoc.Rect.SetRect(x, y, w, h);
//theDoc.Rect.Width=815;
#endregion
#region 根据信息类型生成对应的报告
TempUrl = "/companies/CompanyBasicInfo.aspx?cID=" + CompanyId;
FileName = HttpUtility.UrlEncode("CompanyBasicInfo_By_" + CompanyName);
dickey.Add("企业基本信息", "/companies/CompanyBasicInfo.aspx?cID=" + CompanyId);
#endregion
foreach (KeyValuePair<string, string> kvp in dickey)
{
theURL = "http://127.0.0.1" + kvp.Value;//theURL = "http://" + System.Web.HttpContext.Current.Request.Url.Host + TempUrl;
TemptheURL = theURL;
AddToPdf(theDoc, TemptheURL, pagedOutput, theDoc.PageCount + 1, kvp.Key.ToString());
}
try
{
//theDoc.Save(Server.MapPath("/uploadfile/MyPDF.PDF"));
//弹出保存到本地的对话框
byte[] theData = theDoc.GetData();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", theData.Length.ToString());
//if (Request.QueryString["attachment"] != null)
Response.AddHeader("content-disposition", "attachment; filename=" + FileName + ".PDF");
//else
// Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF");
Response.BinaryWrite(theData);
Response.End();
// }
theDoc.PageNumber = 1;
}
catch
{
Session["warning"] = "<p>Web page is inaccessible. Please try another URL.</p>";
Response.End();
}
}
}
public void AddToPdf(Doc NowDoc, string NowUrl, bool pagedOutput, int CurrPage, string BookMarkName)
{
int theID;
NowDoc.Page = NowDoc.AddPage();
NowDoc.AddBookmark(BookMarkName, true);
theID = NowDoc.AddImageUrl(NowUrl);
if (pagedOutput)
{
///如果内容过长下面的程序就会实现自动分页
while (true)
{
if (!NowDoc.Chainable(theID))
break;
NowDoc.Page = NowDoc.AddPage();
theID = NowDoc.AddImageToChain(theID);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库