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.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...
随机推荐
- Think PHP中URL_MODE相关事项
官网上有关于URL_MODE的解释:http://document.thinkphp.cn/manual_3_2.html#url 这里主要讲一下URL_MODE为2,即REWRITE模式. REWR ...
- python游戏开发:pygame事件与设备轮询
一.pygame事件 1.简介 pygame事件可以处理游戏中的各种事情.其实在前两节的博客中,我们已经使用过他们了.如下是pygame的完整事件列表: QUIT,ACTIVEEVENT,KEYDOW ...
- MFC获取各类指针句柄
最近有些人在问MFC编程一些要点,有一些句柄的获取.指针的获取是常见的问题,本文将对这些问题做以解释,参考了前人的笔录(见reference),希望能够帮助大家更方便地进行MFC程序开发. 一般我们使 ...
- CSS 命名规范 BEM 思想
Part.1 何为 BEM? BEM :Block ( 块 ) 丶Element ( 元素 ) 丶Modifier ( 修饰符 ) 出 处:是由 Yandex 团队提出的一种前端命名方法论 优 点:命 ...
- Controller传值到前端页面的几种方式
一丶追加字符串传值 #region 02-追加字符串传值 /// <summary> /// 02-追加字符串传值 /// </summary> /// <returns ...
- Linux(Centos7)下搭建SVN服务器(新手上路)
以前都是别人直接给地址在svn上,下载或者上传东西,如今要自己建一个版本库用来存放东西.1.安装svnyum install -y subversion 2.查看svn安装位置还有哪些文件rpm -q ...
- vsCode scss安装
点击在settings.json中编辑写入代码: { /** Easy Sass 插件 **/ "easysass.formats": [ { "format" ...
- webpack之webpack-dev-server 与 webpack-hot-server
最近在弄框架,用到了webpack打包,当然不可避免的遇到了开发实际问题.在实际开发中,我们不可能改一个文件,就去构建一次,于是想到了实时更新.查看webpack,看到了webpack-dev-ser ...
- UVA - 11214 Guarding the Chessboard(迭代加深搜索)
题目: 输入一个n*m的棋盘(n,m<10),某些格子有标记,用最少的皇后守卫(即占据或攻击)所有的标记的格子.输出皇后的个数. 思路: 一开始没有想到用迭代加深搜索,直接dfs结果还没写完就发 ...
- python OOP(2)
调用方法有两种形式 实例调用 直接调用后序参数即可 类调用 调用时需要先加上实例 示例 class test1: def pt(self,txt): #定义函数 test1.txt=txt print ...