C# 生成pdf文件客户端下载
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文件客户端下载的更多相关文章
- 在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all
在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all 前天来了个新需求, 有一个授权书的文件要点击下载, 需要在前端生成, ...
- ThinkPHP3.2.3扩展之生成PDF文件(MPDF)
目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...
- [轉載]史上最强php生成pdf文件,html转pdf文件方法
之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...
- asp.net生成PDF文件 (1)
asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- 怎么用PHP在HTML中生成PDF文件
原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...
- .NET生成PDF文件
C#未借助第三方组件,自己封装通用类,生成PDF文件. 调用方式: //路径 string path = @"C:\yuannwu22.pdf"; //内容 string strC ...
- 史上最强php生成pdf文件,html转pdf文件方法
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- java调用wkhtmltopdf生成pdf文件,美观,省事
最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...
- Itext生成pdf文件
来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...
随机推荐
- SQL server 上机练习题
首先创建一个数据库,里面有 登录表 学生表 课程表 选课表 成绩表 1. 查询Student表中的所有记录的Sname.Ssex和Class列.2. 查询教师所有的单位即不重复的Depart列 ...
- Qt杂记——布局、信号与槽等
1.QHBoxLayout布局设置拉伸: ui->TopLayout->setStretch(,); //left ui->TopLayout->setStretch(,); ...
- AdMob设计工具google web designer
一.google web designer工具中文文档: https://support.google.com/webdesigner?hl=zh-Hans#topic=3227692 我用的版本:应 ...
- CAD插入图片
在CAD设计绘图时,需要插入外部图片,可以设置图片的缩放比例.旋转角度.图片显示文件名等属性. 主要用到函数说明: _DMxDrawX::DrawImageMark 绘图制一个图象标记对象.详细说明如 ...
- 10Oracle Database 数据表数据查询
Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...
- Java基础——面向对象(封装——继承——多态 )
对象 对象: 是类的实例(实现世界中 真 实存在的一切事物 可以称为对象) 类: 类是对象的抽象描述 步骤: 1.定义一个类 (用于 描述人:) ( * 人:有特征和行为) 2.根据类 创建对象 -- ...
- 07Html、CSS
07Html.CSS-2018/07/17 1.HTML是用来描述网页的一种标记语言,是一套标记标签.HTML用使用标记标签来描述网页.超文本 标记语言. 2.格式 <html> < ...
- C++ Primer(第4版)-学习笔记-第4部分:面向对象编程与泛型编程
第15章 面向对象编程OOP(Object-oriented programming) 面向对象编程基于三个基本概念:数据抽象.继承和动态绑定. 在 C++ 中,用类进行 ...
- Python学习-if条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 单分支条件语句 if 判断条件: 条件成立,执行该代码块.... 注意:与其他编程语言,如Java和C ...
- Linux终端中的字符串使用总结(截取,切片,查找替换,转换,变量赋值等)
1.基于模式匹配截取字符串 1.#.##号截取 #号截取,从左边开始,以一个出现的分隔符(最左)为准,删除左边字符. ##号截取,从左边开始,以最后(最右)一个分隔符为准,删除左边字符. str='h ...