Click here to Skip to main content
Click here to Skip to main content

Creating PDF with nFOP

, 11 Jan 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use generate PDF files on the fly using nFOP


nFOP is an elegant solution to generate PDF on the fly. It is originally a Java open source product that has been migrated to .NET.

The purpose of this article is to show the use of nFOP to generate a PDF file from XML data. We won't describe the FO format here. Many excellent articles exist on the Web about FO which is a W3C standard. Unfortunately it doesn't seem to be much known by the .NET community so far.

First, you have to download nFOP from here.

Using the Code

Here is the data in input - my favourite French books:


<?xml version="1.0" encoding="utf-8" ?>
    <Title>Madame Bovary</Title>
    <Author>Gustave Flaubert</Author>
    <Title>Bel Ami</Title>
    <Title>La Curée</Title>
    <Author>Emile Zola</Author>
    <Title>Les Chouans</Title>
    <Author>Honoré de Balzac</Author>

Here is now the XSL transformation that I will apply to this XML. I create a table in FO language, with three columns. The rows will be generated by the XSL transformation, one for each book.


      <xsl:for-each select="Books/Book">
              <xsl:value-of select=".//Title" />
              <xsl:value-of select=".//Author" />
              <xsl:value-of select=".//Price" />

In Visual Studio, reference the nFop assembly ApacheFop.Net.dll and the J# assembly vjslib.dll.

You will need to add the following namespaces to your class:

    using System.Xml;
    using System.Xml.Xsl;
    using System.Xml.XPath;
    using org.apache.fop;
    using org.apache.fop.apps;
    using org.xml.sax;

We call the classic XML/XSL transformation to produce the FO file:

    // Load the FO style sheet.
    XslCompiledTransform xslt = new XslCompiledTransform();

    // Execute the transform and output the results to a file.
    xslt.Transform("books.xml", "");

At last, here comes the most interesting part, the use of NFOP to generate the PDF file.

Here is the method that does the job:
    private void GeneratePDF(string foFile, string pdfFile)
        FileInputStream streamFO = new FileInputStream(foFile);
        InputSource src = new InputSource(streamFO);
        FileOutputStream streamOut = new FileOutputStream(pdfFile);
        Driver driver = new Driver(src, streamOut);


nFOP is an easy solution to implement and it is free. Because the PDF structure is described in an XML file, it is easy to configure its disposition, which clients always ask for.

To learn more about FOP, visit the Apache site and download the Java version. It contains plenty of samples of FO files and good documentation.


  • 11th January, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jerome Bellanger
Software Developer (Senior) Freelance
France France
No Biography provided

Comments and Discussions

QuestionError assembly PinmemberMember 953878123-Oct-12 4:32 
SuggestionEditors for FOP PinmemberMember 827588227-Nov-11 1:49 
GeneralMy vote of 5 Pinmembervinayak.bhadage19-Apr-11 23:52 
QuestionDriver.setRenderer(8); not working Pinmemberpricksaw24-May-10 14:43 
GeneralSecured PDF Generation Pinmembervinvino200122-Apr-10 22:32 
GeneralRe: Secured PDF Generation PinmemberWiseLearner11-Feb-13 9:04 
GeneralRe: Secured PDF Generation Pinmembervinvino200111-Feb-13 18:48 
QuestionDriver present in which library? ISO8859_1 error? Pinmemberpricksaw9-Feb-10 14:24 
AnswerRe: Driver present in which library? ISO8859_1 error? Pinmemberpricksaw9-Feb-10 19:22 
GeneralRe: Driver present in which library? ISO8859_1 error? Pinmemberpricksaw11-Feb-10 17:34 
GeneralI am getting error like this PinmemberShaik Haneef14-Sep-09 21:41 
GeneralRe: I am getting error like this PinmemberKotty19856-Nov-09 5:51 
GeneralIssue parsing out encoded html Pinmembercoolaid0925-Aug-09 9:51 
Questionimages Pinmembercoolaid0920-Aug-09 10:34 
AnswerRe: images Pinmemberpricksaw1-Mar-10 12:08 
QuestionFop license? Pinmemberpereszczako8-Jun-09 23:39 
QuestionBarcode fonts embedded using the userconfig.xml file Pinmemberdjblues30-Apr-09 9:46 
GeneralIssue with FOP Pinmembersonu941-Dec-08 23:09 
Generalreference-orientation does not work PinmemberMember 212122031-Mar-08 10:14 
QuestionLatin2 character set PinsussGabor11-Dec-07 0:41 
QuestionBuild Pinmemberlokesh_sg7-Nov-07 1:23 
QuestionHyphenation PinmemberDaniele Fusi29-Mar-07 0:34 
GeneralJava does not work PinmemberMember #385632223-Feb-07 6:42 
GeneralRe: Java does not work PinmemberWingnut121327-Feb-07 9:02 
QuestionLarge FO files take very long time to process PinmemberDadou00220-Nov-06 20:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 11 Jan 2006
Article Copyright 2006 by Jerome Bellanger
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid