PDFSharp组件是.Net下的一个开源类库,可以轻松的在.Net语言中创建PDF文档,在屏幕中显示以及输出到打印机,可以修改,合并拆分已经存在的PDF文件。
总体来说,PDFSharp 组件主要特点有:
1.可以使用任何.NET编程语言动态创建PDF文档
2.很容易使用对象模型来构建文档
3.全部用C#重写设计和编写代码
4.可以生成PDF文件和显示在窗体或者打印,都使用同一源文件
5.可以修改、合并或者分割PDF文件
6.可以控制图片的透明度,嵌入了字体
官方下载地址:
https:pdfsharp.codeplex.com/ 或者 https://sourceforge.net/projects/pdfsharp/
当你有需要通过程序动态生成PDF,或者在.Net平台对现有的PDF进行操作,比如修改,显示,输出打印等时,PDFSharp组件能够发挥很大作用。
PDFSharp组件拥有很多对PDF文档操作的功能,有兴趣可以在官方给出的samples中逐一根据demo进行研究,文章接下来不会详细介绍PDFSharp的具体每个功能特点,但我会写下一些我个人在学习中觉得需要注意的几点以及在最后会给出一个画表格的例子(仅供参考)
一:中文乱码问题
#region PDFsharp - A .NET library for processing PDF // // Authors: // PDFsharp Team (mailto:PDFsharpSupport@pdfsharp.de) // // Copyright (c) 2005-2009 empira Software GmbH, Cologne (Germany) // // http://www.pdfsharp.com // http://sourceforge.net/projects/pdfsharp // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using System; using System.Diagnostics; using System.IO; using PdfSharp; using PdfSharp.Drawing; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; namespace HelloWorld { /// <summary> /// This sample is the obligatory Hello World program. /// 演示新建PDF并保存在指定路径的helloworld PDF文件 /// </summary> class Program { static void Main() { // Create a new PDF document PdfDocument document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; // Create an empty page PdfPage page = document.AddPage(); // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(page); //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); // Create a font //XFont font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic); System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection(); string strFontPath = @"C:/Windows/Fonts/msyh.ttf";//字体设置为微软雅黑 pfcFonts.AddFontFile(strFontPath); XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); XFont font = new XFont(pfcFonts.Families[0], 15, XFontStyle.Regular, options); //PdfWriter.Wirte()里的断言注释掉 // Draw the text gfx.DrawString("你好, 世界!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); // Save the document... //const string filename = "PDF\\HelloWorld_tempfile2.pdf"; string _path = YZRHelper.RPath.GetPath(System.AppDomain.CurrentDomain.BaseDirectory,2)+"\\PDF\\YZR.pdf"; document.Save(_path); // ...and start a viewer. Process.Start(_path); } } }