Articles in this section
Category / Section

How to convert XML to PDF in C# and VB.NET?

5 mins read

The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, texts, fonts, and the layout of the original HTML document or webpage

.

This sample explains how to convert an XML file to PDF using an IE-based HTML converter in C# and VB.NET.

Steps to convert the XML file to PDF programmatically in C#:

  1. Create a new C# console application project. Create a console application in visual studio in WinForms PDF
  2. Install the Syncfusion.HtmlToPdfConverter.IE.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org. Refer NuGet package to the project in WinForms PDF
  3. Include the following namespaces in Program.cs file.

C#

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
using System.Threading;

 

VB.NET

Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf
Imports System.IO

 

  1. Include the following code sample in the main method of Program.cs file, to overcome the ThreadStateException during conversion.

C#

Thread thread = new Thread(ConvertToPDF);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

 

Refer to here to know more about ThreadStateException.

  1. You convert an XML file to PDF using the Convert method of HTMLToPdfConverter class. Use the following code sample for conversion.

C#

//Initialize the HtmlToPdfConverter with IE rendering engine
HtmlToPdfConverter htmlToPDFConverter = new HtmlToPdfConverter(HtmlRenderingEngine.IE);
 
//Initialize IE converter settings
IEConverterSettings IEsettings = new IEConverterSettings();
 
//Set the additional delay for conversion
IEsettings.AdditionalDelay = 1000;
 
//Assign the IEConverterSettings to ConverterSettings of HtmlToPdfConverter
htmlToPDFConverter.ConverterSettings = IEsettings;
 
//Convert the XML file to PDF
PdfDocument document = htmlToPDFConverter.Convert(Path.GetFullPath("../../Input/cdCatalog.xml"));
 
//Save the PDF document
document.Save("Output.pdf");
 
//Close the instance of PdfDocument
document.Close(true);

 

VB.NET

'Initialize the HtmlToPdfConverter with IE rendering engine
Dim htmlToPDFConverter As HtmlToPdfConverter = New HtmlToPdfConverter(HtmlRenderingEngine.IE)
 
'Initialize IE converter settings
Dim IEsettings As IEConverterSettings = New IEConverterSettings
 
'Set the additional delay for conversion
IEsettings.AdditionalDelay = 1000
 
'Assign the IEConverterSettings to ConverterSettings of HtmlToPdfConverter
htmlToPDFConverter.ConverterSettings = IEsettings
 
'Convert the XML file to PDF
Dim document As PdfDocument = htmlToPDFConverter.Convert(Path.GetFullPath("../../Input/cdCatalog.xml"))
 
'Save the PDF document
document.Save("Output.pdf")
 
'Close the instance of PdfDocument
document.Close(True)

 

A complete work sample to convert an XML file to PDF can be downloaded from ConvertXMLtoPDF.zip.

By executing the program, you will get the PDF document as follows.

Xml to pdf output in WinForms PDF

Take a moment to peruse the documentation. You can find other WebKit, Blink, and IE rendering engines supported by the Syncfusion .NET PDF library for converting HTML pages to PDF documents along with the respective customization options and features.

Refer to here to explore a rich set of Syncfusion Essential PDF features.

An online sample link to convert HTML to PDF.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without trail message.

 

Conclusion

I hope you enjoyed learning about how to convert XML to PDF in C# and VB.NET.

You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied