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页面涵盖了很多常见问题以及解决方式,还算比较详尽了,祝大家使用的愉快。

原文地址:https://www.cnblogs.com/laozhao8/p/3174112.html