Home
Suggestion or Comment?

PDFDoc Scout

PDFDoc Scout screenshot (click to enlarge)


Subscribe to Our Bi-Monthly Newsletter (tips, new releases, specials):
 Subscribe in a reader
 PRODUCTS  |  DOWNLOAD  |  PURCHASE  |  SUPPORT  |  TESTIMONIALS   |  NEWS  | 

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:


HOME  |  CONTACT US

Copyright © ByteScout, 2003-2010. Privacy Statement
Microsoft®, Windows®, Windows 2000®, Windows Server®, Windows Vista®, Internet Explorer®, .NET Framework®, ActiveX®, Visual Basic®, Visual C#®, ASP®, ASP.NET®, Excel®, PowerPoint®, are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Adobe®, Flash® and Acrobat® are registered trademarks of Adobe Systems, Incorporated. Mozilla®, Firefox® and the Mozilla and Firefox Logos are registered trademarks of the Mozilla Foundation. Other product names or brandnames used herein are for identification purposes only and might be trademarks or registered trademarks of their respective companies. We disclaim any and all rights to those marks.