Home

PDFDoc Scout

PDFDoc Scout screenshot (click to enlarge)



PDFDoc Scout library - Sample

Download Free Trial See Licensing Options More Information

How to convert XML into PDF using XSLT template transformation in Visual C#

See also XML to PDF example for VBScript (Visual Basic) here

You can download the source code of this example (Visual C#) here:
pdfdocscout_xml_xslt_transformation_example_csharp.zip

This example describes how to transform XML into PDF in Visual C# using XSLT (.xsl) template using PDFDoc Scout library

Source XML data (simpsons.xml):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Simpsons.xsl"?>
<Simpsons>
<FamilyMember>Homer Jay Simpson</FamilyMember>
<FamilyMember>Marjorie Marge Simpson (Bouvier)</FamilyMember>
<FamilyMember>Bartholomew Jojo Bart Simpson</FamilyMember>
<FamilyMember>Lisa Marie Simpson</FamilyMember>
<FamilyMember>Margaret Maggie Simpson</FamilyMember>
<ExtendedFamilyMember>Abraham Jay Simpson</ExtendedFamilyMember>
<ExtendedFamilyMember>Mona J. Simpson</ExtendedFamilyMember>
<ExtendedFamilyMember>Jacqueline Ingrid Bouvier</ExtendedFamilyMember>
<ExtendedFamilyMember>Clancy Bouvier</ExtendedFamilyMember>
<ExtendedFamilyMember>Patricia "Patty" Bouvier</ExtendedFamilyMember>
</Simpsons>

XSLT template (simpsons.xsl):

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="FamilyMember">
<p><b><u><xsl:value-of select="."/></u></b></p>
</xsl:template>

<xsl:template match="ExtendedFamilyMember">
<p><i><xsl:value-of select="."/></i></p>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Produced PDF document (XML2HTML.pdf generated from source Simpsons.xml XML data)

PDF document produced from transformed XML simpsons.xml

Code of Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;


namespace XMLtoPDFUsingPDFDocScout
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void btnXMLToPDF_Click(object sender, EventArgs e)
{
// Transform XML using XSLT and get HTML code
// then create PDF document from produced HTML code

PDFDocScout.PDFDocument PDFDoc = new PDFDocScout.PDFDocument();
PDFDoc.InitLibrary ("demo", "demo");
PDFDoc.OutputFileName = "XML2HTML.pdf";
PDFDoc.AutoOpenGeneratedPDF = true;

PDFDoc.BeginDocument();

// load source XML document
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(boxXML.Text);

XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(boxXSLT.Text);

// create XML doc to store result of transformation
XmlDocument objResultXML = new XmlDocument();

//Transform the XML using XLST into HTML (produced HTML is written to writer object)
StringWriter writer = new StringWriter();
xslt.Transform(new XPathDocument(boxXML.Text), null, writer);
writer.Close();

// convert produced HTML code into PDF
PDFDoc.Page.AddHTMLBox (writer.ToString(), 0,0, PDFDoc.Page.Width, PDFDoc.Page.Height, true);

PDFDoc.EndDocument(); // close PDF document generation

}
}
}

You can download the source code of this example (Visual C#) here:
pdfdocscout_xml_xslt_transformation_example_csharp.zip

See also XML to PDF example for VBScript (Visual Basic) here

Download Free Trial See Licensing Options More Information


See also: Bytescout.PDF - native .NET library to write/read/sign/
modify/merge/split PDF documents (but no HTML2PDF support)



On-line documentation (HTML format)


"Hello, World!" quick start step-by-step tutorials:

Products  |  Download  |  Purchase  |  Support  |  Contacts  |  Testimonials |  

ByteScout, 2003-2008. All other company and product names may be trademarks of their respective companies. Privacy Statement