How to Convert XML to PDF in C# and VB.Net ?

C# XML to PDF directly can be a complex challenge. We've found that to convert XML to PDF in C# it is best to start with XSLT. XML may be rendered to PDF via HTML(5) using XLST transformations. These documents define how XML from a given schema may be converted to an accurate HTML representation and are a well established standard.

https://docs.microsoft.com/en-us/dotnet/standard/data/xml/using-the-xslcompiledtransform-class

The resultant HTML string or file may then be rendered as a PDF using IronPDF:


    XslCompiledTransform transform = new XslCompiledTransform();

    using(XmlReader reader = XmlReader.Create(new StringReader(XSLT))) {

        transform.Load(reader);

    }

    StringWriter results = new StringWriter();

    using(XmlReader reader = XmlReader.Create(new StringReader(XML))) {

        transform.Transform(reader, null, results);

    }

IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

// options, headers and footers may be set there

// Render our XML as a PDF via XSLT

Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
Dim transform As New XslCompiledTransform()

	Using reader As XmlReader = XmlReader.Create(New StringReader(XSLT))

		transform.Load(reader)

	End Using

	Dim results As New StringWriter()

	Using reader As XmlReader = XmlReader.Create(New StringReader(XML))

		transform.Transform(reader, Nothing, results)

	End Using

Dim Renderer As New IronPdf.HtmlToPdf()

' options, headers and footers may be set there

' Render our XML as a PDF via XSLT

Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf")
VB   C#