- Nuget 下載安裝itextsharp
只需安裝在Service就可以了
2.於Service 中
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
3.設定值
var doc1 = new Document(PageSize.A4, 50, 50, 20, 20); //設定紙張格式邊界
string vfilePath = ConfigurationSettings.AppSettings["Path_PDF"].ToString() + @"\" + vPrt.FlowNo.ToString();
System.IO.Directory.CreateDirectory(vfilePath); //文件儲存位置
string vPath = vfilePath + @"\" + fileName;
PdfWriter vpdfWriter = PdfWriter.GetInstance(doc1, new FileStream(vPath, FileMode.Create));
string vFont = ConfigurationSettings.AppSettings["Font_PDF"].ToString(); //字形檔位置
BaseFont bfChinese = BaseFont.CreateFont(vFont, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 建立字形
Font chfont = new Font(bfChinese, 12);
doc1.Open();
4.表格
a.設定Table
PdfPTable iTB = new PdfPTable(new float[] { 1,1,1}); //按比例等分,或固定寬度 PdfPTable iTB = new PdfPTable(new float[] { 100f,200f,22f});
iTB.TotalWidth = 520f;
iTB.LockedWidth = true;
iTB.DefaultCell.Border = Rectangle.NO_BORDER;
b.填入PdfPCell 在iTextSharp 的table 中每個格子都是一個PdfPCell ,由上到下 從左至右依序填入
|
|
|
|
||
|
|
|
|
|
|
|
|
PdfPCell Items = new PdfPCell(new Phrase("1", chfont08));
Items.Border = Rectangle.NO_BORDER;
Items.HorizontalAlignment = Element.ALIGN_CENTER;
Items.Rowspan= 3;
iTB.AddCell(Items);
Items = new PdfPCell(new Phrase("2", chfont08));
Items.Border = Rectangle.NO_BORDER;
Items.HorizontalAlignment = Element.ALIGN_CENTER;
iTB.AddCell(Items);
Items = new PdfPCell(new Phrase("3", chfont08));
Items.Border = Rectangle.NO_BORDER;
Items.HorizontalAlignment = Element.ALIGN_CENTER;
iTB.AddCell(Items);
Items = new PdfPCell(new Phrase("4", chfont08));
Items.Border = Rectangle.NO_BORDER;
Items.HorizontalAlignment = Element.ALIGN_CENTER;
Items.Colspan = 2;
iTB.AddCell(Items);
....................
doc1.Add(iTB); //文件中加入Table
doc1.Close();
vpdfWriter.Close();