Click here to Skip to main content
15,879,348 members
Articles / Programming Languages / C#

Generating PDF reports using nfop

,
Rate me:
Please Sign up or sign in to vote.
4.81/5 (38 votes)
7 Jun 2011CPOL7 min read 91.2K   1.9K   127  
This article will help you to examine the main features of XSL schemes to generate programmatically advanced PDF reports.
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)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions