itextsharp.dll 下载:http://sourceforge.net/projects/itextsharp/

程序需引用:itextsharp.dll,itextsharp.pdfa.dll,PresentationFramework.dll

本人使用的是一般处理程序来写的,废话不多说代码才是硬道理,使用插件定位图片,表格是使用html转的pdf

         public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment;filename=报价单.pdf");
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
context.Response.ContentType = "application/pdf";
//文件临时存储路径
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "/PDF/" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
BaoJiaHtml(filePath);
//读取已建好的文件
FileStream fs = new FileStream(filePath, FileMode.Open);
while (true)
{
byte[] buffer = new byte[fs.Length];
//将文件读取成byte字节
int len = fs.Read(buffer, , (int)fs.Length);
if (len == ) break;
if (len == )
{
context.Response.BinaryWrite(buffer);
break;
}
else
{
//读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
byte[] b = new byte[len];
for (int i = ; i < len; i++)
{
b[i] = buffer[i];
}
context.Response.BinaryWrite(b);
break;
}
}
fs.Flush();
fs.Close();
//删除临时文件
if (File.Exists(filePath))
{
File.Delete(filePath);
}
context.Response.End();
}
         #region  html转pdf zhy + void BaoJiaHtml(string filePath)
/// <summary>
/// html转pdf
/// </summary>
/// <param name="filePath">文件存储路径</param>
public static void BaoJiaHtml(string filePath)
{
string imagePath = System.AppDomain.CurrentDomain.BaseDirectory + @"/Content/IMG/BaoJiaDan/yinz.png";
//注册字体(pdf上显示中文必须注册字体)
FontFactory.RegisterFamily("宋体", "simsun", @"c:\windows\fonts\SIMSUN.TTC,0");
// FontFactory.RegisterFamily("宋体", "simsun bold", @"c:\windows\fonts\SIMSUN_bold.TTC");
//获得商品
List<商品列表类> product = GetProducts();
//获得已拼接好的
StringBuilder sb = PdfHtml(product);
Document document = new Document();
//设置页面大小是A4纸大小
document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
//创建文档
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open(); HTMLWorker html = new HTMLWorker(document);
//将html转为pdf
html.Parse(new StringReader(sb.ToString())); //添加图片
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
//计算图片y轴的位置
int imgY = CountYCoordinate(product.Count); //设置图片的X,Y轴
img.SetAbsolutePosition(, imgY);
//将图片添加到文档中
document.Add(img);
document.Close();
}
#endregion
  #region 根据商品数量设置图标位置 zhy + int CountYCoordinate(int productCount)
/// <summary>
/// 根据商品数量设置图标位置
/// </summary>
/// <param name="productCount">商品数量</param>
/// <returns>公司公章图片的Y轴坐标</returns>
public static int CountYCoordinate(int productCount)
{
int imgY = ;
//只有一页商品
if (productCount > && productCount < )
{
imgY = imgY - (productCount - ) * ;
}
//公章位置固定在顶部(一满页有22件商品)
else if ((productCount >= && productCount <= ) || ((productCount - ) % == ) || ((productCount - ) % == ) || ((productCount - ) % == ) || ((productCount - ) % == ))
{
imgY = ;
}
//商品数量超过12件并不满足固定公章位置的条件,计算y轴坐标
else if (productCount > )
{
imgY = ;
//商品数量减去第一页的半页商品数量在余满页商品数量计算Y轴坐标
imgY = imgY - ((productCount - ) % ) * ;
}
return imgY;
}
#endregion

由于html转pdf有很多css属性在转换时都不支持所以使用纯表格进行布局,支持的属性此博客写的比较全,大家可以看看 http://blog.163.com/wangsongtao_82/blog/static/54480071201351921328227/

  #region 拼接html内容 zhy + StringBuilder PdfHtml(List<us_ShoppingTrolley> products, int pagecount)
/// <summary>
/// 拼接html内容
/// </summary>
/// <param name="products">购物车商品列表</param>=
/// <returns></returns>
public static StringBuilder PdfHtml(List<us_ShoppingTrolley> products)
{
StringBuilder sb = new StringBuilder();
//标题
sb.Append("<table border=\"0\" width=\"80%\" style=\"margin:0 auto;\"><tr>");
sb.Append("<td><img src=\"" + System.AppDomain.CurrentDomain.BaseDirectory + "/Content/IMG/BaoJiaDan/logo.jpg\" width=\"150\" height=\"60\" /></td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 18pt; height: 50px; line-height: 22px;\" encoding=\"Identity-H\" >PDF生成</td></tr></table>");
//中间部分的表格
sb.Append("<table border=\"0.5\" width=\"100%\" style=\"margin:0 auto;\"><tr>");
sb.Append("<td width=\"100%\" height=\"0.1px\" bgcolor=\"rgb(0,0,0)\"></td></tr></table>");
sb.Append("<table border=\"0\" width=\"100%\" style=\"float:left;\">"); sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11:</td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 18pt;width:45%; \" encoding=\"Identity-H\">12354348</td></tr>");
sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11:</td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">25487587</td></tr>");
sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">1252:</td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">电话:400-855-3618</td></tr>");
sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11111:</td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">网址:www.dagou100.com</td></tr>");
sb.Append("<tr><td><table border=\"0.5\" width=\"80%\"><tr><td style=\"font-family: 宋体; font-size: 14pt;width:45%;line-height:20px;height:20px; \" encoding=\"Identity-H\">123452:</td><td colspan=\"3\" style=\"font-family: 宋体; font-size: 12pt;width:45%;line-height:20px;height:20px; \" encoding=\"Identity-H\">" + Guid.NewGuid().ToString() + "</td></tr></table></td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">12452:122527</td></tr></table>");
//商品表格
//表格标题
sb.Append("<table width=\"100%\" border=\"0.5\" style=\"margin:0 auto;\">");
sb.Append("<tr><th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">序号</th>");
sb.Append("<th colspan=\"2\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">商品编号</th>");
sb.Append("<th colspan=\"10\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">商品名称</th>");
sb.Append("<th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">货期</th>");
sb.Append("<th colspan=\"2\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">价格</th>");
sb.Append("<th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">数量</th>");
sb.Append("<th colspan=\"3\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">小计</th></tr>");
//表格内容
int productcount = ;
int no_price = ;
decimal productPriceCount = ;
if (products != null && products.Count > )
{
foreach (商品列表类 item in products)
{
productcount++; sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + productcount + "</td>");
sb.Append("<td colspan=\"2\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + item.productId + "</td>");
sb.Append("<td colspan=\"10\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + item.productName + "</td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">暂无</td>");
sb.Append("<td colspan=\"2\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\"></td>");
sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\"></th>");
sb.Append("<td colspan=\"3\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\"></td></tr>"); }
sb.Append("</table>");
int count = products.Count - ;
//判断商品数量插入空白展位行方便定位公司公章 (由于我的公章必须与商品总计在一起所以图片需要灵活定位,也方便分页时定位)
if (((count % == )) && products.Count != )
{
sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr><tr><td style=\"width:100px;height:100px;\">.</td></tr><tr><td style=\"width:100px;height:100px;\">.</td></tr>");
sb.Append("</table>");
}
else if (products.Count == || (count % == ))
{
sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr><tr><td style=\"width:100px;height:100px;\">.</td></tr>");
sb.Append("</table>");
}
else if (products.Count == || (count % == ))
{
sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr></tr>");
sb.Append("</table>");
}
}
else
{
sb.Append("</table>");
}
//底部
sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">"); sb.Append("<tr><td style=\"font-family: 宋体; font-size: 14pt;text-align:right\" encoding=\"Identity-H\">商品总计:来电咨询</td></tr>"); sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:right\" encoding=\"Identity-H\">注:以上金额含17%的增值税,部分商品不含运费</td></tr>");
sb.Append("<tr><td></td></tr>");
sb.Append("<tr><td></td></tr>");
sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※</td></tr>");
sb.Append("</table>");
return sb;
}
#endregion

此数是获取商品列表代码就不贴了,同志们灵活替换吧

         #region 获取购物车商品列表 zhy +  List<商品列表类> GetProducts()
/// <summary>
/// 获取购物车商品列表
/// </summary>
/// <returns>List<us_ShoppingTrolley></returns>
public static List<商品列表类> GetProducts()
{
return new List<商品列表类>();
}

ok,帮助到大家的话请给点个推荐吧,自己写的哦

C# 生成pdf文件客户端下载的更多相关文章

  1. 在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all

    在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all 前天来了个新需求, 有一个授权书的文件要点击下载, 需要在前端生成, ...

  2. ThinkPHP3.2.3扩展之生成PDF文件(MPDF)

    目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...

  3. [轉載]史上最强php生成pdf文件,html转pdf文件方法

    之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...

  4. asp.net生成PDF文件 (1)

    asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  5. 怎么用PHP在HTML中生成PDF文件

    原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...

  6. .NET生成PDF文件

    C#未借助第三方组件,自己封装通用类,生成PDF文件. 调用方式: //路径 string path = @"C:\yuannwu22.pdf"; //内容 string strC ...

  7. 史上最强php生成pdf文件,html转pdf文件方法

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  8. java调用wkhtmltopdf生成pdf文件,美观,省事

    最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...

  9. Itext生成pdf文件

    来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...

随机推荐

  1. 提取header头进行模块化处理

    在进行爬取网上东西的时候一般网站都做了UA的过滤,解决办法就是在代码中加入. 所以才有了本篇提取header头信息单独写成一个模块或者说是函数/类的想法,直接上示例 1.把UA头信息在浏览器中复制出来 ...

  2. Linux系统资源监控--linux命令、nmon和spotlight

    前言: 系统资源监控一般监控系统的CPU,内存,磁盘和网络.系统分为windows和Linux.本篇主要记录Linux. Linux系统资源监控常用命令及工具 一.常用命令:top.free.iost ...

  3. 推荐一些相见恨晚的 Python 库 「一」

    扯淡 首先说明下,这篇文章篇幅过长并且大部分是链接,因此非常适合在电脑端打开访问. 本文内容摘自 Github 上有名的 Awesome Python.这是由 vinta 在 14 年发起并持续维护的 ...

  4. jquery.ajax之beforeSend方法使用介绍

    常见的一种效果,在用ajax请求时,没有返回前会出现前出现一个转动的loading小图标或者“内容加载中..”,用来告知用户正在请求数据.这个就可以用beforeSend方法来实现. 下载demo:a ...

  5. Java学习3_一些基础3_16.5.7

    字符串的一些常用方法: int length() String replace(CharSequence oldString,CharSequence newString) 用新字符串代替原字符串,返 ...

  6. vue基础---计算属性和侦听器

    [一]计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example"> ...

  7. 用Docker构建MySQL镜像

    构建MySQL镜像 本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动MySQL服务接受外部连接,主要是通过D ...

  8. 【原】Mysql常用语句

    1.修改编码方式为UTF-8 ALTER   TABLE   表名  CHANGE  列名  新列名  VARCHAR(255)    CHARACTER  SET  utf8  COLLATE    ...

  9. Spring Boot 与ElasticSearch

    一.ElasticSearch 介绍 ​ 开源的 ElasticSearch 是目前全文搜索引擎的首选,它是一个分布式搜索服务,提供Restful API,它可以快速地存储.搜索和分析海量数据.底层基 ...

  10. 子集和问题 - 回溯&搜索

    题目地址:http://www.51cpc.com/web/problem.php?id=4264 其实一看到这道题我就想到了01背包,但是卡死在了如何顺序输出: 个人人为回溯本身就会用到搜索,像是充 ...