A simple example using Ibex .NET from C#
This example shows how to call Ibex from a C# program and create a PDF file
from XML and XSL files.
approach
In this example we use an XML file containing the data we want to get into the PDF file, and
and XSL stylesheet which transforms the XML into the FO syntax which Ibex understands.
the XML file
The XML used in this example contains text held in one or more paragraph elements. It looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<para>hello world</para>
</text>
the XSL file
An XSL stylesheet is used to transform the XML into XSL-FO syntax. Using a stylesheet means
that when the data changes (in the XML file) we do not have to make any changes to the
C# program or the XSL stylesheet. If we add new elements to the XML which we want to appear
in the PDF file, then we will need to change the XSL stylesheet, but we won't have to change the
C# program. And changing the XSL is simple because we don't need to recompile it and can
just use a text editor to maintain it.
The XSL used in this example looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/XSL/Format"
xmlns:ibex="http://www.xmlpdf.com/2003/ibex/Format">
<xsl:strip-space elements="*"/>
<xsl:template match="text">
<root>
<layout-master-set>
<simple-page-master master-name="page-layout">
<region-body margin="2.5cm" region-name="body" background-color="#dddddd"/>
</simple-page-master>
</layout-master-set>
<page-sequence master-reference="page-layout">
<flow flow-name="body">
<xsl:apply-templates select="para"/>
</flow>
</page-sequence>
</root>
</xsl:template>
<xsl:template match="para">
<block>
<xsl:apply-templates select="text()"/>
</block>
</xsl:template>
</xsl:stylesheet>
Basically what this XSL does it output the <root> element of the FO file, which contains
the layout-master-set defining the page structure, and then output each <para> element
from the XML inside an FO block element.
the program
The C# program used in this example is:
using System;
using System.IO;
using System.Collections;
using ibex4;
public class HelloWorld {
static void Main( string[] args ) {
if( args.Length < 3 ) {
Console.WriteLine( "usage:hello xml-file xsl-file pdf-file" );
return;
}
FileStream xml = new FileStream( args[0], FileMode.Open, FileAccess.Read );
FileStream xsl = new FileStream( args[1], FileMode.Open, FileAccess.Read );
FileStream pdf = new FileStream( args[2], FileMode.Create, FileAccess.Write );
FODocument doc = new FODocument();
doc.generate( xml, xsl, pdf );
}
}
What this program does is:
- check that we have 3 arguments;
- create streams for input of the XML and XSL, and output of the PDF;
- create an Ibex FODocument object;
- call the generate() method to create the PDF file.
The program can be compiled from the command line, linking in the Ibex DLL like this:
csc /r:ibex20.dll hello.cs
This creates the program hello.exe. To run it we pass the names of the XML, XSL and PDF files on the command
line like this:
hello hello.xml hello.xsl hello-world.pdf
This will create the file hello.pdf containing the data from hello-world.xml.
downloads
Downloads for this example are:
The XML file
hello.xml.
The XSL file
hello.xsl.
The resulting PDF file
hello-world.pdf.