Click here to Skip to main content
15,893,814 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.9K   1.9K   127  
This article will help you to examine the main features of XSL schemes to generate programmatically advanced PDF reports.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:fox="http://xml.apache.org/fop/extensions"
  exclude-result-prefixes="fo">

  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
  <xsl:template match="/">
    <fo:root font-family="arialuni">
      <fo:layout-master-set>
        <!--spm for first page-->
        <fo:simple-page-master master-name="first" page-height="29.7cm" page-width="21cm" >
          <fo:region-body margin="40pt"/>
          <fo:region-after extent="1in"/>
          <fo:region-before extent="0.5in"/>
          <fo:region-end extent="0.4in"/>
          <fo:region-start extent="0.4in"/>
        </fo:simple-page-master>

        <!-- spm for all pages -->
        <fo:simple-page-master master-name="allPages" page-height="29.7cm" page-width="21cm">
          <fo:region-body margin="40pt"/>
          <fo:region-after extent="1in"/>
          <fo:region-before extent="0.4in"/>
          <fo:region-end extent="0.4in"/>
          <fo:region-start extent="0.4in"/>
        </fo:simple-page-master>

        <!-- Control the sequencing for odd and even pages in chapters -->
        <fo:page-sequence-master master-name="chaps">          
        </fo:page-sequence-master>
      </fo:layout-master-set>      
      <!-- page-sequence for introduction page -->
      <fo:page-sequence master-reference="first">
        <fo:static-content flow-name="xsl-region-before">
          <fo:block text-align="center" space-before="10pt" font-size="13pt" font-weight="bold">
            <fo:leader leader-length="90%" leader-pattern="rule"
                   rule-thickness="0.5pt" color="black"/>
          </fo:block>
        </fo:static-content>
        <fo:static-content flow-name="xsl-region-after">
          <!--Numbers of pages and logo in lower right part of report page-->
          <fo:block text-align="right" margin-right="25pt" >
            <fo:external-graphic scaling="uniform" height="0.4in" width="1in" src="file:///E:/pdf_root/1.JPG"/>
          </fo:block>
          <fo:block text-align="center" space-before="10pt" font-size="13pt" font-weight="bold">
            <fo:page-number/>
          </fo:block>
        </fo:static-content>
        
        <fo:flow flow-name="xsl-region-body">
          <!--Building table of contents-->
          <fo:block font-family="arialuni" font-size="25pt" id="content_table" space-after="20pt" text-align="center">
            Table of contents       
          </fo:block>          
          <fo:block text-align-last="justify" break-after="page">
            <xsl:call-template name="BuildContentTable"/>
          </fo:block>          
          <!--Building table with data-->
          <fo:block>
            <xsl:for-each select="ArrayOfAuthor/Author">
              <fo:block>
                <xsl:if test="position() != last()">
                  <xsl:attribute name="break-after">
                    <xsl:value-of select="'page'"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:call-template name="BuildTable"/>
              </fo:block>
            </xsl:for-each>
          </fo:block>
          
          <!--Building bookmark tree-->
          <fo:block>
            <xsl:call-template name="BuildBookmarkTree"/>
          </fo:block>         
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>  

  <xsl:template name="BuildContentTable">
    <xsl:for-each select="ArrayOfAuthor/Author">
      <fo:basic-link>
        <xsl:attribute name="internal-destination">
          <xsl:value-of select="id"/>
        </xsl:attribute>
        <fo:inline font-weight="bold" color="red">
          <xsl:value-of select="name"/>
        </fo:inline>
        <fo:leader leader-pattern="dots"/>
        <fo:page-number-citation>
          <xsl:attribute name="ref-id">
            <xsl:value-of select="id"/>
          </xsl:attribute>
        </fo:page-number-citation>       
      </fo:basic-link>      
      <xsl:for-each select="created/Book">
          <fo:block margin-left="1cm" text-align-last="justify">
            <fo:basic-link>
              <xsl:attribute name="internal-destination">
                <xsl:value-of select="id"/>
              </xsl:attribute>
              <fo:inline font-weight="bold" font-family="Courier">
                <xsl:value-of select="bookProps/name"/>
              </fo:inline>
              <fo:leader leader-pattern="dots"/>
              <fo:page-number-citation>
                <xsl:attribute name="ref-id">
                  <xsl:value-of select="id"/>
                </xsl:attribute>
              </fo:page-number-citation>
            </fo:basic-link>
          </fo:block>
        </xsl:for-each>
      <xsl:if test="position() != last()">
        <fo:block>
          <!--separator-->
          <fo:leader leader-length="100%" leader-pattern="rule"
                   rule-thickness="0.5pt" color="black"/>
        </fo:block>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template name="BuildBookmarkTree">
    <fox:outline internal-destination="content_table">
      <fox:label>
        <xsl:text>Table of contents</xsl:text>
      </fox:label>
    </fox:outline>
    <xsl:for-each select="ArrayOfAuthor/Author">    
      <fox:outline>
        <xsl:attribute name="internal-destination">
          <xsl:value-of select="id"/>
        </xsl:attribute>
        <fox:label>
          <xsl:value-of select="name"/><!--Name of bookmark-->
        </fox:label>
        <xsl:for-each select="created/Book">
          <fox:outline>
            <xsl:attribute name="internal-destination">
              <xsl:value-of select="id"/>
            </xsl:attribute>
            <fox:label>
              <xsl:value-of select="bookProps/name"/>
            </fox:label>
          </fox:outline>
          </xsl:for-each>
      </fox:outline>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template name="FillGrid">
    <fo:table>
      <fo:table-column background-color="#DCDCDC" column-width="20%"/>
      <fo:table-column background-color="#FFFFFF"/>
      <fo:table-body>
        <xsl:for-each select="articles/Pare">
          <fo:table-row>
            <fo:table-cell padding="1pt" border="0.5pt solid black">
              <fo:block>
                <xsl:value-of select="name"/>
              </fo:block>
            </fo:table-cell>
            <fo:table-cell padding="1pt" border="0.5pt solid black">
              <fo:block>
                <xsl:value-of select="pages"/>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </xsl:for-each>
      </fo:table-body>
    </fo:table>
  </xsl:template>
  
  <xsl:template name="BuildTable">    
    <fo:block text-align="center" font-size="25pt"  space-after="5pt" font-weight="bold" color="#58FF11">
      <xsl:attribute name="id">
        <xsl:value-of select="id"/>
      </xsl:attribute>        
      <xsl:value-of select="name"/>
    </fo:block>
    <xsl:for-each select="created">
      <xsl:for-each select="Book">
        <fo:table space-after="3pt">
          <fo:table-column background-color="#DCDCDC"/>
          <fo:table-body>
            <fo:table-row>
              <fo:table-cell padding="4pt" border="1pt solid black">
                <fo:block text-align="center">
                  <xsl:attribute name="id">
                    <xsl:value-of select="id"/>
                  </xsl:attribute>
                  <xsl:value-of select="bookProps/name"/>
                  <xsl:text> / Price </xsl:text>
                  <xsl:value-of select="bookProps/price"/>
                </fo:block>
                <fo:block>
                  <xsl:call-template name="FillGrid"/>
                </fo:block>
              </fo:table-cell>
            </fo:table-row>            
          </fo:table-body>
        </fo:table>
      </xsl:for-each>     
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="ParallelReading">
    <xsl:for-each select="Rows">
      <fo:table>
        <fo:table-column background-color="#DCDCDC" column-width="20%"/>
        <fo:table-column background-color="#FFFFFF" />
        <fo:table-body>
          <xsl:for-each select="Row">
            <fo:table-row>
              <fo:table-cell padding="4pt" border="1pt solid black">
                <xsl:for-each select="../../Columns/Column">
                  <fo:block>
                    <xsl:value-of select="Title"/>
                  </fo:block>
                </xsl:for-each>
              </fo:table-cell>
              <fo:table-cell padding="4pt" border="1pt solid black">
                <xsl:for-each select="Value">
                  <fo:block>
                    <xsl:value-of select="Link"/>
                  </fo:block>
                </xsl:for-each>
              </fo:table-cell>
            </fo:table-row>
          </xsl:for-each>
        </fo:table-body>      
      </fo:table>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

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