PDF processing control Aspose.PDF function demo: convert XML to PDF

Aspose.PDF  is an advanced PDF processing API that can easily generate, modify, convert, render, protect and print documents in cross-platform applications. No need to use Adobe Acrobat. Additionally, the API provides compression options, table creation and manipulation, graphics and image functionality, extensive hyperlink functionality, stamp and watermark tasks, extended security controls, and custom font handling.

Aspose API supports popular file format processing and allows exporting or converting various types of documents to fixed layout file formats and most commonly used image/multimedia formats.

Aspose.pdf latest download (qun: 761297826) icon-default.png?t=N6B9https://www.evget.com/product/4118/download

Aspose.PDF for ASP.NET API has extensive support for XML conversion functionality, as it contains many related enhancements as requested by API users to boost .NET applications with efficient functionality to export XML to PDF.

In this article, we will convert an XML file to PDF using C#. Consider the following use case related to XML transformation using C#:

  • Convert XML to PDF
  • Use HTML in XML and convert to PDF
  • Convert XML and XSLT to PDF

Currently, the .NET version of Aspose.PDF is upgraded to v20.6, which optimizes the problem of lost pages and fixes some problems when converting PDF to HTML. Interested friends can click the button below to download the latest version.

Convert XML to PDF

To convert XML to PDF, you need to follow Aspose.PDF for .NET API's XML Schema, which is an XSD file. Below is an XML file that we convert to PDF as a Hello World demo.

   This is Html String. ]]>   

The following steps need to be followed to convert XML to PDF files:

  • Initialize the object of the Document class
  • Load the XML file using the BindXml method
  • Save the converted PDF file

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

// Instantiate Document object
Document doc = new Document();
// Bind source XML file
doc.BindXml(dataDir + "XML.xml");
// Convert XML to PDF
doc.Save(dataDir + "XMLToPDF.pdf");

Use HTML in XML and convert to PDF

Sometimes it may be necessary to first convert the HTML in the XML to HTML. Aspose.PDF for .NET API also supports this feature. However, HTML and XML tags are very similar. Therefore, CDATA tags need to be specified so that HTML is not parsed as XML tags. The sample XML file below includes HTML expressed in CDATA to avoid any exceptions:

  
    
      Hello
    
 
    
      World!
    
  

Convert this XML file to PDF with the following steps:

  • Instantiate an object of the Document class
  • Load the input XML file
  • Save the output PDF file

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

// Instantiate Document object
Document doc = new Document();
// Bind source XML file
doc.BindXml(dataDir + "XML.xml");
// Convert XML to PDF
doc.Save(dataDir + "XMLToPDF.pdf");

Convert XML and XSLT to PDF

Sometimes, you may already have an XML file containing important application data and want to use that XML file to generate a PDF report. In this case, an XSLT file can be created to transform an existing XML document into an Aspose.PDF compatible XML document. Then, you can go ahead and convert the XML to PDF. Let's learn this with a simple and basic example:

  Hello World!

We will notice that this XML file does not follow the XML schema of the Aspose.PDF for .NET API. However, the XSLT file transforms it to the required compatibility. Now, such XML can be converted to PDF using XSLT as follows:

  • Initialize PDF document
  • Binding XML and XSLT files
  • Save the output PDF document

The code snippet below is based on the following steps that show how to convert XML to PDF in C#:

//Create pdf document
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
//Bind XML and XSLT files to the document
try
{
    pdf.BindXml(dataDir + "\\HelloWorld.xml", dataDir + "\\HelloWorld.xslt");
}
catch (System.Exception)
{

    throw;
}
            
//Save the document
 pdf.Save(dataDir + "HelloWorldUsingXmlAndXslt.pdf");

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/131801666