Main menu

Create PDF Files on the fly with C# and PDFSharp

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

Aneef Fashir (39 Posts)

Software Architect @ Assette, Sri Lanka


  • Jeff

    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!

  • Jeff

    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!

  • http://www.ytechie.com/ superjason

    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.

  • http://www.ytechie.com superjason

    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.

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

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

  • pascon

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

  • pascon

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

  • James

    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/

  • James

    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/

  • irfan

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

    Regards

  • irfan

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

    Regards

  • http://www.aneef.net/ Aneef Fashir

    @Irfan,

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

  • http://www.aneef.net Aneef Fashir

    @Irfan,

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

  • AD

    What is the difference between iTextSharp and PDF Sharp? I need some PDF creation in my C# project and was looking at PDF sharp. Not sure what the iTextsharp is?

  • Maki

    How I can create PDF that excepts Unicode content using this library?

    Thank you

  • Dhaval

    Thanks aneef, your code helped me solve my problem

  • http://www.myalphablog.com Pdf tutorials

    wow nice post. PDF sharp works well.

  • Srinivasmca Kamma

    Could not load file or assembly ’36763-5441-itextsharp’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0×80131040) plz slove this error

  • http://www.cogniview.com/ pdf to excel converter

    HI, 

    Its great guys that PDFsharp is the Open Source library that easily creates PDF documents from any .NET language. PDFsharp works so well guys, why I use other, I’ll use it. 

  • CARLOS CHAVEZ

    Hello

    It is possible with pdfSharp

    If a PDF has applied ocr
    measure resolution PDF
    and see if this grayscale???

    I hope you can help me, Greetings

Tags

Archives