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. 利用反射重写toString()方法

    为了方便输出对象,Object类提供了toString()方法.但是该方法的默认值是由类名和哈希码组成的,实用性并不强.通常需要重写该方法以提供更多的信息.本实例使用反射输出类的包.类的名字.类的公共 ...

  2. 使ThinkPHP(3.2.3)的分页类支持Bootstrap风格

    ThinkPHP 3.2.3自带的分页类位于:/ThinkPHP/Library/Think/Pages.class.php ,官方文档在这里:ThinkPHP3.2.3数据分页 Pages.clas ...

  3. Codeforces_750_C_(二分查找)

    C. New Year and Rating time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. Django框架 之基础入门

    django是一款MVT的框架 一.基本过程 1.创建项目:django-admin startproject 项目名称 2.编写配置文件settings.py(数据库配置.时区.后台管理中英文等) ...

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

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

  6. 牛客多校Round 6

    Solved:3 rank:156 J. Heritage of skywalker 学习一下nth_element 可以o (n)的找出前多少大的元素 #include <bits/stdc+ ...

  7. 牛客多校Round 5

    Solved:3 rank:195 F. take 官方题解:小 A 在打开第 i 个箱子后会交换手中的钻石和第 i 个箱子中的钻石 当且仅当第 i个箱子的钻石是前 i 个箱子打开后出现的所有钻石里最 ...

  8. "ping: unknown host www.baidu.com"问题解决方式

    参考:https://blog.csdn.net/wbainngg123/article/details/51540535 在虚拟机VMware里选择桥接模式,并配置网络之后,发现ping ip地址可 ...

  9. UVA - 11175 From D to E and Back(思路)

    题目: 思路: 如图E:图中a.b.c.d是有向图D中的顶点,如果ac.bc都指向cd,而ac又指向ce,那bc同样应该有一条指向ce的边不然就不能从图D转换来.所以直接枚举顶点就可以了. 代码: # ...

  10. Linux有几种安装软件的方式?????

    看了Windows后台软件安装的过程,想必Linux也是这样.拿RHEL7来打比方 最开始Linux上安装软件只提供源代码,需要自己去编译源代码,拷贝库文件等 RPM 红帽软件包管理器可以自动地执行上 ...