基於ITextSharp插件在ASP.NET MVC中將圖表導出爲PDF

樣本:

在這個示例中,咱們使用的是微軟給咱們提供的數據庫,也就是家喻戶曉的Northwind數據庫。要下載Microsoft的免費樣本Northwind數據庫,您須要訪問如下URL。下載Northwind數據庫在頁面上,您將找到下載按鈕,如如下屏幕截圖所示。html

 

第2步:安裝Microsoft的免費樣本Northwind數據庫
一個安裝程序文件(.msi)將被下載。您能夠將其保存在桌面上,由於下載完成後您須要執行它。文件下載完成後,您能夠經過雙擊安裝文件或右鍵單擊而後單擊上下文菜單中的安裝選項來開始安裝。
安裝完成後,您能夠在如下位置檢查已安裝的數據庫文件,您將在安裝文件夾中找到Northwind數據庫。
步驟3:使用Management Studio將Northwind MDF文件附加到SQL Server數據庫
如今,你須要啓動SQL Server Management Studio中,而後用鼠標右鍵單擊數據庫文件夾在對象資源管理器。在上下文菜單中,單擊Attach選項,以下所示
此選項將在SQL Server中打開文件瀏覽器,您須要導航並選擇NORTHWIND.MDF文件並按OK按鈕。
這就是你將看到數據庫如今能夠在SQL Server中與其餘數據庫一塊兒使用。

若是由於數據庫版本問題或其餘緣由,附加不上,那你就用那幾個腳本文件。數據庫

安裝圖表:

 這玩膩更新的仍是比較快的,因此可用性仍是比較大的。如今咱們建立一個model(用於綁定圖標值)瀏覽器

public class OrderModel
    {
        public string ShipCity { get; set; } public int TotalOrders { get; set; } }

咱們如今確定是要去建立咱們的控制器了,定義以下。app

using System;
using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using WebApplication1.Models; using System.Web.Helpers; namespace WebApplication1.Controllers { public class PdfController : Controller { public static PdfContext pdfcontextoBJ = new PdfContext(); // GET: Pdf public ActionResult Index() { byte[] bytes = PopulateChart(); ViewBag.ChartImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length); return View(); } [HttpPost] public FileResult Export() { byte[] bytes = PopulateChart(); ViewBag.ChartImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length); using (MemoryStream stream = new System.IO.MemoryStream()) { //Initialize the PDF document object. using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f)) { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); //Add the Image file to the PDF document object. iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bytes); pdfDoc.Add(img); pdfDoc.Close(); //Download the PDF file. return File(stream.ToArray(), "application/pdf", "Chart.pdf"); } } } private static byte[] PopulateChart() { List<OrderModel> chartData = new List<OrderModel>(); //根據id統計的腳本 var objList = pdfcontextoBJ.Orders.GroupBy(u => u.ShipCity) .Select(s => new { TotalOrders = s.Key, count = s.Count() }).ToList().Take(5).ToList(); foreach (var item in objList) { chartData.Add(new OrderModel() { TotalOrders = item.count, ShipCity = item.TotalOrders }); } Chart chart = new Chart(width: 500, height: 500, theme: ChartTheme.Blue); chart.AddTitle("USA City Distribution"); chart.AddSeries("Default", chartType: "Pie", xValue: chartData, xField: "ShipCity", yValues: chartData, yFields: "TotalOrders"); return chart.GetBytes(format: "jpeg"); } } }

 在view中定義以下:spa

@{
    Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <img alt="Chart" src="@ViewBag.ChartImageUrl" style="height:300px; width:300px" /> <br /> @using (Html.BeginForm("Export", "Pdf", FormMethod.Post)) { <input type="submit" id="btnSubmit" value="Export" /> } </body> </html>

效果圖:

相關文章
相關標籤/搜索