How to Convert XML to PDF Using C#

How to Convert XML to PDF in IronPDF C#

IronPDF converts XML to PDF in C# by running an XSLT transformation that turns the XML into HTML, then rendering that HTML with ChromePdfRenderer.RenderHtmlAsPdf. IronPDF has no native XML renderer, so the supported path is always XML to XSLT/HTML to PDF.

XML holds structured data without any visual layout, so it cannot be drawn on a page on its own. An XSLT stylesheet supplies that presentation: it maps each XML element onto HTML markup, and that HTML carries the fonts, tables, and styling the PDF will show. To read more about the underlying transform, see Microsoft's guide on the XslCompiledTransform class.

Quickstart: Convert XML to PDF with IronPDF

Load an XSLT stylesheet with XslCompiledTransform, transform your XML file into an HTML string, then hand that string to IronPDF. The conversion runs through IronPDF's HTML to PDF engine, which renders the layout the stylesheet defines.

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf
  2. Copy and run this code snippet.

    var transform = new System.Xml.Xsl.XslCompiledTransform();
    transform.Load("catalog.xslt");
    var html = new System.IO.StringWriter();
    transform.Transform("catalog.xml", null, html);
    new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf(html.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 Convert an XML File to PDF in C#?

Convert an XML file by chaining three steps: load an XSLT stylesheet with XslCompiledTransform, transform the XML into HTML, then call RenderHtmlAsPdf on the result. The stylesheet decides how each XML element appears, and IronPDF renders the HTML it produces through the Chromium engine.

The transform writes its HTML into a StringBuilder through an XmlWriter, which keeps the whole document in memory and avoids a temporary file on disk. That HTML string is what you pass to the renderer. Once you hold the resulting PdfDocument, you can keep working on it, such as rendering an HTML string for other parts of the same report.

What Does the XML Input Look Like?

The sample below is a product catalog. Download the catalog.xml sample file and the matching catalog.xslt stylesheet to follow along.

Input

What Code Do I Need to Convert an XML File?

:path=/static-assets/pdf/content-code-examples/how-to/xml-to-pdf-from-file.cs
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using IronPdf;

// Load the XSLT stylesheet that maps the XML schema onto HTML
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("catalog.xslt");

// Apply the transform to the XML file and capture the HTML output
StringBuilder htmlBuilder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };

using (XmlReader reader = XmlReader.Create("catalog.xml"))
using (XmlWriter writer = XmlWriter.Create(htmlBuilder, settings))
{
    transform.Transform(reader, null, writer);
}

string html = htmlBuilder.ToString();

// Render the resulting HTML as a PDF with IronPDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("xml-to-pdf.pdf");
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Xsl
Imports IronPdf

' Load the XSLT stylesheet that maps the XML schema onto HTML
Dim transform As New XslCompiledTransform()
transform.Load("catalog.xslt")

' Apply the transform to the XML file and capture the HTML output
Dim htmlBuilder As New StringBuilder()
Dim settings As New XmlWriterSettings With {.OmitXmlDeclaration = True, .Indent = True}

Using reader As XmlReader = XmlReader.Create("catalog.xml")
    Using writer As XmlWriter = XmlWriter.Create(htmlBuilder, settings)
        transform.Transform(reader, Nothing, writer)
    End Using
End Using

Dim html As String = htmlBuilder.ToString()

' Render the resulting HTML as a PDF with IronPDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("xml-to-pdf.pdf")
$vbLabelText   $csharpLabel

Output

How Can I Control the PDF Layout and Page Settings?

Set the renderer's RenderingOptions before calling RenderHtmlAsPdf to control margins, orientation, headers, and footers. These options apply to the HTML the stylesheet produced, so page-level concerns stay in C# while element-level styling stays in the XSLT and its CSS.

The example below keeps portrait orientation, trims the top and bottom margins, and adds a running header and a page-number footer. For the full set of toggles, see the guide on rendering options, and to add per-page numbering see page numbers.

:path=/static-assets/pdf/content-code-examples/how-to/xml-to-pdf-advanced.cs
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using IronPdf;
using IronPdf.Rendering;

// Transform the XML into HTML using the XSLT stylesheet
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("catalog.xslt");

StringBuilder htmlBuilder = new StringBuilder();
using (XmlReader reader = XmlReader.Create("catalog.xml"))
using (XmlWriter writer = XmlWriter.Create(htmlBuilder, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
    transform.Transform(reader, null, writer);
}

// Configure rendering options before generating the PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
renderer.RenderingOptions.MarginTop = 20;
renderer.RenderingOptions.MarginBottom = 20;
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
    CenterText = "Spring Product Catalog",
    FontSize = 12
};
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
    RightText = "Page {page} of {total-pages}",
    FontSize = 10
};

PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlBuilder.ToString());
pdf.SaveAs("xml-to-pdf-advanced.pdf");
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Xsl
Imports IronPdf
Imports IronPdf.Rendering

' Transform the XML into HTML using the XSLT stylesheet
Dim transform As New XslCompiledTransform()
transform.Load("catalog.xslt")

Dim htmlBuilder As New StringBuilder()
Using reader As XmlReader = XmlReader.Create("catalog.xml")
    Using writer As XmlWriter = XmlWriter.Create(htmlBuilder, New XmlWriterSettings With {.OmitXmlDeclaration = True})
        transform.Transform(reader, Nothing, writer)
    End Using
End Using

' Configure rendering options before generating the PDF
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait
renderer.RenderingOptions.MarginTop = 20
renderer.RenderingOptions.MarginBottom = 20
renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {
    .CenterText = "Spring Product Catalog",
    .FontSize = 12
}
renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
    .RightText = "Page {page} of {total-pages}",
    .FontSize = 10
}

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlBuilder.ToString())
pdf.SaveAs("xml-to-pdf-advanced.pdf")
$vbLabelText   $csharpLabel

Output

Icon Quote related to Output

My favorite library of this kind is IronPDF. It allows for fast and efficient manipulation of PDF files. It also has many valuable features, like exporting to PDF/A format and digitally signing PDF documents.

Milan Jovanovic related to Output

Milan Jovanovic

Microsoft MVP

View case study
Icon Quote related to Output

IronOCR means we can save $40,000 annually from manual processing, while enhancing productivity and freeing up resources for high-impact tasks. I would highly recommend it.

Brent Matzelle related to Output

Brent Matzelle

Chief Technology Officer, OPYN

View case study
Icon Quote related to Output

The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.

David Jones related to Output

David Jones

Lead Software Engineer, Agorus Build

View case study

How Do I Handle Complex XML Schemas?

Pass runtime values into the stylesheet with an XsltArgumentList when the XML schema needs conditional formatting or data that lives outside the document. The stylesheet reads each value through an <xsl:param>, so the same template can flag low stock, stamp a report date, or branch on element values without any change to the C# code.

This pattern keeps the transformation logic in the XSLT, where nested elements, attributes, and <xsl:choose> branches handle the structure. Download the catalog-params.xslt stylesheet to see the parameter and conditional markup it uses.

:path=/static-assets/pdf/content-code-examples/how-to/xml-to-pdf-parameters.cs
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using IronPdf;

// Pass runtime values into the stylesheet with an XsltArgumentList
XsltArgumentList xsltArgs = new XsltArgumentList();
xsltArgs.AddParam("reportDate", "", DateTime.Now.ToString("MMMM d, yyyy"));
xsltArgs.AddParam("minStock", "", 50);

// Load a parameterized stylesheet that reads <xsl:param> values
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("catalog-params.xslt");

StringBuilder htmlBuilder = new StringBuilder();
using (XmlReader reader = XmlReader.Create("catalog.xml"))
using (XmlWriter writer = XmlWriter.Create(htmlBuilder, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
    transform.Transform(reader, xsltArgs, writer);
}

// Render the parameterized HTML as a PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlBuilder.ToString());
pdf.SaveAs("xml-to-pdf-parameters.pdf");
Imports System
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Xsl
Imports IronPdf

' Pass runtime values into the stylesheet with an XsltArgumentList
Dim xsltArgs As New XsltArgumentList()
xsltArgs.AddParam("reportDate", "", DateTime.Now.ToString("MMMM d, yyyy"))
xsltArgs.AddParam("minStock", "", 50)

' Load a parameterized stylesheet that reads <xsl:param> values
Dim transform As New XslCompiledTransform()
transform.Load("catalog-params.xslt")

Dim htmlBuilder As New StringBuilder()
Using reader As XmlReader = XmlReader.Create("catalog.xml")
    Using writer As XmlWriter = XmlWriter.Create(htmlBuilder, New XmlWriterSettings With {.OmitXmlDeclaration = True})
        transform.Transform(reader, xsltArgs, writer)
    End Using
End Using

' Render the parameterized HTML as a PDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlBuilder.ToString())
pdf.SaveAs("xml-to-pdf-parameters.pdf")
$vbLabelText   $csharpLabel

Output


Infographic

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

Conclusion

You converted XML to PDF by transforming it into HTML with XslCompiledTransform, then rendering that HTML through IronPDF, and you saw how RenderingOptions and XsltArgumentList extend the result for layout control and schema-driven formatting. Because the output is ordinary HTML, the same rendering options that drive headers, footers, and paper sizes elsewhere in IronPDF apply directly to your catalog. A runnable XML to PDF sample project is available to download as a starting point.

Frequently Asked Questions

How do I convert XML to PDF in C#?

IronPDF has no native XML renderer, so the supported path is XML to XSLT/HTML to PDF. Load an XSLT stylesheet with XslCompiledTransform, transform your XML into an HTML string, then call RenderHtmlAsPdf on IronPDF's ChromePdfRenderer to produce 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 renders the HTML produced by your XSLT transform using a Chromium rendering engine, giving pixel-perfect output and compatibility with modern web standards. XML itself is never rendered directly; it is the transformed HTML that becomes the PDF.

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 19,680,294 | Version: 2026.7 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast? PM > Install-Package IronPdf
run a sample watch your HTML become a PDF.