PdfSharp - Getting started

╄→гoц情女王★ 提交于 2019-12-11 08:44:18

问题


I have a C# console application and would like to add some of the functionality of PdfSharp to it.

I've downloaded both zip files from Sourceforge ... both the assemblies and the source code.

There seem to be a couple of ways to add the functionality

  • a reference to the .dll file in the unzipped assemblies folder.
  • actually add the projects to my solution.

I'm trying to use the latter method.

Is this all I need to do?

  1. I copied and pasted the folder "PdfSharp" from the unzipped source code file into my solution folder.
  2. In VS I went to Solution Explorer right-clicked the solution and chose Add>Existing Project...
  3. Then I chose the following ...

I assume the projects PdfSharp-ag.csproj; PdfSharp-Hybrid.csproj; PdfSharp-WPF.csproj are for some other applications? what I mean is for my simple console application is all I need the project PdfSharp.csproj?

Once I've followed the above steps my Solution looks like the following:

...and when expanded as so with a reference to PdfSharp in the original Projects reference section:


I then tested all was working ok with a console app using the following code:

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);

    }
  }
}

回答1:


You can either use PdfSharp.csproj or PdfSharp-WPF.csproj - the former uses GDI+ for graphics operations, the latter uses WPF functions. -Hybrid is internal and -ag is for SilverLight (not fully functional yet).




回答2:


Could you elaborate on why you're trying to add the program source to your own application? If you just want to utilise the features of the library then you're much better off just referencing the DLL in your project and then using the API it provides.

The only reasons I could think of to go anywhere near the source code would be a) if you needed to debug into it or b) if you were intending to modify the functionality in some way. Are you trying to do that, or just utilise the API in the usual fashion?




回答3:


One advantage of including the source code in the project would be removing the need of distributing the DLL. I personally use the DLL. But I can see the advantage of only distributing an EXE.



来源:https://stackoverflow.com/questions/12391244/pdfsharp-getting-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!