close
1.前言:
iTextSharp是一種Affero GNU Public License的開放原始碼函式庫,提供C#程式開發人員產生PDF檔案,相當於Java版本的iText。
2.說明:
iTextSharp是最常被使用產生PDF檔案的函式庫,特點有:
•產生PDF格式檔案
•PDF檔案操作(浮水印、合併/分割文件等等)
•填寫表單
•XML功能
•數位簽章
iTextSharp的前身iText說明可參考:
http://en.wikipedia.org/wiki/IText
iTextSharp軟體下載網址:
http://sourceforge.net/projects/itextsharp/
本範例使用版本為itextsharp-5.4.4
軟體解壓縮後,將\itextsharp-all-5.4.4\itextsharp-dll-core\itextsharp.dll的DLL檔複製到自己專案的bin目錄下
加入參考: itextsharp.dll
加入命名空間:
using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf;
程式碼:
//範例一: 簡單的Hello World文件 Document myDoc = new Document(); try { FileStream fs = new FileStream(@"d:\tmp\test.pdf", FileMode.Create); PdfWriter.GetInstance(myDoc, fs); Paragraph p = new Paragraph("Hello World!", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 20f)); myDoc.Open(); myDoc.Add(p);//加入文章段落 myDoc.AddTitle("Tutorial-Hello World!");//文件標題 myDoc.AddAuthor("einboch");//文件作者 } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if(myDoc.IsOpen()) myDoc.Close(); } //範例二: 將圖檔加入到PDF Document myDoc = new Document(); try { FileStream fs = new FileStream(@"d:\tmp\test.pdf", FileMode.Create); PdfWriter.GetInstance(myDoc, fs); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(@"d:\tmp\pic7.png"); myDoc.Open(); myDoc.Add(image);//加入影像 myDoc.AddTitle("Tutorial-Add image files");//文件標題 myDoc.AddAuthor("einboch");//文件作者 } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (myDoc.IsOpen()) myDoc.Close(); }
圖例:
文章標籤
全站熱搜