C#利用iTextSharp將datatable數據轉化爲PDF文件

一、下載iTextSharp.dll文件字體

下載連接:https://pan.baidu.com/s/14o-pJ-U2yU8n0EyIn249qg
提取碼:tklu spa

二、PDF轉換方法.net

/// <summary>
        /// datatable轉PDF方法 /// </summary>
        /// <param name="Data">dataTable數據</param>
        /// <param name="PDFFile">PDF文件保存的路徑</param>
        /// <param name="FontSize">字體大小</param>
        /// <returns></returns>
        public bool ConvertDataTableToPDF(DataTable Data, string PDFFile, float FontSize) { //默認頁面大小
            Document document = new Document(); document.SetPageSize(PageSize.A2); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFile, FileMode.Create)); document.Open(); //設置字體 //BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); //Font font = new Font(bf, FontSize); //設置字體,支持中文
            BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL, new BaseColor(0, 0, 0)); 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) { table.AddCell(new Phrase(dc.ColumnName.ToString(), fontChinese)); } //插入數據
            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(), fontChinese)); } } document.Add(table); document.Close(); writer.Close(); return true; }

三、調用代碼3d

private void btnCreatePDF_Click(object sender, EventArgs e) { //獲取datatable數據
            DataTable tab = GetDataTableList(); //獲取保存路徑
            string savePath = "D://PDF//"; string saveName = "PDF文件.pdf"; CreateFileDirectory(savePath); //文件路徑:保存路徑+保存名稱
            string path = savePath + saveName; //調用轉化PDF的方法
            ConvertDataTableToPDF(tab,path,12); }

四、調用界面code

 

 

 五、生成結果orm

 

 六、所有demo代碼blog

using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DataTableToPDF { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCreatePDF_Click(object sender, EventArgs e) { //獲取datatable數據
            DataTable tab = GetDataTableList(); //獲取保存路徑
            string savePath = "D://PDF//"; string saveName = "PDF文件.pdf"; CreateFileDirectory(savePath); //文件路徑:保存路徑+保存名稱
            string path = savePath + saveName; //調用轉化PDF的方法
            ConvertDataTableToPDF(tab,path,12); } /// <summary>
        /// datatable轉PDF方法 /// </summary>
        /// <param name="Data">dataTable數據</param>
        /// <param name="PDFFile">PDF文件保存的路徑</param>
        /// <param name="FontSize">字體大小</param>
        /// <returns></returns>
        public bool ConvertDataTableToPDF(DataTable Data, string PDFFile, float FontSize) { //默認頁面大小
            Document document = new Document(); document.SetPageSize(PageSize.A2); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFile, FileMode.Create)); document.Open(); //設置字體 //BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); //Font font = new Font(bf, FontSize); //設置字體,支持中文
            BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL, new BaseColor(0, 0, 0)); 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) { table.AddCell(new Phrase(dc.ColumnName.ToString(), fontChinese)); } //插入數據
            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(), fontChinese)); } } document.Add(table); document.Close(); writer.Close(); return true; } /// <summary>
        /// 建立文件夾 /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string CreateFileDirectory(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } return path; } /// <summary>
        /// 建立默認dataTable數據 /// </summary>
        /// <returns></returns>
        public DataTable GetDataTableList() { DataTable dt = new DataTable(); dt.Columns.Add("學生編號", typeof(string));//添加列 
            dt.Columns.Add("學生名稱", typeof(string));//添加列 
            dt.Columns.Add("家庭住址", typeof(string));//添加列 //循環添加行的數據
            for (var i=0; i < 10; i++) { DataRow dtRow = dt.NewRow(); dtRow["學生編號"] = "S10"+(i+1); dtRow["學生名稱"] = "學生" + (i + 1); dtRow["家庭住址"] = "地址" + (i + 1); dt.Rows.Add(dtRow); } return dt; } } }

七、demo源碼下載源碼

下載地址:https://pan.baidu.com/s/1tTG4lzzDvC8vOvEjng8N5Qstring

提取碼:c8mcit

PS

參考網址:https://blog.csdn.net/qq_40253245/article/details/87694147

相關文章
相關標籤/搜索