C#写PDF文件类库PDF File Writer介绍
作者:网络转载 发布时间:[ 2016/8/23 13:19:39 ] 推荐标签:测试开发技术 C#
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.资源
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
注意:源代码中的相关素材进行了精简,否则文件比较大,长传比较大。如果有需求可以去文章链接原文下载,或者单独留下邮箱,我有空发送一下。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南