Generate PDF from C#.NET

Generating PDF files from .NET has always been a cumbersome task. There are a couple of different ways by which a PDF can be created from Microsoft.NET.

  1. Crystal Reports.NET that comes with the Visual Studio.NET
  2. Free Utilities like NFop and Open source systems Report.NET, PDFSharp etc.
  3. Commercial software such as ABC Pdf, Aspose Pdf, etc

NFOP has always been my favorite to generate PDF documents. I have used this in enterprise level web applications to generate hundreds of PDF documents successfully. NFOP was originally developed by Jason Pettys. It is an extension of the open source project Apache FOP. NFOP (Formatting Objects Processor) accepts XSL-FO (XSL-Formatting object) as input and renders the result in PDF as output.

Even though the original article on NFOP by Laurent Kempfe gets you started, in the initial days I found this not adequate as it fails to provide a comprehensive view of how the FOP, .NET, XML, and XSL-FO go together in creating a PDF. This blog aims to bridge that gap.

Follow these six steps to successfully generate a PDF on the fly.

  1. Download NFop.
  2. Download Microsoft J#.NET redistributable package for .NET 1.1 or for .NET 2.0
  3. Create an ASP.NET project and add ApacheFop.Net and vjslib as a reference to the project.
  4. Data in XML format: Usually when generating a PDF document you will be dealing with dynamic content rather than static content. The first step is to get all the data that you want in a properly formatted XML document.
    <?xml version=”1.0″ encoding=”utf-8″?>
  5. Create XSLT: Google XSL-FO and you will find lot of articles on the subject. However, you have to remember that not all the language elements in XSL-FO are supported by NFOP and there are no reliable documents that I have come across that provide this information. When generating complex PDF docs you may end up following trial and error method to find the XSL-FO elements accepted by NFOP. However, for most practical purposes you should not have any problem.
  6. Render PDF: Use the C# Code, XML (as part of C# code) and XSLT-File to generate the PDF. That’s all to it.

7 Responses

  1. Kenneth Jessen says:


    I have tried using nFop, BUT I always run into the problem of using SVG and more important : ttf-fonts, for example barcode fonts.

    Question : Have you tried others fonts with nFop, and if so, How ?

  2. Ethan Hall says:


    You can specify the font you want on the XSLT. When the PDF renders it will honor the font if the font is available.

    I have not used SVG with nFOP. What are you trying to do?

  3. Kenneth Jessen says:

    specifying the fonts in the XSLT, how do that.

    I im using XSLFAST til generate my xslt. ???

  4. Ethan Hall says:

    Specify the font-family as an attribute of fo:block in your xslt file.

  5. Kenneth Jessen says:

    Hi again

    Do you have a working example, as a zip file, or something ?

  6. Ethan Hall says:


    The code in the blog should give you the working copy that you are expecting.

  7. theifastkann says:

    “The first step is to get all the data that you want in a properly formatted XML document.”
    Are you sure that this is true?

Leave a Reply