PdfSharp - Getting started
Asked Answered
I

3

1

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 ...

enter image description here

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:

enter image description here

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

enter image description here


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

    }
  }
}
Immortality answered 12/9, 2012 at 15:7 Comment(0)
B
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).

Berber answered 12/9, 2012 at 15:33 Comment(3)
(I'm a relative c# newcomer) The project I'm developing is a Console Application - so I assume there's no real need to use the WPF version? GDI+ sounds a little scary...like I said it's just a C# Console App?Immortality
PDFsharp has to use operating system functions to measure strings, load images &c. GDI+ is the original implementation, WPF came later. Both should work with console applications (I think I'd use the "normal" GDI+ build to get started).Berber
I've added it and tried some of the samples from your website - all working so far. Specicifically tomorrow I'll be using it to add a password to an existing pdf before emailing...looks like you have stuff in place for this. As a newbie it's really nice having some quality source code to nose through aswell. Thanks.Immortality
W
1

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?

Wedged answered 12/9, 2012 at 15:9 Comment(4)
By referencing the projects you can jump into the commented source code using F12 (or whatever key you assigned for it). This will work if you refer to the DLLs, but then you will see the information coming from the DLL (no comments, no source code).Berber
Yeah I realise that, but it's not generally something a developer would need to do (IMO at least). To give an example, I use NHibernate commonly on projects, and I very very seldom have any desire to look at the source code!Wedged
@HenryWilson - if you read their website it is one of the recommended methods; all I need to know is if I'm following their instructions correctlyImmortality
@Henry Wilson: there are different ways to use PDFsharp. There also is a NuGet package that will get the DLLs for you, you can download the DLLs from SourceForge or CodePlex (zipped) or get the full source code. Intellisense works fine when you use the source code.Berber
B
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).

Berber answered 12/9, 2012 at 15:33 Comment(3)
(I'm a relative c# newcomer) The project I'm developing is a Console Application - so I assume there's no real need to use the WPF version? GDI+ sounds a little scary...like I said it's just a C# Console App?Immortality
PDFsharp has to use operating system functions to measure strings, load images &c. GDI+ is the original implementation, WPF came later. Both should work with console applications (I think I'd use the "normal" GDI+ build to get started).Berber
I've added it and tried some of the samples from your website - all working so far. Specicifically tomorrow I'll be using it to add a password to an existing pdf before emailing...looks like you have stuff in place for this. As a newbie it's really nice having some quality source code to nose through aswell. Thanks.Immortality
W
1

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.

Wolcott answered 31/1, 2015 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.