close

1.前言:
PDFsharp是一個MIT license的開放原始碼的函式庫,可用來產生PDF格式的檔案,支援.NET各種語言的開發。

2.說明:
PDFsharp在首頁中說明包含兩種函式庫,一種是單純產生PDF檔案的PDFsharp,一種是產生PDF, XPS或RTF格式檔案的MigraDoc,本範例僅介紹PDFsharp,說明如何運用PDFsharp的函式庫簡單地產生PDF檔案。

PDFsharp的主要特點是:
•使用.NET的任何語言如C#或VB.NET動態地產生PDF檔案
•用容易瞭解的物件模型來組成文件
•完全以C#開發而成
•修改、合併或分割既有的PDF檔案
•透明感的影像
•可用GDI+或WPF產生圖形
•有豐富的範例文件可供參考

PDFsharp的說明可參考:
http://www.pdfsharp.net/wiki/MainPage.ashx

PDFsharp軟體下載網址:
http://sourceforge.net/projects/pdfsharp/

本範例使用版本為PDFSharp-MigraDocFoundation-1_32
軟體解壓縮後,將\PDFsharp-MigraDocFoundation-Assemblies-1_32\GDI+\PdfSharp.dll的DLL檔複製到自己專案的bin目錄下

加入參考: PdfSharp.dll
加入命名空間:

using PdfSharp; 
using PdfSharp.Drawing; 
using PdfSharp.Pdf; 
using PdfSharp.Pdf.IO;

程式碼:

//範例一: 簡單的Hello World文件
PdfDocument myDoc = new PdfDocument();
myDoc.Info.Title = "Tutorial-Hello World!";//文件標題
myDoc.Info.Author = "einboch";//文件作者
PdfPage myPage = myDoc.AddPage();//新增頁面
XGraphics g = XGraphics.FromPdfPage(myPage);
g.DrawString("Hello World!", new XFont("Courier", 20), XBrushes.Black, new XRect(0, 0, myPage.Width, myPage.Height), XStringFormats.Center);
myDoc.Save(@"d:\tmp\test.pdf");//產生PDF檔案
//清空資源
myPage = null;
myDoc = null;

//範例二: 將圖檔加入到PDF
PdfDocument myDoc = new PdfDocument();
myDoc.Info.Title = "Tutorial-Add image files";//文件標題
myDoc.Info.Author = "einboch";//文件作者
PdfPage myPage = myDoc.AddPage();//新增頁面
XImage img = XImage.FromFile(@"d:\tmp\pic5.png");
XGraphics g = XGraphics.FromPdfPage(myPage);//加入影像到頁面
g.DrawImage(img, new XRect(0, 0, myPage.Width, myPage.Height));
myDoc.Save(@"d:\tmp\test.pdf");//產生PDF檔案
//清空資源
myPage = null;
myDoc = null;

圖例:

pdfsharp  

arrow
arrow
    文章標籤
    C# PDFsharp PDF
    全站熱搜

    西夏普 發表在 痞客邦 留言(0) 人氣()