[問題] C# datatabe 匯出 PDF BY iTextSharp

看板C_Sharp (C#)作者 (Jeff)時間5年前 (2019/07/10 20:23), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
請問一下, 我要把DATATABLE 匯出成PDF ,匯出的資料頁數不固定, 如果希望每一頁都有標題,請問標題語法要怎麼打? 謝謝 //設定中文字體 string fontsfolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts); string chFontPath = fontsfolder + "\\KAIU.ttf";//windows內建的 SimHei字體(中易黑體) // string chFontPath = fontsfolder + "\\msjh.ttf";//windows內建的 SimHei字體(中易黑體) BaseFont chBaseFont = BaseFont.CreateFont(chFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // Phrase helloPhrase = new Phrase(); iTextSharp.text.Font twFont = new iTextSharp.text.Font(chBaseFont, 12); // iTextSharp.text.Font headerFont = new iTextSharp.text.Font(chBaseFont, 18); iTextSharp.text.Font headerFont = new iTextSharp.text.Font(chBaseFont, 16, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(51, 0, 153)); //Creating iTextSharp Table from the DataTable data PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount); pdfTable.DefaultCell.Padding = 3; //use 100% page width pdfTable.WidthPercentage = 100;//欄位間距 pdfTable.HorizontalAlignment = Element.ALIGN_MIDDLE;//置中 pdfTable.DefaultCell.BorderWidth = 20; //Adding Header row foreach (DataGridViewColumn column in dataGridView1.Columns) { //Asign Chinese Font to cell PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, twFont)); cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240); pdfTable.AddCell(cell); } //Adding DataRow foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value != null) { PdfPCell datacell = new PdfPCell(new Phrase(cell.Value.ToString(), twFont)); datacell.BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255); pdfTable.AddCell(datacell); } } } //Exporting to PDF string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//取得桌 面相對路徑; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } using (FileStream stream = new FileStream(folderPath+ "\\"+textBox1.Text +"復健科治療師服務量.pdf", FileMode.Create)) { Document pdfDoc = new Document(PageSize.A4, 28f, 28f, 30f, 30f); //大小方向 左邊界 右邊界 上邊界 下邊界 // Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);//橫向 PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(new Paragraph(" "+textBox1.Text+" 復健科治療師服務 量", headerFont));//繁中 Paragraph paragraph = new Paragraph(" "); pdfDoc.Add(paragraph); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.164.122.111 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1562761384.A.1AF.html
文章代碼(AID): #1T9TYe6l (C_Sharp)
文章代碼(AID): #1T9TYe6l (C_Sharp)