c#中使用ABCpdf處理PDF,so easy


QQ交流群:276874828  (ABCpdf )

 

這幾天項目中需要將頁面導成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頁面涵蓋了很多常見問題以及解決方式,還算比較詳盡了,祝大家使用的愉快。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM