RSS

Create PDF Files on the fly with C# and PDFSharp

Thu, Jul 24, 2008

Libraries, Visual Studio, c#

Recently one of my colleague asked me is there anyway to programmatically merge huge number of PDF  files in to a single file, without using Adobe Acrobat libraries, well the answer is yeah. and there are a lot of off the shelve and open source libraries available for this. the best one I found is the PDFSharp, and its totally FREE.

Here is a little introduction about PDFSharp:

  • PDFsharp is the Open Source library that easily creates PDF documents from any .NET language.
    The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer.
  • It can use either GDI+ or WPF.
  • It includes support for Unicode in PDF files.
  • It also includes MigraDoc Lite which brings you all the high-level functionality not included in PDFsharp.

Using PDFSharp is very simple. the samples in the official download file are more than enough to learn the functionality of PDFSharp.here is a little piece of code I used to merge A collection of PDFs in a folder to a single PDF file.

   1:  PdfDocument inputDocument=
   2:                  PdfReader.Open(file.FullName,PdfDocumentOpenMode.Import);
   3:  int count = inputDocument.PageCount;
   4:    for (int idx = 0; idx < count; idx++)
   5:     {
   6:         // Get the page from the external document...
   7:         PdfPage page = inputDocument.Pages[idx];
   8:         // ...and add it to the output document.
   9:         outputDocument.AddPage(page);
  10:     }
  11:  
  12:  outputDocument.Save(filename);

if you want to download the full sample I created download it  here.

if you want to download pdfSharp Library visit the site below:

http://www.pdfsharp.com/PDFsharp

Popularity: 85% [?]

This post was written by:

Aneef Fashir - who has written 32 posts on Aneef.Net.

Software Engineer, Sri Lanka.

Contact the author

6 Comments For This Post

  1. Jeff Says:

    Awesome! I was messing with iTextSharp, Gio’s implementation at Code Project and Adobe’s conflaguration and nothing was working for the files I was using. This one did though… THANK YOU!

  2. superjason Says:

    I’ve used iTextSharp and it’s a very mature product. I would be concerned that this one is missing some critical feature. Thanks for bringing it to my attention, I’ll have to try using it to see if is better and/or easier.

  3. pascon Says:

    As an alternative there is simple program Pdftk – PDF toolkit, that do merge of existing documents and even more. http://www.accesspdf.com/pdftk/

  4. James Says:

    Good one… But I have prefered to use NFop to create PDF docs from C#. The following blog gives step by step instructions..

    http://www.codelathe.com/blog/index.php/2009/02/28/generate-pdf-from-cnet/

  5. irfan Says:

    hello
    what if i do not have adobe reader and writer install on that system do this work

    Regards

  6. Aneef Fashir Says:

    @Irfan,

    yeah you dont need acrobat, it will still work :)

2 Trackbacks For This Post

  1. countnazgul.com » Blog Archive » Diigo bookmarks 07/25/2008 (p.m.) Says:

    [...] Create PDF Files on the fly with C# and PDFSharp [...]

  2. Always 英文技术文章参照( 十一 ){ UpdateTime:2008-7-27; } My article in the cnblogs - cnblogs.com Says:

    [...] 14.使用C#创建PDF文件。 [...]

Leave a Reply

Follow me