前言 PDF 不論甚麼場合都需要使用,包含公司都會需要有PDF轉出轉入的工具,這邊使用PDF轉檔進行作業。
iTextSharp 本函式庫原名是iText,主要是支援Java程式語言。之後針對Microsoft .NET C # 做了一個版本。
一、 初始設定 製作方式需要新增 Document 內部內容都會以Document 才新增、寫入。 以下就是初始化設定。
1 2 3 4 5 6 7 8 9 10 11 12 Document doc = new  Document(PageSize.A4, 20 , 20 , 50 , 50 );  MemoryStream ms = new  MemoryStream(); PdfWriter.GetInstance(doc, ms).CloseStream = false ; doc.Open(); doc.Close(); 
 
如果少掉 CloseStream 會遇到 cannot access a closed Stream問題,所以必須要小心。
二、 設定文字內容 一般輸出文字會需要有標題、內容,這便需要使用 Paragraph 儲存文字。以下使用方式。
1 2 3 4 5 6 7 8 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); BaseFont bfChinese = BaseFont.CreateFont(_hostEnvironment.WebRootPath + "\\font\\KAIU.TTF" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED); BaseFont chBaseFont = BaseFont.CreateFont(_hostEnvironment.WebRootPath + "\\font\\KAIU.TTF" , BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font ChFont = new  Font(bfChinese, 12 ); 
 
iTextSharp 提供兩個類別   Paragraph 、 Chunk,如果要單獨設定Font內容也可以。
2-1 Font 1 2 3 4 5 Font CbFont = new  Font(bfChinese, 12 ,1 ); Font CbFont = new  Font(bfChinese, 12 ,2 ); 
 
2-2 Paragraph 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Paragraph title = new  Paragraph("This Title Area" ,ChFont); title.Alignment = Element.ALIGN_CENTER; title.Leading = 10 ; title.SpacingAfter =50 ; title.SpacingBefore =50 ; title.IndentationLeft=50 ; title.IndentationRight=50 ; title.FirstLineIndent =50 ; 
 
2-3 Chunk 1 2 3 Chunk chunk = new  Chunk("測試底線文字" , CbFont); chunk.SetUnderline(0.2f , -2f ); doc.Add(chunk); 
 
2-4 Error : ‘windows-1252’ is not a supported encoding name. [System.Text.Encoding.CodePages v5.0.0 ] : https://www.nuget.org/packages/System.Text.Encoding.CodePages/5.0.0 
三、 設定表格 創建表格是常常需要再Pdf 去設定,如何去使用這個必須要先知道你要幾個table欄位。
3-1 一般表格 下方用很簡單範例執行。
1 2 3 4 5 6 7 8 9 10 11 12 PdfPTable pt = new  PdfPTable(3 ); pt.AddCell(new  PdfPCell(new  Phrase($" 第三欄 " ,ChFont)){ Colspan=3  }); for (int  i = 1  ; i <= 3 ;++i){    for (int  j = 1  ; j <= 3 ;++j){         Phrase text =  new  Phrase($"line{i} ,cell{j} " );         PdfPCell cell= new  PdfPCell(text);         pt.AddCell(cell);     } } doc.Add(new  Paragraph(){pt}); 
 
使用概念:
Phrase : 儲存文字,如果有中文字眼記得要加入剛才的Font。 
PdfPCell : 這邊是儲存cell方式(單欄)。後面可以細項設定,目前不用設定時候就會比較單存一點。 
AddCell : 加入單欄內容。 
 
3-2 客製表格 如果要自定義方式,可以參考下方。設定table 一定會用到
Colspan : 行合併  
Rowspan : 列合併 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 PdfPTable pt = new  PdfPTable(3 ); pt.AddCell(new  PdfPCell(new  Phrase($" 所有第三欄合併 " ,ChFont)){ Colspan=3  }); for (int  i = 1  ; i <= 3 ;++i){    for (int  j = 1  ; j <= 3 ;++j){         Phrase text =  new  Phrase($"line{i} ,cell{j} " );         PdfPCell cell= new  PdfPCell(text);                  if (i != 1  && j == 3  ) { continue ;}                  if (j == 3  ){             pt.AddCell(new  PdfPCell(new  Phrase($"cell{j} " )){ Rowspan=3  });             continue ;         }                  pt.AddCell(cell);     } } doc.Add(new  Paragraph(){pt}); 
 
3-3 使用建議 如果第一次使用可以使用下方方式,一行一行創建可以知道使用方式。按照上方範例呈現方式會像下方一樣,概念是合併不能出現下一行,不然就會有多個 Rows畫面。
1 2 3 4 5 6 7 8 9 pt.AddCell(new  PdfPCell(new  Phrase($"line 1,cell 1" ))); pt.AddCell(new  PdfPCell(new  Phrase($"line 1,cell 2" ))); pt.AddCell(new  PdfPCell(new  Phrase($"line 1,cell 3" )){ Rowspan=3  }); pt.AddCell(new  PdfPCell(new  Phrase($"line 2,cell 1" ))); pt.AddCell(new  PdfPCell(new  Phrase($"line 2,cell 2" ))); pt.AddCell(new  PdfPCell(new  Phrase($"line 3,cell 1" ))); pt.AddCell(new  PdfPCell(new  Phrase($"line 3,cell 2" ))); 
 
四、 預覽效果 下方為預覽效果,僅供參閱。
1 2 3 4 5 6 7 8 9 10 11 12 13 public  IActionResult Preview () {  Document doc = new  Document(PageSize.A4);   MemoryStream ms = new  MemoryStream();   PdfWriter.GetInstance(doc, ms).CloseStream = false ;   doc.Open();   ...   ...   doc.Close();              ms.Seek(0 , SeekOrigin.Begin);   return  new  FileStreamResult(ms, "application/pdf" ); } 
 
 
結論 使用 iTextSharp 就到這邊,目前遇到只有這幾些,使用 tables 還沒搞清楚先後順序,可能會卡非常久。