Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Generating PDF reports using nfop

, , 7 Jun 2011 CPOL
This article will help you to examine the main features of XSL schemes to generate programmatically advanced PDF reports.
PDFReport.zip
PDFReport
PDFReport
PDFReport
PDFReport
Properties
report.fo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Xml.Xsl;

using java.io;
using org.xml.sax;

using org.apache.fop;
using org.apache.fop.apps;
using org.apache.fop.tools;
using org.apache.fop.configuration;

namespace PDFReport
{
    public static class IDGenerator
    {
        private static int id = 0;
        public static int Generate()
        {
            return id++;
        }
    }
    public class Author
    {
        public List<Book> created;
        public string name;
        public int id;
        public Author()
        {
            this.name = "no name";
            this.created = new List<Book>();
            this.id = IDGenerator.Generate();
        }
        public Author(string value)
        {
            this.name = value;
            this.created = new List<Book>();
            this.id = IDGenerator.Generate();
        }
        public void CreateBook(string name, List<Pare> articles, double bookPrice)
        {
            Book book = new Book(name);
            book.SetPrice(bookPrice);
            foreach(Pare article in articles)
            {
                book.WriteArticle(article);
            }
            this.created.Add(book);
        }
    }

    public class Book
    {
        public int id;
        public Props bookProps;
        public List<Pare> articles;
        
        public Book(string value)
        {
            this.bookProps = new Props(value, 0);
            this.articles = new List<Pare>();            
            this.id = IDGenerator.Generate();
        }
        public Book()
        {
            this.bookProps = new Props("no name", 0);
            this.articles = new List<Pare>();
            this.id = IDGenerator.Generate();
        }
        public void SetPrice(double priceValue)
        {
            this.bookProps.price = priceValue;
        }
        public void WriteArticle(Pare article)
        {
            this.articles.Add(article);
        }
    }
    public class Props
    {
        public string name;
        public double price;
        public Props()
        {
            this.name = "no name";
            this.price = 0;    
        }
        public Props(string stringParam, int intParam)
        {
            this.name = stringParam;
            this.price = intParam;
        }
    }
    public class Pare
    {
        
        public string name;
        public int pages;
        public Pare()
        {
            this.name = "no name";
            this.pages = 0;
        }
        public Pare(string stringParam, int intParam)
        {
            this.name = stringParam;
            this.pages = intParam;
        }
    }
    
    class Program
    {      
        static void Main(string[] args)
        {
            ConfigurationReader reader_ = null;

            string fileName = "E://pdf_root/report";
            string fileXML = fileName + ".xml";
            string fileXSL = fileName + ".xslt";
            string fileFO = fileName + ".fo";

            Author writer = new Author("Tolkein");
            List<Pare> articles = new List<Pare>();

            articles.Add(new Pare("Gollum",190));
            articles.Add(new Pare("Gendalf", 90));
            articles.Add(new Pare("Frodo",31));
            articles.Add(new Pare("Boromir", 095));
            writer.CreateBook("Lord of the rings",articles, 900.0);             

            articles.Clear();
            articles.Add(new Pare("Introduction",86));
            articles.Add(new Pare("Table of contents", 17));
            articles.Add(new Pare("Chapter 1", 902));
            articles.Add(new Pare("Chapter 2", 1500));
            articles.Add(new Pare("Chapter 3",91));
            articles.Add(new Pare("Chapter 100500",74));
            writer.CreateBook("How i met your mother",articles, 490.0);             

            articles.Clear();
            articles.Add(new Pare("1",43));
            articles.Add(new Pare("2",73));
            writer.CreateBook("Sample",articles, 130.0);

            Author writer1 = new Author("Tolstoy");
            articles.Clear();
            articles.Add(new Pare("sample ыыы1", 123));
            articles.Add(new Pare("sample 2", 124));
            articles.Add(new Pare("sample 3", 125));
            writer1.CreateBook("New one", articles, 12.0);

            //Serializing data
            FileStream fs = System.IO.File.Create(fileXML);
            XmlSerializer s = new XmlSerializer(typeof(List<Author>));

            List<Author> list = new List<Author>();
            list.Add(writer);
            list.Add(writer1);

            s.Serialize(fs, list);
            fs.Close();
            
            
            //Generate FO file
            XslTransform xslt = new XslTransform();
            xslt.Load(fileXSL);
            xslt.Transform(fileXML, fileFO);
            
            //Setup font configurations
            string configPath = "..fonts\\userconfig.xml";//path to you config file 
            FileInputStream inputStream = null;
            InputSource inputSource = null;

            try
            {
                inputStream = new FileInputStream(configPath);
                inputSource = new InputSource(inputStream);
                reader_ = new ConfigurationReader(inputSource);
                reader_.start();
            }
            catch (FOPException ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.close();
                }
            }

            //Generate PDF file
            java.io.FileInputStream streamFO = null;
            java.io.FileOutputStream streamOut = null;
            try
            {
                streamFO = new java.io.FileInputStream(fileFO);
                streamOut = new java.io.FileOutputStream(fileName + ".pdf");

                InputSource src = new InputSource(streamFO);
                Driver driver = new Driver(src, streamOut);
                driver.setRenderer(Driver.RENDER_PDF);
                driver.run();
            }
            catch(FOPException ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
            finally
            {
                if (streamOut != null)
                {
                    streamOut.close();
                }
                if (streamFO != null)
                {
                    streamFO.close();
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Authors

Apriorit Inc
Apriorit Inc.
Ukraine Ukraine
ApriorIT is a Software Research and Development company that works in advanced knowledge-intensive scopes.
 
Company offers integrated research&development services for the software projects in such directions as Corporate Security, Remote Control, Mobile Development, Embedded Systems, Virtualization, Drivers and others.
 
Official site http://www.apriorit.com
Group type: Organisation

31 members

Follow on   LinkedIn

Petro Vodopyan

Ukraine Ukraine
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 7 Jun 2011
Article Copyright 2010 by Apriorit Inc, Petro Vodopyan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid