C# 生成pdf文件客戶端下載


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,幫助到大家的話請給點個推薦吧,自己寫的哦

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM