在一些訂閱號中無心中看到了這樣一篇文章,就記錄了一下:算法
1、PDF File Writer基本介紹測試
1.1 支持的經常使用功能字體
PDF File Writer組件能夠在.NET應用程序中直接建立PDF格式的文件。最低的開發環境是.NET 4.0 + VS 2013。咱們來看看該組件支持的PDF的一些功能:加密
圖形:支持畫線、矩形、多邊形、貝塞爾曲線,前景和背景顏色,模式和陰影。code
圖片:支持位圖圖像和矢量圖像orm
文本:支持行文本和列文本視頻
條形碼:支持條形碼:Barcode 128, Barcode 39, Barcode interleaved 2 of 5等對象
二維碼:支持二維條碼blog
加密:支持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.三、PDF File Writer建立的PDF文件效果預覽
看看使用PDF File Writer建立的PDF的效果,很是不錯。這也是我偶爾碰到很是震撼,拿過來分享的重要緣由。
2、一個簡單的使用案例
咱們根據官方提供的例子,能夠快速入門,一塊兒來看看基本代碼。
2.1 先建立基本對象
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;
而後建立空白文檔
// 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 建立字體等資源
//定義不一樣的字體類型,以下所示
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 建立文字示例
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 繪製條形碼
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 繪製圖表
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
// Step 6:建立PDF
Document.CreateFile();
//打開PDF文件
Process Proc = new Process();
Proc.StartInfo = new ProcessStartInfo(FileName);
Proc.Start();
3、資源
一、Codeproject文章鏈接:http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version
二、PDF File Writer DLL下載:PdfFileWriter_dll.zip
(http://files.cnblogs.com/files/asxinyu/PdfFileWriter_dll.zip)
三、PDF File Writer 幫助文檔:PdfFileWriterCHM.rar
(http://files.cnblogs.com/files/asxinyu/PdfFileWriterCHM.rar)
四、PDF File Writer源代碼與Demo:PdfFileWriter-Code.rar
(http://files.cnblogs.com/files/asxinyu/PdfFileWriter-Code.rar)
注意:源代碼中的相關素材進行了精簡,不然文件比較大,長傳比較大。