IRONSOFTWAREHOME

How to Convert XML to PDF in IronPDF C#

Curtis Chau
Curtis Chau
Updated: August 2, 2026

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. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2Copy 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");
    C#
  3. 3Deploy 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?

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");
C#

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

Microsoft MVP

View case study

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

Chief Technology Officer, OPYN

View case study

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

Lead Software Engineer, Agorus Build

View case study

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.

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");
C#

Output

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.

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");
C#

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 does IronPDF convert XML to PDF in C#?

IronPDF converts XML to PDF in C# by first transforming the XML into an HTML format using an XSLT stylesheet. This HTML is then rendered into a PDF using IronPDF's `ChromePdfRenderer.RenderHtmlAsPdf` method.

What role does XSLT play in converting XML to PDF with IronPDF?

XSLT is used to map XML elements to HTML markup, providing a visual layout for the structured data that XML lacks. IronPDF then uses this HTML to render the PDF document.

Can I control the layout and page settings of the PDF generated by IronPDF?

Yes, before calling `RenderHtmlAsPdf`, you can set the renderer's `RenderingOptions` to control features such as margins, orientation, headers, and footers, allowing for detailed customization of the PDF layout.

How do I pass dynamic values to the XSLT during the XML transformation?

You can use an `XsltArgumentList` to pass runtime values into the XSLT, which can be accessed within the stylesheet using `` elements, allowing for conditional formatting or additional data inclusion.

What libraries are necessary for converting XML to PDF using IronPDF?

You need IronPDF for rendering the HTML as a PDF, and .NET libraries like `System.Xml.Xsl.XslCompiledTransform` to handle the XML to HTML transformation.

What should I do if my XML schema is complex?

For complex XML schemas, use `XsltArgumentList` to pass parameters to the XSLT. This allows the stylesheet to handle conditional logic based on schema particulars without altering the C# code.

Is a sample project available to understand XML to PDF conversion using IronPDF?

Yes, a runnable sample project for converting XML to PDF is available for download, providing a starting point for implementing this functionality in your own applications.

What are some common use cases for converting XML to PDF with IronPDF?

Common use cases include generating reports, invoices, catalogs, or any document where structured data needs to be presented in a professional and easily distributable format.

How is the HTML output managed during the XML transformation process?

During the transformation process, HTML output is typically managed using a `StringWriter` and an `XmlWriter`, which convert the XML based on the XSLT rules into an in-memory HTML string for further PDF conversion.

Can IronPDF handle pagination features for the generated PDFs?

Yes, IronPDF supports pagination features such as adding page numbers and footers through its rendering options, providing flexibility in creating structured, multi-page PDF documents.

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

Ready to Get Started?

Nuget Downloads 20,809,720Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required