Click here to Skip to main content
15,038,466 members
Articles / Mobile Apps / Xamarin
Article
Posted 20 Jun 2016

Stats

18.8K views
235 downloads
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:

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

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

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

using (FileStream file = new FileStream("out1.pdf", FileMode.Create))
{
  document.Write(file);
}

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:

XML
<document xmlns="http://www.tallcomponents.com/schemas/tallpdf/v3">
   <section>
      <paragraph type="textparagraph">
         <fragment>Hello World</fragment>
      </paragraph>
   </section>
</document>

Here is the code that transforms this XML to PDF:

C#
// create a new document from xml
Document document = new Document();
document.Read("helloworld.xml");

using (FileStream file = new FileStream("out2.pdf", FileMode.Create))
{
  document.Write(file);
}

Create PDF from XML and XSL

Consider the following simple XML document describing two customers:

XML
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
    <Customer id="1">
        <Name>Chris Sharp</Name>
    </Customer>
    <Customer id="2">
        <Name>Mike Jones</Name>
    </Customer>
</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:

XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.tallcomponents.com/schemas/tallpdf/v3">
    <xsl:output method="xml" encoding="utf-8"/>
    <xsl:template match="/">
        <document>
            <section>
                <paragraph type="textparagraph">
                    <fragment font="helveticabold" fontsize="14">
                        Simple XSL Transformation
                    </fragment>
                </paragraph>
                <xsl:apply-templates select="/Customers/Customer"/>
            </section>
        </document>
    </xsl:template>
    <xsl:template match="/Customers/Customer">
        <paragraph type="textparagraph">
            <fragment font="helveticabold" fontsize="10">
                <xsl:value-of select="@id"/>.
            </fragment>
            <fragment font="helvetica" fontsize="10">
                <xsl:value-of select="Name"/>
            </fragment>
        </paragraph>
    </xsl:template>
</xsl:stylesheet>

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

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

// load xsl
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load("transform.xslt");

// 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);
document.Read(reader);

using (FileStream file = new FileStream("out3.pdf", FileMode.Create))
{
  document.Write(file);
}

The result:

Image 4

License

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

Share

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 --