How to Convert XML to PDF Using C#

Convert XML to PDF in C# and VB.NET

Convert XML to PDF in C# using XSLT transformations to first convert XML to HTML, then render the HTML as PDF with IronPDF, providing a reliable solution for complex XML schemas.

Converting XML directly to PDF in C# requires a strategic approach. The most effective method uses XSLT as a transformation template. XML converts to PDF via HTML(5) using XSLT transformations. XSLT documents define how XML from a given schema converts to accurate HTML representation following well-established standards. XSLT acts as a custom translator from XML to HTML. IronPDF's Chrome rendering engine ensures pixel-perfect conversion from HTML to PDF.

Navigate to 'Using the XslCompiledTransform Class' article by Microsoft to learn more about XSLT transformation.

Quickstart: Convert XML to PDF with IronPDF

Transform XML files to PDFs effortlessly using IronPDF. With a few lines of code, utilize XSLT to convert XML data into HTML and render it as a PDF document. IronPDF provides straightforward integration while maintaining formatting and ensuring compatibility across platforms. This process leverages IronPDF's HTML to PDF capabilities to create high-quality documents.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer()
        .RenderHtmlAsPdf(
            XslCompiledTransform.Load("template.xslt")
                .Transform(XmlReader.Create("data.xml"), new StringWriter())
                .ToString()
        )
        .SaveAs("output.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

How Do I Implement XML to PDF Conversion in C#?

To implement XML to PDF conversion in C#, combine XSLT transformation with PDF generation. First, create an XSLT template that defines how XML data should format as HTML. Then use XslCompiledTransform class to apply this transformation to XML data. Finally, render the resulting HTML as PDF using IronPDF's ChromePdfRenderer.

The process involves loading the XSLT template, applying it to the XML data source, and capturing the HTML output. Pass this HTML directly to IronPDF's renderer, which handles all complexities of creating a properly formatted PDF document. For advanced scenarios, apply custom CSS styling to enhance the visual presentation of PDF output.

Why Use XSLT for XML to PDF Conversion?

XSLT (Extensible Stylesheet Language Transformations) provides the most flexible and maintainable approach for converting XML to PDF. Unlike direct XML-to-PDF libraries, XSLT allows defining precise transformation rules that handle complex XML schemas and nested structures. This approach gives complete control over the final document's appearance while maintaining separation between data (XML) and presentation (XSLT).

Using XSLT enables leveraging existing web technologies like HTML and CSS for styling PDF output. Use familiar tools and techniques rather than learning proprietary PDF formatting languages. Additionally, XSLT templates are reusable and easily modified without changing C# code, making maintenance straightforward.

What Are the Key Components Needed?

Essential components for XML to PDF conversion include the System.Xml namespace for XML processing, XslCompiledTransform class for XSLT transformations, and IronPDF for HTML to PDF rendering. Install IronPDF via NuGet to access PDF generation capabilities.

The XSLT template serves as the blueprint for transformation, defining how XML elements map to HTML structures. The template should include proper HTML structure with styling information, either inline or through CSS. For complex layouts, manage fonts and handle images appropriately within transformations.

When Should I Choose This Approach Over Alternatives?

The XSLT transformation approach excels when dealing with structured XML data needing flexible formatting options. It suits generating reports, invoices, catalogs, or documents where XML data must present in specific layouts. This method works well when supporting multiple output formats from the same XML source by creating different XSLT templates for different presentation requirements.

However, for simple XML structures or occasional document conversions, consider simpler alternatives. For basic conversions, parse XML directly and build HTML programmatically. But for production systems handling various XML schemas or requiring maintainable transformation logic, the XSLT approach with IronPDF provides the most robust solution.

The resultant HTML string or file renders as PDF using the .NET PDF Generator. Download a sample project showcasing IronPDF's capabilities for converting XML to PDF from this XML to PDF Conversion Example.

Example

// XSLT template that defines the transformation from XML to HTML
string xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<html>
<style>
td{
  text-align: center;
  padding: 20px;
  border: 1px solid #CDE7F0;
}
th{
  color: white;
  padding: 20px;
}
</style>
<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>
  <table style='border-collapse: collapse;'>
    <thead>
      <tr>
        <th colspan='3'>
          <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr bgcolor='#9acd32'>
        <th bgcolor='#32ab90'>Title</th>
        <th bgcolor='#f49400'>Feature</th>
        <th bgcolor='#2a95d5'>Compatible</th>
      </tr>
      <xsl:for-each select='catalog/cd'>
      <tr>
        <td style='font-weight: bold;'><xsl:value-of select='title'/></td>
        <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>
        <td><xsl:value-of select='compatible'/></td>
      </tr>
      </xsl:for-each>
    </tbody>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
";

// XML data to transform
string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
  <cd>
    <title>IronPDF</title>
    <feature>Generate, format and manipulate PDFs</feature>
    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>
  </cd>
  <cd>
    <title>IronOCR</title>
    <feature>OCR engine, input, result</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
  <cd>
    <title>IronBarcode</title>
    <feature>Format, read and write Barcode</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
</catalog>
";

// Create an instance of XslCompiledTransform
XslCompiledTransform transform = new XslCompiledTransform();

// Load the XSLT from a string
using (XmlReader reader = XmlReader.Create(new StringReader(xslt)))
{
    transform.Load(reader);
}

// Transform the XML to HTML
StringWriter results = new StringWriter();
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
    transform.Transform(reader, null, results);
}

// Create a renderer for converting HTML to PDF
IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();
// Options, headers, and footers may be set here if needed
// Render our XML as a PDF via XSLT transformation
renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
// XSLT template that defines the transformation from XML to HTML
string xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<html>
<style>
td{
  text-align: center;
  padding: 20px;
  border: 1px solid #CDE7F0;
}
th{
  color: white;
  padding: 20px;
}
</style>
<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>
  <table style='border-collapse: collapse;'>
    <thead>
      <tr>
        <th colspan='3'>
          <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr bgcolor='#9acd32'>
        <th bgcolor='#32ab90'>Title</th>
        <th bgcolor='#f49400'>Feature</th>
        <th bgcolor='#2a95d5'>Compatible</th>
      </tr>
      <xsl:for-each select='catalog/cd'>
      <tr>
        <td style='font-weight: bold;'><xsl:value-of select='title'/></td>
        <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>
        <td><xsl:value-of select='compatible'/></td>
      </tr>
      </xsl:for-each>
    </tbody>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
";

// XML data to transform
string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
  <cd>
    <title>IronPDF</title>
    <feature>Generate, format and manipulate PDFs</feature>
    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>
  </cd>
  <cd>
    <title>IronOCR</title>
    <feature>OCR engine, input, result</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
  <cd>
    <title>IronBarcode</title>
    <feature>Format, read and write Barcode</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
</catalog>
";

// Create an instance of XslCompiledTransform
XslCompiledTransform transform = new XslCompiledTransform();

// Load the XSLT from a string
using (XmlReader reader = XmlReader.Create(new StringReader(xslt)))
{
    transform.Load(reader);
}

// Transform the XML to HTML
StringWriter results = new StringWriter();
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
    transform.Transform(reader, null, results);
}

// Create a renderer for converting HTML to PDF
IronPdf.ChromePdfRenderer renderer = new IronPdf.ChromePdfRenderer();
// Options, headers, and footers may be set here if needed
// Render our XML as a PDF via XSLT transformation
renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
' XSLT template that defines the transformation from XML to HTML
Dim xslt As String = "<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<html>
<style>
td{
  text-align: center;
  padding: 20px;
  border: 1px solid #CDE7F0;
}
th{
  color: white;
  padding: 20px;
}
</style>
<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>
  <table style='border-collapse: collapse;'>
    <thead>
      <tr>
        <th colspan='3'>
          <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr bgcolor='#9acd32'>
        <th bgcolor='#32ab90'>Title</th>
        <th bgcolor='#f49400'>Feature</th>
        <th bgcolor='#2a95d5'>Compatible</th>
      </tr>
      <xsl:for-each select='catalog/cd'>
      <tr>
        <td style='font-weight: bold;'><xsl:value-of select='title'/></td>
        <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>
        <td><xsl:value-of select='compatible'/></td>
      </tr>
      </xsl:for-each>
    </tbody>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
"

' XML data to transform
Dim xml As String = "<?xml version='1.0' encoding='UTF-8'?>
<catalog>
  <cd>
    <title>IronPDF</title>
    <feature>Generate, format and manipulate PDFs</feature>
    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>
  </cd>
  <cd>
    <title>IronOCR</title>
    <feature>OCR engine, input, result</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
  <cd>
    <title>IronBarcode</title>
    <feature>Format, read and write Barcode</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
</catalog>
"

' Create an instance of XslCompiledTransform
Dim transform As New XslCompiledTransform()

' Load the XSLT from a string
Using reader As XmlReader = XmlReader.Create(New StringReader(xslt))
	transform.Load(reader)
End Using

' Transform the XML to HTML
Dim results As New StringWriter()
Using reader As XmlReader = XmlReader.Create(New StringReader(xml))
	transform.Transform(reader, Nothing, results)
End Using

' Create a renderer for converting HTML to PDF
Dim renderer As New IronPdf.ChromePdfRenderer()
' Options, headers, and footers may be set here if needed
' Render our XML as a PDF via XSLT transformation
renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf")
$vbLabelText   $csharpLabel

Advanced XML to PDF Conversion with Custom Rendering Options

Production applications often need more control over PDF output. IronPDF provides extensive rendering options to customize the final document. This enhanced example demonstrates applying professional formatting, headers, footers, and page settings:

// Configure advanced rendering options
var renderer = new ChromePdfRenderer();

// Set custom paper size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginBottom = 40;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;

// Add professional headers and footers
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    Height = 25,
    HtmlFragment = "<div style='text-align: center; font-size: 12px;'>Product Catalog - {date}</div>",
    DrawDividerLine = true
};

renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    Height = 25,
    HtmlFragment = "<div style='text-align: center; font-size: 10px;'>Page {page} of {total-pages}</div>",
    DrawDividerLine = true
};

// Enable JavaScript execution for dynamic content
renderer.RenderingOptions.EnableJavaScript = true;

// Set print media type for better formatting
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;

// Apply the transformation and render
renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("ProfessionalCatalog.pdf");
// Configure advanced rendering options
var renderer = new ChromePdfRenderer();

// Set custom paper size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginBottom = 40;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;

// Add professional headers and footers
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    Height = 25,
    HtmlFragment = "<div style='text-align: center; font-size: 12px;'>Product Catalog - {date}</div>",
    DrawDividerLine = true
};

renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    Height = 25,
    HtmlFragment = "<div style='text-align: center; font-size: 10px;'>Page {page} of {total-pages}</div>",
    DrawDividerLine = true
};

// Enable JavaScript execution for dynamic content
renderer.RenderingOptions.EnableJavaScript = true;

// Set print media type for better formatting
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;

// Apply the transformation and render
renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("ProfessionalCatalog.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Working with Complex XML Schemas

When dealing with complex XML structures, XSLT templates must handle nested elements, attributes, and conditional formatting. Apply watermarks and add page numbers to enhance document professionalism. Consider using XSLT parameters to make templates more flexible:

// Create XSLT argument list for dynamic parameters
XsltArgumentList args = new XsltArgumentList();
args.AddParam("companyName", "", "Your Company Name");
args.AddParam("generatedDate", "", DateTime.Now.ToString("yyyy-MM-dd"));

// Apply transformation with parameters
transform.Transform(reader, args, results);
// Create XSLT argument list for dynamic parameters
XsltArgumentList args = new XsltArgumentList();
args.AddParam("companyName", "", "Your Company Name");
args.AddParam("generatedDate", "", DateTime.Now.ToString("yyyy-MM-dd"));

// Apply transformation with parameters
transform.Transform(reader, args, results);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Infographic

XML to HTML transformation using XSLT showing catalog data converted to tabular format

Frequently Asked Questions

How do I convert XML to PDF in C#?

To convert XML to PDF in C#, use XSLT transformations to first convert XML to HTML, then render the HTML as PDF with IronPDF. Load your XSLT template using XslCompiledTransform, apply it to your XML data to generate HTML, and then use IronPDF's ChromePdfRenderer to create the final PDF document.

Why should I use XSLT for XML to PDF conversion?

XSLT provides the most flexible approach for XML to PDF conversion as it acts as a custom translator from XML to HTML. Combined with IronPDF's Chrome rendering engine, this method ensures pixel-perfect conversion while maintaining full control over how your XML data is formatted in the final PDF document.

What are the steps to implement XML to PDF conversion?

The implementation involves 5 steps: 1) Install IronPDF C# library, 2) Load your XSLT template using the Load method, 3) Transform XML to HTML using the Transform method, 4) Render HTML to PDF with IronPDF's custom rendering options, and 5) Export the PDF document to your desired location.

Can I apply custom styling to my XML to PDF conversion?

Yes, you can apply custom CSS styling to enhance the visual presentation of your PDF output. IronPDF supports responsive CSS styling, allowing you to create professionally formatted PDFs from your XML data with complete control over fonts, layouts, and visual elements.

Is it possible to convert XML to PDF in a single line of code?

Yes, IronPDF enables one-line XML to PDF conversion by chaining methods: new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf(XslCompiledTransform.Load("template.xslt").Transform(XmlReader.Create("data.xml"), new StringWriter()).ToString()).SaveAs("output.pdf");

What rendering engine is used for XML to PDF conversion?

IronPDF uses a Chrome rendering engine for HTML to PDF conversion, ensuring pixel-perfect rendering and compatibility with modern web standards. This engine handles all complexities of creating properly formatted PDF documents from your transformed XML data.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit
Ready to Get Started?
Nuget Downloads 16,585,857 | Version: 2025.12 just released