我有一个 C# 控制台应用程序,并想向其中添加 PdfSharp 的一些功能。
我已经从Sourceforge下载了两个 zip 文件……程序集和源代码。
似乎有几种方法可以添加功能
.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);
}
}
}