.NET平台開源項目速覽(16)C#寫PDF文件類庫PDF File Writer介紹
1年前,我在文章:這些.NET開源項目你知道嗎?.NET平台開源文檔與報表處理組件集合(三)中(第9個項目),給大家推薦了一個開源免費的PDF讀寫組件 PDFSharp,PDFSharp我2年前就看過,用過簡單的例子,不過代碼沒有寫成專門的文章。最近在查找資料的時候,又發現一款小巧的寫PDF文件的C#組件:PDF File Writer。該開源組件是在codeproject,還沒有托管到其他地方,所以花了點時間了解了一下,分享給大家。
.NET開源目錄:【目錄】本博客其他.NET開源項目文章目錄
本文原文地址:http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_PdfFileWriter.html
1.PDF File Writer基本介紹
1.1 支持的常用功能
PDF File Writer組件可以在.NET應用程序中直接創建PDF格式的文件。最低的開發環境是.NET 4.0 + VS 2013。我們來看看該組件支持的PDF的一些功能:
圖形:支持畫線、矩形、多邊形、貝塞爾曲線,前景和背景顏色,模式和陰影。
圖片:支持位圖圖像和矢量圖像
文本:支持行文本和列文本
條形碼:支持條形碼:Barcode 128, Barcode 39, Barcode interleaved 2 of 5等
二維碼:支持二維條碼
加密:支持AES-128加密算法
Web鏈接:支持Web超鏈接
書簽:支持文檔大綱
圖表:支持微軟的圖表,支持數據表格,支持聲音,視頻播放;
1.2 使用PDF File Writer創建PDF的步驟
使用PDF File Writer在程序中創建一個PDF文件的主要步驟如下:
Step 1: 創建PdfDocument文件對象
Step 2: 創建資源對象,如文字(PdfFont),圖像(PdfImage)等
Step 3: 創建文件頁對象PdfPage
Step 4: 創建內容對象PdfContents
Step 5: 在內容對象上添加文字,或者圖像等內容
重復3, 4 ,5 創建其他頁
Step 6: 使用PdfDocument對象的CreateFile方法創建PDF文
1.3 PDF File Writer創建的PDF文件效果預覽
看看使用PDF File Writer創建的PDF的效果,非常不錯。這也是我偶爾碰到非常震撼,拿過來分享的重要原因。
2.一個簡單的使用案例
我們根據官方提供的例子,可以快速入門,一起來看看基本代碼。
2.1 先創建基本對象
1
2
3
4
5
6
7
8
9
10
|
private
PdfFont ArialNormal;
private
PdfFont ArialBold;
private
PdfFont ArialItalic;
private
PdfFont ArialBoldItalic;
private
PdfFont TimesNormal;
private
PdfFont Comic;
private
PdfTilingPattern WaterMark;
private
PdfDocument Document;
private
PdfPage Page;
private
PdfContents Contents;
|
然后創建空白文檔
1
2
3
4
5
6
7
8
9
10
|
// Step 1:創建空文檔,文檔參數有類型,可以使用枚舉進行選擇,和返回的文件名稱
Document =
new
PdfDocument(PaperType.Letter,
false
, UnitOfMeasure.Inch, FileName);
//加密測試例子
//Document.SetEncryption(null, null, Permission.All & ~Permission.Print, EncryptionType.Aes128);
//創建PDF文件信息目錄
PdfInfo Info = PdfInfo.CreatePdfInfo(Document);
Info.Title(
"Article Example"
);
Info.Author(
"Uzi Granot Granotech Limited"
);
Info.Keywords(
"PDF, .NET, C#, Library, Document Creator"
);
Info.Subject(
"PDF File Writer C# Class Library (Version 1.14.1)"
);
|
2.2 創建字體等資源
1
2
3
4
5
6
7
8
9
10
|
//定義不同的字體類型,如下所示
String FontName1 =
"Arial"
;
String FontName2 =
"Times New Roman"
;
ArialNormal = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Regular,
true
);
ArialBold = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Bold,
true
);
ArialItalic = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Italic,
true
);
ArialBoldItalic = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Bold | FontStyle.Italic,
true
);
TimesNormal = PdfFont.CreatePdfFont(Document, FontName2, FontStyle.Regular,
true
);
Comic = PdfFont.CreatePdfFont(Document,
"Comic Sans MS"
, FontStyle.Bold,
true
);
|
2.3 創建文字示例
1
2
3
4
5
6
7
8
9
|
Contents.DrawText(Comic, 40.0, 4.25, 9.25, TextJustify.Center, 0.02, Color.FromArgb(128, 0, 255), Color.FromArgb(255, 0, 128),
"PDF FILE WRITER"
);
Contents.SaveGraphicsState();
Contents.SetColorNonStroking(Color.Purple);
Contents.DrawText(Comic, 30.0, 4.25, 8.75, TextJustify.Center,
"Example"
);
Contents.RestoreGraphicsState();
//Step 3:添加新頁面
Page =
new
PdfPage(Document);
//Step 4:添加內容到頁面
Contents =
new
PdfContents(Page);
|
2.4 繪制條形碼
1
2
3
4
5
6
7
8
9
|
Contents.SaveGraphicsState();
BarcodeEAN13 Barcode1 =
new
BarcodeEAN13(
"1234567890128"
);
Contents.DrawBarcode(1.3, 7.05, 0.012, 0.75, Barcode1, ArialNormal, 8.0);
PdfQRCode QRCode =
new
PdfQRCode(Document,
"http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version"
, ErrorCorrection.M);
Contents.DrawQRCode(QRCode, 6.0, 6.8, 1.2);
// 添加鏈接
Page.AddWebLink(6.0, 6.8, 7.2, 8.0,
"http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version"
);
//保存
Contents.RestoreGraphicsState();
|
2.5 繪制圖表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
Contents.SaveGraphicsState();
//創建MS Chart圖表
Chart PieChart = PdfChart.CreateChart(Document, 1.8, 1.5, 300.0);
PdfImageControl ImageControl =
new
PdfImageControl();
ImageControl.SaveAs = SaveImageAs.IndexedImage;
PdfChart PiePdfChart =
new
PdfChart(Document, PieChart, ImageControl);
PieChart.AntiAliasing = AntiAliasingStyles.None;
//設置顏色
PieChart.BackColor = Color.FromArgb(220, 220, 255);
PieChart.Palette = ChartColorPalette.BrightPastel;
//默認字體
Font DefaultFont = PiePdfChart.CreateFont(
"Verdana"
, FontStyle.Regular, 0.05, FontSizeUnit.UserUnit);
Font TitleFont = PiePdfChart.CreateFont(
"Verdana"
, FontStyle.Bold, 0.07, FontSizeUnit.UserUnit);
// 設置標題
Title Title1 =
new
Title(
"Pie Chart Example"
, Docking.Top, TitleFont, Color.Purple);
PieChart.Titles.Add(Title1);
//圖例
Legend Legend1 =
new
Legend();
PieChart.Legends.Add(Legend1);
Legend1.BackColor = Color.FromArgb(230, 230, 255);
Legend1.Docking = Docking.Bottom;
Legend1.Font = DefaultFont;
// 圖表區域
ChartArea ChartArea1 =
new
ChartArea();
PieChart.ChartAreas.Add(ChartArea1);
ChartArea1.BackColor = Color.FromArgb(255, 200, 255);
Series Series1 =
new
Series();
PieChart.Series.Add(Series1);
Series1.ChartType = SeriesChartType.Pie;
Series1.Font = DefaultFont;
Series1.IsValueShownAsLabel =
true
;
Series1.LabelFormat =
"{0} %"
;
Series1.Points.Add(22.0);
Series1.Points[0].LegendText =
"Apple"
;
Series1.Points.Add(27.0);
Series1.Points[1].LegendText =
"Banana"
;
Series1.Points.Add(33.0);
Series1.Points[2].LegendText =
"Orange"
;
Series1.Points.Add(18.0);
Series1.Points[3].LegendText =
"Grape"
;
Contents.DrawChart(PiePdfChart, 5.6, 5.0);
// 保存
Contents.RestoreGraphicsState();
|
2.6 生成PDF
1
2
3
4
5
6
|
// Step 6:創建PDF
Document.CreateFile();
//打開PDF文件
Process Proc =
new
Process();
Proc.StartInfo =
new
ProcessStartInfo(FileName);
Proc.Start();
|
3.資源
1.Codeproject文章連接:http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version
2.PDF File Writer DLL下載:PdfFileWriter_dll.zip
3.PDF File Writer 幫助文檔:PdfFileWriterCHM.rar
4.PDF File Writer源代碼與Demo:PdfFileWriter-Code.rar
注意:源代碼中的相關素材進行了精簡,否則文件比較大,長傳比較大。如果有需求可以去文章鏈接原文下載,或者單獨留下郵箱,我有空發送一下。