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. java实现麦克风自动录音

    最近在研究语音识别,使用百度的sdk.发现只有识别的部分,而我需要保存音频文件,并且实现当有声音传入时自动生成音频文件. 先上代码: public class EngineeCore { String ...

  2. (转)淘淘商城系列——Solr的安装

    http://blog.csdn.net/yerenyuan_pku/article/details/72874134 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service ...

  3. Solr搜索引擎 — 通过mysql配置数据源

    一,准备数据库数据表结构 CREATE TABLE `app` ( `id` int(11) NOT NULL AUTO_INCREMENT, `app_name` varchar(255) NOT ...

  4. MFC_1.3 控件子类化 消息反射

    控件子类化 如果想要在默认的控件类中添加一些功能,就需要子类化一个控件类 在类内可以响应控件所有的消息,并且可以添加自己的函数和数据 通过类向导子类化控件的步骤 打开类向导,创建一个 MFC 类,不要 ...

  5. BZOJ 3884: 上帝与集合的正确用法 扩展欧拉定理 + 快速幂

    Code: #include<bits/stdc++.h> #define maxn 10000004 #define ll long long using namespace std; ...

  6. Java基础(三)--final关键字

    final通常是指"不可改变的",例如我们使用的常量 通常可以有三种使用情况: 一.final修饰数据 如果final修饰数据,也就是通常所说的常量,从字面上看,常量就是不能修改的 ...

  7. RabbitMQ系列(八)--顺序消费模式和迅速消息发送模式

    MQ使用过程中,有些业务场景需要我们保证顺序消费,而如果一个Producer,一个Queue,多个Consumer的情况下是无法保证顺序的 举例: 1.业务上产生三条消息,分别是对数据的增加.修改.删 ...

  8. 01C++编辑编译运行环境

    C++编辑编译运行环境 Bloodshed Dev-C++ Microsoft Visual Studio

  9. 2019西安多校联训 Day3

    试题链接:http://www.accoders.com/contest.php?cid=1895    考试密码请私信; 特别鸣谢:zkc奆佬帮助我优化本篇题解(语言表达方面) T1 显然二分求解的 ...

  10. MYEclipse Available Memory is low 警告 解决方法

    1,  设置Eclipse内存使用情况 修改eclipse根目录下的eclipse.ini文件 -vmargs  //虚拟机设置 -Xms40m -Xmx256m -XX:PermSize=128M ...