XMLをPDFに変換するC#とVB.NET

This article was translated from English: Does it need improvement?
Translated
View the article in English

C#のXMLを直接PDFに変換することは、複雑な課題になることがあります。 XMLをC#でPDFに変換する際、変換テンプレートとしてXSLTを使用するのが最適であることが分かりました。 XMLはHTMLを介してPDFにレンダリングできます(5) XSLT 変換を使用して。 XSLTドキュメントは、特定のスキーマからのXMLを正確なHTML表現に変換する方法を定義しており、確立された標準です。 簡単に言うと、XSLTはXMLからHTMLへのカスタム変換機能を果たします。

「に移動」XslCompiledTransform クラスの使用「XSLT変換について詳しく知るために、Microsoftのこの記事をご覧ください。」

その結果生成されたHTML文字列またはファイルをPDFとしてレンダリングできます。 .NET PDFジェネレーター. このリンクからプロジェクトファイルをダウンロードできます リンク.

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>
";

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>
";

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.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
// options, headers and footers may be set there
// Render our XML as a PDF via XSLT
Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
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>
";

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>
";

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.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
// options, headers and footers may be set there
// Render our XML as a PDF via XSLT
Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
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>
"

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>
"

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.ChromePdfRenderer()
' 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#

インフォグラフィック

XMLからPDFへ