Click here to Skip to main content
15,305,577 members
Articles / Mobile Apps / Xamarin
Posted 20 Jun 2016


9 bookmarked

Create PDF documents from any XML format

20 Jun 2016CPOL2 min read
This article explains how to create PDF documents from any XML format.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

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:

Image 1

Create PDF Programmatically

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

// create a new document with a single section
Document document = new Document();
Section section = document.Sections.Add();

// add a new textparagraph to the section
TextParagraph textParagraph = new TextParagraph();

// create a fragment, set some text and add it to the paragraph
Fragment fragment = new Fragment();
fragment.Text = "Hello world!";

using (FileStream file = new FileStream("out1.pdf", FileMode.Create))

Image 2

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

Image 3

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:

<document xmlns="">
      <paragraph type="textparagraph">
         <fragment>Hello World</fragment>

Here is the code that transforms this XML to PDF:

// create a new document from xml
Document document = new Document();

using (FileStream file = new FileStream("out2.pdf", FileMode.Create))

Create PDF from XML and XSL

Consider the following simple XML document describing two customers:

<?xml version="1.0" encoding="utf-8" ?>
    <Customer id="1">
        <Name>Chris Sharp</Name>
    <Customer id="2">
        <Name>Mike Jones</Name>

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:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    <xsl:output method="xml" encoding="utf-8"/>
    <xsl:template match="/">
                <paragraph type="textparagraph">
                    <fragment font="helveticabold" fontsize="14">
                        Simple XSL Transformation
                <xsl:apply-templates select="/Customers/Customer"/>
    <xsl:template match="/Customers/Customer">
        <paragraph type="textparagraph">
            <fragment font="helveticabold" fontsize="10">
                <xsl:value-of select="@id"/>.
            <fragment font="helvetica" fontsize="10">
                <xsl:value-of select="Name"/>

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

// load xml
XmlReader xml = new XmlTextReader("data.xml");

// load xsl
XslCompiledTransform xsl = new XslCompiledTransform();

// transform
Stream converted = new MemoryStream();
xsl.Transform(xml, null, converted);
converted.Position = 0;

// construct the pdf from transformed xml
Document document = new Document();
XmlReader reader = new XmlTextReader(converted);

using (FileStream file = new FileStream("out3.pdf", FileMode.Create))

The result:

Image 4


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


About the Author

Frank Rem
Software Developer
Netherlands Netherlands
Worked for some years as a software engineer, architect and project leader for different software companies. Works at TallComponents, vendor of class libraries for creating, manipulating and rendering PDF documents.

Comments and Discussions

-- There are no messages in this forum --