new IronPdf.ChromePdfRenderer()
.RenderHtmlAsPdf(
XslCompiledTransform.Load("template.xslt")
.Transform(XmlReader.Create("data.xml"), new StringWriter())
.ToString()
)
.SaveAs("output.pdf");
Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
// 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
class="tutorial-segment-title">信息圖
class="center-image-wrapper">
常見問題解答
如何在C#中將XML轉換為PDF?
您可以使用 C# 將 XML 轉換為 PDF。首先,使用 XSLT 作為轉換模板,將 XML 資料轉換為 HTML。然後,使用像 IronPDF 這樣的 .NET PDF 產生器將 HTML 渲染成 PDF 文件。
在 XML 轉 PDF 轉換中使用 XSLT 的目的為何?
XSLT 用於將 XML 資料轉換為精確的 HTML 表示形式。它充當自訂轉換器,確保在轉換為 PDF 的過程中保留資料結構和樣式。
如何使用 XSLT 將 XML 轉換為 HTML?
若要使用 XSLT 將 XML 轉換為 HTML,您需要:1) 使用Load方法載入 XSLT 模板,2) 使用Transform方法將 XML 資料轉換為 HTML,以及 3) 使用 IronPDF 等函式庫將此 HTML 渲染為 PDF。
如何使用 IronPDF 進行 XML 到 PDF 的轉換?
首先,使用 XSLT 將 XML 轉換為 HTML。然後,使用 IronPDF 的RenderHtmlAsPdf方法從 HTML 建立 PDF。最後,使用SaveAs等方法儲存 PDF。
將 XML 轉換為 PDF 時,我可以自訂 PDF 輸出嗎?
是的,您可以使用 IronPDF 在 HTML 到 PDF 的渲染過程中套用自訂選項(例如頁首、頁尾和樣式)來客製化 PDF 輸出。
是否有 XML 轉 PDF 的範例項目?
是的,我們提供了一個示範 XML 轉 PDF 的範例專案供您下載。它包含範例 XML 資料、XSLT 範本以及 IronPDF 的使用說明。
哪些平台支援使用 IronPDF 進行 XML 到 PDF 的轉換?
IronPDF 可在多個平台上使用,包括 Microsoft Windows、Linux(Debian、CentOS、Ubuntu)、MacOS、Docker(Windows、Linux、Azure)、Azure(VPS、Webapps、Websites、Functions)和 AWS。
如何安裝用於 XML 轉 PDF 的 .NET PDF 生成器庫?
您可以透過造訪 NuGet 上的 IronPdf 套件頁面並按照那裡提供的安裝說明來安裝 .NET PDF 產生器庫。
IronPDF 將 XML 轉換為 PDF 時是否完全相容於 .NET 10?
是的,IronPDF 與 .NET 10 完全相容。它的跨平台功能支援 .NET 10,因此您可以在面向 .NET 10 的專案中使用 IronPDF 的 HTML 渲染、HTML 到 PDF 轉換和完整功能集,而無需額外的配置或相容性問題。