zoukankan      html  css  js  c++  java
  • c#中使用ABCpdf处理PDF

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;using System.Web.UI;
    using System.Web.UI.WebControls;
    using WebSupergoo.ABCpdf9;using System.Text;
    namespace ABCpdfTest{
    ///
    /// 官方网站:http://www.websupergoo.com/
    /// demo用的是当前的最新版本ABCpdf .NET 9.1 X64,支持当前windows的主流操作系统,包括最新的win8,IE10(服务器版本)
    /// ABCpdf有30天的试用期
    /// 引用方式,安装ABCpdf组件,有两个DLL是有用的,需要对ABCpdf.dll添加引用,ABCpdf9-64.dll(引擎组件)放在bin目录下就可以了
    /// 它有其他组件比如(iTextSharp)所不具备的功能,如能直接指定一个URL就可以将页面转换为PDF,这也是它的强大之处托福答案 www.jx-jf.com
    /// 在选择版本时要注意,区分64位和32位,如果版本放错了,会发生错误,在IIS的部署上一定要注意,这里很可能会出现问题,请参考官方资料
    /// 地址:http://www.websupergoo.com/support.htm 常见问题介绍的比较详细
    ///
    public partial class Default : System.Web.UI.Page
    {
    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";
    }
    ///
    /// 指定URL生成PDF
    ///
    ///
    ///
    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);
    } ///
    /// 自定义页面
    ///
    ///
    ///
    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);
    }
    ///
    /// 支持HTML元素
    ///
    ///
    ///
    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("
    How to use the ABCpdf
    "); doc.AddHtml("
    "); doc.AddHtml(@"
    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…
    "); //这里是不是很神奇,html都支持,很灵活,赞一个 doc.Save(Server.MapPath(fileName)); byte[] buffer = doc.GetData(); DownloadPDF(fileName, buffer);
    }
    ///
    /// 自定义页眉页脚
    ///
    ///
    ///
    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("
    How to use the ABCpdf
    "); doc.AddHtml("
    "); //自定义页眉
    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("" + "Laozhao learn ABCpdf,Save time for" + DateTime.Now.ToString() + "");
    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("Page: " + 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启用缓存
    */
    }}
     

  • 相关阅读:
    北京联通光猫WO-36(HG220GS-U)改为桥接模式
    使用DataGrip导入数据
    Spring知识点
    mybatis-generator
    项目中mybatis连接mysql常见问题
    Spring AOP
    Volatile
    Redis知识点
    Spring IOC
    Observer模式
  • 原文地址:https://www.cnblogs.com/haosola/p/3601945.html
Copyright © 2011-2022 走看看