首先:添加GET包:
iTextSharp
其次:添加命名空间:
using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf;
之后:操作sql sever 数据库中的表,创建DataTable表格,DataTable中字段类型和数据库的名称的类型一致,把表中的内容循环到DataTable表格里
var query = db.PlanInfo.Where(a => a.Isdeleted == 0); if (!IsAdmin()) { query = query.Where(a => a.Dept == _User.DepartMentId); } if (this.txtUserName.Text.Trim() != "") { query = query.Where(a => a.Title.Contains(this.txtUserName.Text.Trim())); } query = query.OrderBy(a => a.Number); DataTable dt = new DataTable(); dt.Columns.Add("Title", typeof(string)); dt.Columns.Add("BeginTime", typeof(DateTime)); dt.Columns.Add("EndTime", typeof(DateTime)); dt.Columns.Add("stateName", typeof(string)); dt.Columns.Add("ActiTypeName", typeof(string)); dt.Columns.Add("SiteName", typeof(string)); foreach (PlanInfo item in query) { DataRow row = dt.NewRow(); row["Title"] = item.Title; row["BeginTime"] = item.BeginTime; row["EndTime"] = item.EndTime; row["stateName"] = item.stateName; row["ActiTypeName"] = item.ActiTypeName; row["SiteName"] = item.SiteName; dt.Rows.Add(row); } string path = Server.MapPath("~/Content/Templet/") + "计划审批模板.pdf"; ConvertDataTableToPDF(dt, path, 16); using (FileStream fs = new FileStream(path, FileMode.Open)) { byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("计划审批模板.pdf", System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }
之后:绘制PDF格式的表
public bool ConvertDataTableToPDF(DataTable Data, string PDFFile, float FontSize) { //定义页面的大小 //Rectangle pageSize = new Rectangle(PoundsToPixels(2000), PoundsToPixels(3000)); //Document document = new Document(pageSize); //默认页面大小 Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFile, FileMode.Create)); document.Open(); //设置字体 string path = Server.MapPath("~/typeface/") + "simsun.ttc"; BaseFont bf = BaseFont.CreateFont("" + path + ",1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bf, 12, iTextSharp.text.Font.NORMAL, new BaseColor(0, 0, 0)); //BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); Font font = new Font(bf, FontSize); PdfPTable table = new PdfPTable(Data.Columns.Count); table.WidthPercentage = 100; // percentage table.DefaultCell.Padding = 1; table.DefaultCell.BorderWidth = 1; table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; //将datatable表头转换成PDFTable的表头 foreach (DataColumn dc in Data.Columns) { if (dc.ColumnName.ToString() == "Title") { table.AddCell(new Phrase("标题", font)); } if (dc.ColumnName.ToString() == "BeginTime") { table.AddCell(new Phrase("开始时间", font)); } if (dc.ColumnName.ToString() == "EndTime") { table.AddCell(new Phrase("结束时间", font)); } if (dc.ColumnName.ToString() == "stateName") { table.AddCell(new Phrase("状态", font)); } if (dc.ColumnName.ToString() == "ActiTypeName") { table.AddCell(new Phrase("站所名称", font)); } if (dc.ColumnName.ToString() == "SiteName") { table.AddCell(new Phrase("计划类型", font)); } } //插入数据 for (int i = 0; i < Data.Rows.Count; i++) { for (int j = 0; j < Data.Columns.Count; j++) { table.AddCell(new Phrase(Data.Rows[i][j].ToString(), font)); } } document.Add(table); document.Close(); writer.Close(); return true; }