Convert XML to PDF – Easy XML to PDF Conversion

XML to PDF

In this article, we will be converting XML files to PDF using C#. Aspose.PDF for .NET API extensively supports the XML conversion feature because of the inclusions of many related enhancements, as requested by the API users. You can elevate your .NET applications with this efficient feature of exporting XML to PDF. Let us consider following use cases related to XML conversion using C#:

Convert XML to PDF

For converting an XML to PDF, you need to follow the XML Schema of Aspose.PDF for .NET API which is available as XSD file. Following is an XML file that we will be converting to PDF as a Hello World demonstration.

<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
  <Page id="mainPage">
    <TextFragment>
      <TextSegment>Hello</TextSegment>
    </TextFragment>
 
    <TextFragment>
      <TextSegment>World!</TextSegment>
    </TextFragment>
  </Page>
</Document>

You need to follow the steps below to convert XML to PDF file:

  1. Initialize an object of Document Class
  2. Load XML file with BindXml method
  3. Save the converted PDF file

Following code snippet shows how to convert XML to PDF in C#:

Use HTML in XML and Convert to PDF

Sometimes you might need to use HTML in XML prior conversion to HTML. Aspose.PDF for .NET API supports this feature as well. However, HTML and XML tags are quite similar. Therefore, you need to specify the CDATA tag so that the HTML is not parsed as XML markup. Below sample XML file includes HTML denoted by CDATA to avoid any anomaly:

<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
  <Page id="mainSection">
    <HtmlFragment>
      <![CDATA[
        <font style="font-family:Tahoma; font-size:40px;">This is Html String.</font>
        ]]>
    </HtmlFragment>
  </Page>
</Document>

You can convert this XML file to PDF with following steps:

  1. Instantiate Document class object
  2. Load the input XML file
  3. Save the output PDF file

Below code snippet shows how to convert an XML file, containing HTML, to PDF in C#:

Convert XML and XSLT to PDF

Sometimes you may have an existing XML file that contains important application data and you want to generate PDF reports using that XML file. In such scenarios, you can create XSLT file to transform your existing XML document to Aspose.PDF’s compatible XML document. Then you can proceed to convert XML to PDF. Let us learn this with simple and basic example:

<?xml version="1.0" encoding="utf-8" ?>
<Contents>
  <Content>Hello World!</Content>
</Contents>
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text()"/>
    <xsl:template match="/Contents">
    <html>
      <Document xmlns="Aspose.Pdf" IsAutoHyphenated="false">
        <PageInfo>
          <DefaultTextState
                            Font = "Helvetica" FontSize="8" LineSpacing="4"/>
          <Margin Left="5cm" Right="5cm" Top="3cm" Bottom="15cm" />
        </PageInfo>
        <Page id="mainSection">
          <TextFragment>
            <TextSegment>
              <xsl:value-of select="Content"/>
            </TextSegment>
          </TextFragment>
        </Page>
      </Document>
    </html>
</xsl:template>
</xsl:stylesheet>

You can notice that the XML file does not follow the XML schema of Aspose.PDF for .NET API. However, the XSLT file transforms it to required compatibility. Now you can follow the steps below to convert such XML using XSLT to PDF:

  1. Initialize PDF Document
  2. Bind XML and XSLT file
  3. Save the output PDF document

The code snippet below is based on these steps which show how to convert XML to PDF in C#:

Conclusion

We have learned how to convert XML to PDF file provided the XML follows the mentioned schema of the API. Considering the popularity and usability of HTML tags, we have also considered the scenario when you need to include some HTML in the source XML file. Moreover, we have also explored how to convert XML and XSLT to PDF when the existing XML file does not follow the schema.

See Also

Merge Multiple PDF Files into One PDF using C# .NET