天天看点

.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍 1.PDF File Writer基本介绍 2.一个简单的使用案例3.资源

<b>阅读目录</b>

<a href="http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_PdfFileWriter.html#_label0">1.PDF File Writer基本介绍</a>

<a href="http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_PdfFileWriter.html#_label1">2.一个简单的使用案例</a>

<a href="http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_PdfFileWriter.html#_label2">3.资源</a>

  图形:支持画线、矩形、多边形、贝塞尔曲线,前景和背景颜色,模式和阴影。

  图片:支持位图图像和矢量图像

    文本:支持行文本和列文本

  条形码:支持条形码:Barcode 128, Barcode 39, Barcode interleaved 2 of 5等

  二维码:支持二维条码

       加密:支持AES-128加密算法

       Web链接:支持Web超链接

       书签:支持文档大纲

       图表:支持微软的图表,支持数据表格,支持声音,视频播放;   

使用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文

    看看使用PDF File Writer创建的PDF的效果,非常不错。这也是我偶尔碰到非常震撼,拿过来分享的重要原因。

.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍 1.PDF File Writer基本介绍 2.一个简单的使用案例3.资源
.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍 1.PDF File Writer基本介绍 2.一个简单的使用案例3.资源

我们根据官方提供的例子,可以快速入门,一起来看看基本代码。

1

2

3

4

5

6

7

8

9

10

<code>private</code> <code>PdfFont            ArialNormal;</code>

<code>private</code> <code>PdfFont            ArialBold;</code>

<code>private</code> <code>PdfFont            ArialItalic;</code>

<code>private</code> <code>PdfFont            ArialBoldItalic;</code>

<code>private</code> <code>PdfFont            TimesNormal;</code>

<code>private</code> <code>PdfFont            Comic;</code>

<code>private</code> <code>PdfTilingPattern WaterMark;</code>

<code>private</code> <code>PdfDocument        Document;</code>

<code>private</code> <code>PdfPage            Page;</code>

<code>private</code> <code>PdfContents        Contents;</code>

    然后创建空白文档

<code>// Step 1:创建空文档,文档参数有类型,可以使用枚举进行选择,和返回的文件名称</code>

<code>Document = </code><code>new</code> <code>PdfDocument(PaperType.Letter, </code><code>false</code><code>, UnitOfMeasure.Inch, FileName);</code>

<code>//加密测试例子</code>

<code>//Document.SetEncryption(null, null, Permission.All &amp; ~Permission.Print, EncryptionType.Aes128);</code>

<code>//创建PDF文件信息目录</code>

<code>PdfInfo Info = PdfInfo.CreatePdfInfo(Document);</code>

<code>Info.Title(</code><code>"Article Example"</code><code>);</code>

<code>Info.Author(</code><code>"Uzi Granot Granotech Limited"</code><code>);</code>

<code>Info.Keywords(</code><code>"PDF, .NET, C#, Library, Document Creator"</code><code>);</code>

<code>Info.Subject(</code><code>"PDF File Writer C# Class Library (Version 1.14.1)"</code><code>);</code>

<code>//定义不同的字体类型,如下所示</code>

<code>String FontName1 = </code><code>"Arial"</code><code>;</code>

<code>String FontName2 = </code><code>"Times New Roman"</code><code>;</code>

<code>ArialNormal = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Regular, </code><code>true</code><code>);</code>

<code>ArialBold = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Bold, </code><code>true</code><code>);</code>

<code>ArialItalic = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Italic, </code><code>true</code><code>);</code>

<code>ArialBoldItalic = PdfFont.CreatePdfFont(Document, FontName1, FontStyle.Bold | FontStyle.Italic, </code><code>true</code><code>);</code>

<code>TimesNormal = PdfFont.CreatePdfFont(Document, FontName2, FontStyle.Regular, </code><code>true</code><code>);</code>

<code>Comic = PdfFont.CreatePdfFont(Document, </code><code>"Comic Sans MS"</code><code>, FontStyle.Bold, </code><code>true</code><code>);</code>

<code>Contents.DrawText(Comic, 40.0, 4.25, 9.25, TextJustify.Center, 0.02, Color.FromArgb(128, 0, 255), Color.FromArgb(255, 0, 128), </code><code>"PDF FILE WRITER"</code><code>);</code>

<code>Contents.SaveGraphicsState();</code>

<code>Contents.SetColorNonStroking(Color.Purple);</code>

<code>Contents.DrawText(Comic, 30.0, 4.25, 8.75, TextJustify.Center, </code><code>"Example"</code><code>);</code>

<code>Contents.RestoreGraphicsState();</code>

<code>//Step 3:添加新页面</code>

<code>Page = </code><code>new</code> <code>PdfPage(Document);</code>

<code>//Step 4:添加内容到页面</code>

<code>Contents = </code><code>new</code> <code>PdfContents(Page);</code>

<code>BarcodeEAN13 Barcode1 = </code><code>new</code> <code>BarcodeEAN13(</code><code>"1234567890128"</code><code>);</code>

<code>Contents.DrawBarcode(1.3, 7.05, 0.012, 0.75, Barcode1, ArialNormal, 8.0);</code>

<code>PdfQRCode QRCode = </code><code>new</code> <code>PdfQRCode(Document, </code><code>"http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version"</code><code>, ErrorCorrection.M);</code>

<code>Contents.DrawQRCode(QRCode, 6.0, 6.8, 1.2);</code>

<code>// 添加链接</code>

<code>Page.AddWebLink(6.0, 6.8, 7.2, 8.0, </code><code>"http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version"</code><code>);</code>

<code>//保存</code>

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

<code>//创建MS Chart图表</code>

<code>Chart PieChart = PdfChart.CreateChart(Document, 1.8, 1.5, 300.0);</code>

<code>PdfImageControl ImageControl = </code><code>new</code> <code>PdfImageControl();</code>

<code>ImageControl.SaveAs = SaveImageAs.IndexedImage;</code>

<code>PdfChart PiePdfChart = </code><code>new</code> <code>PdfChart(Document, PieChart, ImageControl);</code>

<code>PieChart.AntiAliasing = AntiAliasingStyles.None; </code>

<code>//设置颜色</code>

<code>PieChart.BackColor = Color.FromArgb(220, 220, 255);</code>

<code>PieChart.Palette = ChartColorPalette.BrightPastel;</code>

<code>//默认字体</code>

<code>Font DefaultFont = PiePdfChart.CreateFont(</code><code>"Verdana"</code><code>, FontStyle.Regular, 0.05, FontSizeUnit.UserUnit);</code>

<code>Font TitleFont = PiePdfChart.CreateFont(</code><code>"Verdana"</code><code>, FontStyle.Bold, 0.07, FontSizeUnit.UserUnit);</code>

<code>// 设置标题</code>

<code>Title Title1 = </code><code>new</code> <code>Title(</code><code>"Pie Chart Example"</code><code>, Docking.Top, TitleFont, Color.Purple);</code>

<code>PieChart.Titles.Add(Title1);</code>

<code>//图例</code>

<code>Legend Legend1 = </code><code>new</code> <code>Legend();</code>

<code>PieChart.Legends.Add(Legend1);</code>

<code>Legend1.BackColor = Color.FromArgb(230, 230, 255);</code>

<code>Legend1.Docking = Docking.Bottom;</code>

<code>Legend1.Font = DefaultFont;</code>

<code>// 图表区域</code>

<code>ChartArea ChartArea1 = </code><code>new</code> <code>ChartArea();</code>

<code>PieChart.ChartAreas.Add(ChartArea1);</code>

<code>ChartArea1.BackColor = Color.FromArgb(255, 200, 255);</code>

<code>Series Series1 = </code><code>new</code> <code>Series();</code>

<code>PieChart.Series.Add(Series1);</code>

<code>Series1.ChartType = SeriesChartType.Pie;</code>

<code>Series1.Font = DefaultFont;</code>

<code>Series1.IsValueShownAsLabel = </code><code>true</code><code>;</code>

<code>Series1.LabelFormat = </code><code>"{0} %"</code><code>;</code>

<code>Series1.Points.Add(22.0);</code>

<code>Series1.Points[0].LegendText = </code><code>"Apple"</code><code>;</code>

<code>Series1.Points.Add(27.0);</code>

<code>Series1.Points[1].LegendText = </code><code>"Banana"</code><code>;</code>

<code>Series1.Points.Add(33.0);</code>

<code>Series1.Points[2].LegendText = </code><code>"Orange"</code><code>;</code>

<code>Series1.Points.Add(18.0);</code>

<code>Series1.Points[3].LegendText = </code><code>"Grape"</code><code>;</code>

<code>Contents.DrawChart(PiePdfChart, 5.6, 5.0);</code>

<code>// 保存</code>

<code>// Step 6:创建PDF</code>

<code>Document.CreateFile();</code>

<code>//打开PDF文件</code>

<code>Process Proc = </code><code>new</code> <code>Process();</code>

<code>Proc.StartInfo = </code><code>new</code> <code>ProcessStartInfo(FileName);</code>

<code>Proc.Start();</code>

注意:源代码中的相关素材进行了精简,否则文件比较大,长传比较大。如果有需求可以去文章链接原文下载,或者单独留下邮箱,我有空发送一下。

本文转自叶小钗 h数据之巅博客园博客,原文链接:http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_PdfFileWriter.html,如需转载请自行联系原作者