Create PDF documents from any XML format

6/21/2016 By Arjen 0 comments

TallPDF.NET is a commercial class library for creating PDF documents from a layout-oriented document object model. The document object model can be constructed either programatically and from XML. If created from XML, an XSL can be used to transform any XML to the XML that is TallPDF.NET compliant.

Layout classes

The TallPDF.NET class library consists of classes such as Document, Section, Footer and Paragraph. Here is the full documentation. The following diagram shows how a PDF document is constructed from layout objects:

Layout objects to PDF.gif

Create PDF programmatically

Here is a Hello world! example of creating a PDF programmatically:


1 // create a new document with a single section 2 Document document = new Document(); 3 Section section = document.Sections.Add(); 4 document.Sections.Add(section); 5 6 // add a new textparagraph to the section 7 TextParagraph textParagraph = new TextParagraph(); 8 section.Paragraphs.Add(textParagraph); 9 10 // create a fragment, set some text and add it to the paragraph 11 Fragment fragment = new Fragment(); 12 fragment.Text = "Hello world!"; 13 textParagraph.Fragments.Add(fragment); 14 15 using (FileStream file = new FileStream("out1.pdf", FileMode.Create)) 16 { 17 document.Write(file); 18 }

Although this seems like a lot of code for just "Hello world!", its flexibility does pay of when constructing more complex documents.

Xamarin.iOS and Xamarin.Android editions

The new version 5 of TallPDF.NET includes Xamarin editions for iOS and Android. A first version is already available. If you are interested, you can join the beta program


Create PDF from XML

If you are familiar with XAML, then you know that XAML is a means of specifying how objects should be instantiated and how properties should be assigned. Creating PDF from XML with TallPDF.NET works similarly. As a side-note: we implemented this approach in 2002, way before XAML which was initially released in 2008.

Here is the exact same Hello world! example but now specified in XML:


1 <document xmlns=""> 2 <section> 3 <paragraph type="textparagraph"> 4 <fragment>Hello World</fragment> 5 </paragraph> 6 </section> 7 </document>

Here is the code that transforms this XML to PDF:


1 // create a new document from xml 2 Document document = new Document(); 3 document.Read("helloworld.xml"); 4 5 using (FileStream file = new FileStream("out2.pdf", FileMode.Create)) 6 { 7 document.Write(file); 8 }

Create PDF from XML and XSL

Consider the following simple XML document describing two customers:


1 <?xml version="1.0" encoding="utf-8" ?> 2 <Customers> 3 <Customer id="1"> 4 <Name>Chris Sharp</Name> 5 </Customer> 6 <Customer id="2"> 7 <Name>Mike Jones</Name> 8 </Customer> 9 </Customers>

Clearly, this document cannot be converted to PDF since it contains no mapping of XML elements to layout elements. For this, we write an XSL that transforms this XML to a TalPDF.NET compliant XML that we saw before:


1 <?xml version="1.0" encoding="utf-8"?> 2 <xsl:stylesheet version="1.0" 3 xmlns:xsl="" 4 xmlns=""> 5 <xsl:output method="xml" encoding="utf-8"/> 6 <xsl:template match="/"> 7 <document> 8 <section> 9 <paragraph type="textparagraph"> 10 <fragment font="helveticabold" fontsize="14"> 11 Simple XSL Transformation 12 </fragment> 13 </paragraph> 14 <xsl:apply-templates select="/Customers/Customer"/> 15 </section> 16 </document> 17 </xsl:template> 18 <xsl:template match="/Customers/Customer"> 19 <paragraph type="textparagraph"> 20 <fragment font="helveticabold" fontsize="10"> 21 <xsl:value-of select="@id"/>. 22 </fragment> 23 <fragment font="helvetica" fontsize="10"> 24 <xsl:value-of select="Name"/> 25 </fragment> 26 </paragraph> 27 </xsl:template> 28 </xsl:stylesheet>

Here is the code that transforms the customers XML to PDF using the XSL:


1 // load xml 2 XmlReader xml = new XmlTextReader("data.xml"); 3 4 // load xsl 5 XslCompiledTransform xsl = new XslCompiledTransform(); 6 xsl.Load("transform.xslt"); 7 8 // transform 9 Stream converted = new MemoryStream(); 10 xsl.Transform(xml, null, converted); 11 converted.Position = 0; 12 13 // construct the pdf from transformed xml 14 Document document = new Document(); 15 XmlReader reader = new XmlTextReader(converted); 16 document.Read(reader); 17 18 using (FileStream file = new FileStream("out3.pdf", FileMode.Create)) 19 { 20 document.Write(file); 21 }

The result: