PdfSharp – 入门
我有一个C#控制台应用程序,并希望将PdfSharp的一些function添加到它。
我从Sourceforge下载了两个zip文件……程序集和源代码。
似乎有几种方法可以添加function
- 解压缩程序集文件夹中对
.dll
文件的引用。 - 实际上将项目添加到我的解决方案。
我正在尝试使用后一种方法。
这就是我需要做的吗?
- 我将解压缩的源代码文件中的文件夹“PdfSharp”复制并粘贴到我的解决方案文件夹中。
- 在VS中,我访问了解决方案资源管理器,右键单击了解决方案并选择了
Add
>Existing Project...
- 然后我选择了以下……
我假设项目PdfSharp-ag.csproj
; PdfSharp-Hybrid.csproj
; PdfSharp-WPF.csproj
适用于其他一些应用程序? 我的意思是我的简单控制台应用程序是我需要的项目PdfSharp.csproj
?
一旦我按照上述步骤操作,我的解决方案如下所示:
…当在原始项目参考部分中引用PdfSharp时进行扩展:
然后,我使用以下代码测试了所有使用控制台应用程序正常工作:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; //<< additional reference required using PdfSharp; using PdfSharp.Pdf; using PdfSharp.Drawing; namespace PDFsharpSolutionQF { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument(); pdf.Info.Title = "My First PDF"; PdfPage pdfPage = pdf.AddPage(); XGraphics graph = XGraphics.FromPdfPage(pdfPage); XFont font = new XFont("Verdana",20,XFontStyle.Bold); graph.DrawString("This is my first PDF document",font,XBrushes.Black,new XRect(0,0,pdfPage.Width.Point,pdfPage.Height.Point),XStringFormats.Center); string pdfFilename = "firstpage.pdf"; pdf.Save(pdfFilename); //Process.Start(pdfFilename); } } }
您可以使用PdfSharp.csproj或PdfSharp-WPF.csproj – 前者使用GDI +进行图形操作,后者使用WPF函数。 -Hybrid是内部的,-ag是SilverLight(尚未完全正常运行)。
你能详细说明为什么要尝试将程序源添加到自己的应用程序中吗? 如果您只是想利用库的function,那么只需在项目中引用DLL然后使用它提供的API就会好得多。
我想到去源代码附近的唯一原因是a)如果你需要调试它或b)如果你打算以某种方式修改function。 您是尝试这样做,还是只是以通常的方式使用API?
在项目中包含源代码的一个优点是不需要分发DLL。 我个人使用DLL。 但我可以看到只分发EXE的优势。