第一种:
用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
示例源码:
代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
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.Font×× = Request.Form["××Fonts"] == "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 " ] = "
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.Font×× = Request.Form["××Fonts"] == "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 " ] = "
Web page is inaccessible. Please try another URL.
" ;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);
}
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-630143/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-630143/