文章目錄
這幾天項目中需要將頁面導成PDF,剛開始使用iTextSharp,覺得在分頁處理上比較複雜,後來無意中看到了ABCpdf,使用非常簡單,並將一些常用操作記錄下來,平時可以瞅瞅,也分享給大傢伙們,廢話不多說,直接貼代碼。
2013/7/6修改:昨天發了這篇博文之後,今天發現不在首頁顯示了,好生奇怪,原來部落格園發來了訊息,被過濾了,我這發的是個人分享我擦,不就是有個官網連結,並且代碼多一點嗎,給我封了幹嘛???
ABCpdf簡介
官方網站:http://www.websupergoo.com/
demo用的是當前的最新版本ABCpdf .NET 9.1 X64,支援當前最新的win8,IE10(伺服器版本)以及舊版本server2003,xp,vista,win7,win8
ABCpdf有30天的試用期
引用方式,安裝ABCpdf組件,有兩個DLL是有用的,需要對ABCpdf.dll添加引用,ABCpdf9-64.dll(引擎組件)放在bin目錄下就可以了
它有其他組件比如(iTextSharp)所不具備的功能,如能直接指定一個URL就可以將頁面轉換為PDF,這也是它的強大之處
在選擇版本時要注意,區分64位和32位,如果版本放錯了,會發生錯誤,在IIS的部署上一定要注意,這裡很可能會出現問題,請參考官方資料:http://www.websupergoo.com/support.htm 常見問題介紹的比較詳細
用法簡介
下面上一點代碼看看吧。
添加引用:
using WebSupergoo.ABCpdf9;
string url = "http://www.websupergoo.com/support.htm"; private void DownloadPDF(string fileName, byte[] buffer) { Response.Buffer = false; Response.AddHeader("Connection", "Keep-Alive"); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", buffer.Length.ToString()); Response.BinaryWrite(buffer); } private string GetFileName() { return DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".pdf"; } /// <summary> /// 指定URL產生PDF /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { string fileName = GetFileName(); Doc doc = new Doc(); doc.Page = doc.AddPage();//建立一個頁面 doc.Rect.Inset(10, 10);//設定矩形邊距 int id = doc.AddImageUrl(url, true, 800, false);//添加一個URL的頁面返回一個頁面ID //以下這段代碼很重要,關係到分頁,如果不寫這段代碼,就無法分頁 while (true) { //這個判斷應該是判斷id是否是頁面對象,如果不是,就跳出迴圈 if (!doc.Chainable(id)) { break; } doc.Page = doc.AddPage(); id = doc.AddImageToChain(id);//這裡是將這個可連結的對象ID添加到頁面並返回一個id } doc.Flatten();//壓縮pdf doc.Save(Server.MapPath(fileName));//這裡儲存pdf到相對路徑 //你也你可以這樣做把檔案輸出 byte[] buffer = doc.GetData();//得到bytes[] DownloadPDF(fileName, buffer); } /// <summary> /// 自訂頁面 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { string fileName = GetFileName(); Doc doc = new Doc(); doc.Page = doc.AddPage();//建立一個頁面 doc.Rect.Inset(10, 10);//設定矩形邊距,這裡Rect是一個重要的對象,你也可以doc.Rect.String來設定屬性 doc.FontSize = 24; //設定預設字型大小 doc.Color.String = "89,89,254"; int id = doc.AddText("Hello World!!!");//添加文字 doc.FrameRect(); //添加邊框操作 doc.Save(Server.MapPath(fileName)); byte[] buffer = doc.GetData(); DownloadPDF(fileName, buffer); } /// <summary> /// 支援HTML元素 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button3_Click(object sender, EventArgs e) { string fileName = GetFileName(); Doc doc = new Doc(); doc.Page = doc.AddPage(); doc.Rect.Inset(10, 10); doc.AddHtml("<h2>How to use the ABCpdf</h2>"); doc.AddHtml("<hr>"); doc.AddHtml(@"<p>Use ABCpdf to create Adobe PDF documents on the fly. You won't believe how simple - yet how powerful it truly is. Find out more... If you've been using Version 8 you'll love Version 9. It includes many powerful new features designed to make your life easier. Find out more... or check out our Feature Chart... ABCpdf .NET is a .NET Native product encapsulated in an easy-to-deploy set of DLLs. It also offers a virtualized COM interface designed for backwards compatibility with ABCpdf ASP and Classic ASP/COM. ABCpdf is normally priced from $329. However as a special offer we'll give you a free license key - all you have to do is link back to our web site. For full details check out our link guidelines...</p>"); //這裡是不是很神奇,html都支援,很靈活,贊一個 doc.Save(Server.MapPath(fileName)); byte[] buffer = doc.GetData(); DownloadPDF(fileName, buffer); } /// <summary> /// 自訂頁首頁尾 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button4_Click(object sender, EventArgs e) { string fileName = GetFileName(); Doc doc = new Doc(); doc.Page = doc.AddPage(); doc.Rect.Inset(20, 40); doc.AddHtml("<h2>How to use the ABCpdf</h2>"); doc.AddHtml("<hr>"); //自訂頁首 doc.Rect.String = "24 750 588 778"; //記得這裡要定位哦 doc.HPos = 0; //置中, 0代表居左, 1代表居右 doc.VPos = 0.5; //置中, 0代表靠上, 1代表靠下 doc.Color.String = "blue"; //藍色 for (int i = 1; i <= doc.PageCount; i++) { doc.PageNumber = i; doc.AddHtml("<b><font>" + "Laozhao learn ABCpdf,Save time for" + DateTime.Now.ToString() + "</font></b>"); doc.AddLine(24, 750, 588, 750); //畫一條分隔線 } //頁尾 doc.Rect.String = "24 12 588 40"; doc.HPos = 1.0; //Right doc.VPos = 0.5; //Middle doc.Color.String = "black"; for (int i = 1; i <= doc.PageCount; i++) { doc.PageNumber = i; doc.AddHtml("<u>Page:</u> " + i.ToString() + " / " + doc.PageCount.ToString()); doc.AddLine(24, 40, 588, 40); } doc.Save(Server.MapPath(fileName)); byte[] buffer = doc.GetData(); DownloadPDF(fileName, buffer); }
以上就是我用到的一些部分功能,還有一些功能也非常好使
Doc還支援AddImageHtml
參數說明:
html:需要添加的html
paged:是否分頁,true啟用分頁
width:頁面的寬度(瀏覽器解析html時瀏覽器的寬度)
disableCache:是否忽略緩衝,true不啟用緩衝,false啟用緩衝
需要提的一點還是支援人員方面,官網做的不錯,一個support頁面涵蓋了很多常見問題以及解決方式,還算比較詳盡了,祝大家使用的愉快。