65.9K
CodeProject is changing. Read more.
Home

Create PDF documents from any XML format

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jun 20, 2016

CPOL

2 min read

viewsIcon

27329

downloadIcon

298

This article explains how to create PDF documents from any XML format.

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:

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();
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);
}

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:

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

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

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