itextsharp.dll 下载: http://sourceforge.net/projects/itextsharp/
程序需引用:itextsharp.dll,itextsharp.pdfa.dll,PresentationFramework.dll
本人使用的是一般处理程序来写的,废话不多说代码才是硬道理,使用插件定位图片,表格是使用html转的pdf
1 public void ProcessRequest(HttpContext context)
2 {
3 context.Response.Clear();
4 context.Response.AddHeader("content-disposition", "attachment;filename=报价单.pdf");
5 context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
6 context.Response.ContentType = "application/pdf";
7 //文件临时存储路径
8 string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "/PDF/" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
9 BaoJiaHtml(filePath);
10 //读取已建好的文件
11 FileStream fs = new FileStream(filePath, FileMode.Open);
12 while (true)
13 {
14 byte[] buffer = new byte[fs.Length];
15 //将文件读取成byte字节
16 int len = fs.Read(buffer, 0, (int)fs.Length);
17 if (len == 0) break;
18 if (len == 1024)
19 {
20 context.Response.BinaryWrite(buffer);
21 break;
22 }
23 else
24 {
25 //读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
26 byte[] b = new byte[len];
27 for (int i = 0; i < len; i++)
28 {
29 b[i] = buffer[i];
30 }
31 context.Response.BinaryWrite(b);
32 break;
33 }
34 }
35 fs.Flush();
36 fs.Close();
37 //删除临时文件
38 if (File.Exists(filePath))
39 {
40 File.Delete(filePath);
41 }
42 context.Response.End();
43 }
1 #region html转pdf zhy + void BaoJiaHtml(string filePath)
2 /// <summary>
3 /// html转pdf
4 /// </summary>
5 /// <param name="filePath">文件存储路径</param>
6 public static void BaoJiaHtml(string filePath)
7 {
8 string imagePath = System.AppDomain.CurrentDomain.BaseDirectory + @"/Content/IMG/BaoJiaDan/yinz.png";
9 //注册字体(pdf上显示中文必须注册字体)
10 FontFactory.RegisterFamily("宋体", "simsun", @"c:\windows\fonts\SIMSUN.TTC,0");
11 // FontFactory.RegisterFamily("宋体", "simsun bold", @"c:\windows\fonts\SIMSUN_bold.TTC");
12 //获得商品
13 List<商品列表类> product = GetProducts();
14 //获得已拼接好的
15 StringBuilder sb = PdfHtml(product);
16 Document document = new Document();
17 //设置页面大小是A4纸大小
18 document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
19 //创建文档
20 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
21 document.Open();
22
23 HTMLWorker html = new HTMLWorker(document);
24 //将html转为pdf
25 html.Parse(new StringReader(sb.ToString()));
26
27 //添加图片
28 iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
29 //计算图片y轴的位置
30 int imgY = CountYCoordinate(product.Count);
31
32 //设置图片的X,Y轴
33 img.SetAbsolutePosition(400, imgY);
34 //将图片添加到文档中
35 document.Add(img);
36 document.Close();
37 }
38 #endregion
1 #region 根据商品数量设置图标位置 zhy + int CountYCoordinate(int productCount)
2 /// <summary>
3 /// 根据商品数量设置图标位置
4 /// </summary>
5 /// <param name="productCount">商品数量</param>
6 /// <returns>公司公章图片的Y轴坐标</returns>
7 public static int CountYCoordinate(int productCount)
8 {
9 int imgY = 230;
10 //只有一页商品
11 if (productCount > 1 && productCount < 10)
12 {
13 imgY = imgY - (productCount - 1) * 20;
14 }
15 //公章位置固定在顶部(一满页有22件商品)
16 else if ((productCount >= 10 && productCount <= 12) || ((productCount - 12) % 22 == 19) || ((productCount - 12) % 22 == 20) || ((productCount - 12) % 22 == 21) || ((productCount - 12) % 22 == 0))
17 {
18 imgY = 475;
19 }
20 //商品数量超过12件并不满足固定公章位置的条件,计算y轴坐标
21 else if (productCount > 12)
22 {
23 imgY = 475;
24 //商品数量减去第一页的半页商品数量在余满页商品数量计算Y轴坐标
25 imgY = imgY - ((productCount - 12) % 22) * 22;
26 }
27 return imgY;
28 }
29 #endregion
由于html转pdf有很多css属性在转换时都不支持所以使用纯表格进行布局,支持的属性此博客写的比较全,大家可以看看 http://blog.163.com/wangsongtao_82/blog/static/54480071201351921328227/
1 #region 拼接html内容 zhy + StringBuilder PdfHtml(List<us_ShoppingTrolley> products, int pagecount)
2 /// <summary>
3 /// 拼接html内容
4 /// </summary>
5 /// <param name="products">购物车商品列表</param>=
6 /// <returns></returns>
7 public static StringBuilder PdfHtml(List<us_ShoppingTrolley> products)
8 {
9 StringBuilder sb = new StringBuilder();
10 //标题
11 sb.Append("<table border=\"0\" width=\"80%\" style=\"margin:0 auto;\"><tr>");
12 sb.Append("<td><img src=\"" + System.AppDomain.CurrentDomain.BaseDirectory + "/Content/IMG/BaoJiaDan/logo.jpg\" width=\"150\" height=\"60\" /></td>");
13 sb.Append("<td style=\"font-family: 宋体; font-size: 18pt; height: 50px; line-height: 22px;\" encoding=\"Identity-H\" >PDF生成</td></tr></table>");
14 //中间部分的表格
15 sb.Append("<table border=\"0.5\" width=\"100%\" style=\"margin:0 auto;\"><tr>");
16 sb.Append("<td width=\"100%\" height=\"0.1px\" bgcolor=\"rgb(0,0,0)\"></td></tr></table>");
17 sb.Append("<table border=\"0\" width=\"100%\" style=\"float:left;\">"); 19 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11:222</td>");
20 sb.Append("<td style=\"font-family: 宋体; font-size: 18pt;width:45%; \" encoding=\"Identity-H\">12354348</td></tr>");
21 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11:0000</td>");
22 sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">25487587</td></tr>");
23 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">1252:1244</td>");
24 sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">电话:400-855-3618</td></tr>");
25 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11111:23254235325</td>");
26 sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">网址:www.dagou100.com</td></tr>");
27 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>");
28 sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">12452:122527</td></tr></table>");
29 //商品表格
30 //表格标题
31 sb.Append("<table width=\"100%\" border=\"0.5\" style=\"margin:0 auto;\">");
32 sb.Append("<tr><th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">序号</th>");
33 sb.Append("<th colspan=\"2\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">商品编号</th>");
34 sb.Append("<th colspan=\"10\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">商品名称</th>");
35 sb.Append("<th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">货期</th>");
36 sb.Append("<th colspan=\"2\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">价格</th>");
37 sb.Append("<th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">数量</th>");
38 sb.Append("<th colspan=\"3\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">小计</th></tr>");
39 //表格内容
40 int productcount = 0;
41 int no_price = 0;
42 decimal productPriceCount = 0;
43 if (products != null && products.Count > 0)
44 {
45 foreach (商品列表类 item in products)
46 {
47 productcount++; 52 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + productcount + "</td>");
53 sb.Append("<td colspan=\"2\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + item.productId + "</td>");
54 sb.Append("<td colspan=\"10\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + item.productName + "</td>");
55 sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">暂无</td>");
56 sb.Append("<td colspan=\"2\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">12457</td>");
57 sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">1</th>");
58 sb.Append("<td colspan=\"3\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">12457</td></tr>"); 60 }
61 sb.Append("</table>");
62 int count = products.Count - 12;
63 //判断商品数量插入空白展位行方便定位公司公章 (由于我的公章必须与商品总计在一起所以图片需要灵活定位,也方便分页时定位)
64 if (((count % 22 == 19)) && products.Count != 12)
65 {
66 sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
67 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>");
68 sb.Append("</table>");
69 }
70 else if (products.Count == 10 || (count % 22 == 20))
71 {
72 sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
73 sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr><tr><td style=\"width:100px;height:100px;\">.</td></tr>");
74 sb.Append("</table>");
75 }
76 else if (products.Count == 11 || (count % 22 == 21))
77 {
78 sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
79 sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr></tr>");
80 sb.Append("</table>");
81 }
82 }
83 else
84 {
85 sb.Append("</table>");
86 }
87 //底部
88 sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">"); 92 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 14pt;text-align:right\" encoding=\"Identity-H\">商品总计:来电咨询</td></tr>"); 98 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:right\" encoding=\"Identity-H\">注:以上金额含17%的增值税,部分商品不含运费</td></tr>");
99 sb.Append("<tr><td></td></tr>");
100 sb.Append("<tr><td></td></tr>");
101 sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※</td></tr>");
102 sb.Append("</table>");
103 return sb;
104 }
105 #endregion
此数是获取商品列表代码就不贴了,同志们灵活替换吧
1 #region 获取购物车商品列表 zhy + List<商品列表类> GetProducts()
2 /// <summary>
3 /// 获取购物车商品列表
4 /// </summary>
5 /// <returns>List<us_ShoppingTrolley></returns>
6 public static List<商品列表类> GetProducts()
7 { return new List<商品列表类>(); }
ok,帮助到大家的话请给点个推荐吧,自己写的哦
来源:https://www.cnblogs.com/zhhying/p/4172399.html