Category / Section
How to convert XAML to PDF using C# and VB.NET
3 mins read
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can convert XAML to PDF in C# and VB.NET by using following steps:
- Convert XAML to XPS
- Convert XPS to PDF
Steps to convert XAML to PDF programmatically:
- Create a new Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Add the following system assemblies as reference to your application.
- PresentationCore.dll
- PresentationFramework.dll
- ReachFramework.dll
- System.Printing.dll
- Include the following namespace in the Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using System.Windows; using System.Windows.Xps; using System.Windows.Documents; using System.Windows.Xps.Packaging;
VB.NET
Imports Syncfusion.Pdf Imports System.Windows Imports System.Windows.Xps Imports System.Windows.Documents Imports System.Windows.Xps.Packaging
- Add the following code snippet to convert XAML document to PDF.
C#
private void button_Click(object sender, System.EventArgs e) { //Input XAML file location string fileName = "input.xaml"; //Convert XAML file to XPS file Stream xpsFile = GetXPSDocument(fileName); if (xpsFile != null) { xpsFile.Position = 0; //Initialize XPSToPdfConverter Syncfusion.XPS.XPSToPdfConverter converter = new Syncfusion.XPS.XPSToPdfConverter(); //Convert XPS document into PDF document PdfDocument document = converter.Convert(xpsFile); //Save the Pdf document document.Save("XAMLToPDF.pdf"); //Close the Pdf document document.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("XAMLToPDF.pdf"); } } /// <summary> /// Convert XAML flow document into XPS file /// </summary> private MemoryStream GetXPSDocument(string fileName) { //Create visual UIElement UIElement visual = System.Windows.Markup.XamlReader.Load(System.Xml.XmlReader.Create(fileName)) as System.Windows.UIElement; FixedDocument doc = new FixedDocument(); PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); //Create first page of document fixedPage.Children.Add(visual); ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage); //Adding page content to pages doc.Pages.Add(pageContent); //Create the stream MemoryStream xpsStream = new MemoryStream(); XpsDocument xpsDocument = new XpsDocument(System.IO.Packaging.Package.Open(xpsStream, FileMode.Create)); XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument); //Write the XPS document xpsDocumentWriter.Write(doc); //Close the XPS document xpsDocument.Close(); return xpsStream; }
VB.NET
Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button.Click 'Input XAML file location Dim fileName As String = "input.xaml" 'Convert XAML file to XPS file Dim xpsFile As Stream = GetXPSDocument(fileName) If xpsFile IsNot Nothing Then xpsFile.Position = 0 'Initialize XPSToPdfConverter Dim converter As New Syncfusion.XPS.XPSToPdfConverter() 'Convert XPS document into PDF document Dim document As PdfDocument = converter.Convert(xpsFile) 'Save the Pdf document document.Save("XAMLToPDF.pdf") 'Close the Pdf document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("XAMLToPDF.pdf") End If End Sub ''' <summary> ''' Convert XAML flow document into XPS file ''' </summary> Private Function GetXPSDocument(fileName As String) As MemoryStream 'Create visual UIElement Dim visual As UIElement = TryCast(System.Windows.Markup.XamlReader.Load(System.Xml.XmlReader.Create(fileName)), UIElement) Dim doc As New FixedDocument() Dim pageContent As New PageContent() Dim fixedPage As New FixedPage() 'Create first page of document fixedPage.Children.Add(visual) DirectCast(pageContent, System.Windows.Markup.IAddChild).AddChild(fixedPage) 'Adding page content to pages doc.Pages.Add(pageContent) 'Create the stream Dim xpsStream As New MemoryStream() Dim xpsDocument As New XpsDocument(System.IO.Packaging.Package.Open(xpsStream, FileMode.Create)) Dim xpsDocumentWriter As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument) 'Write the XPS document xpsDocumentWriter.Write(doc) 'Close the XPS document xpsDocument.Close() Return xpsStream End Function
A complete working sample can be downloaded from XAMLToPDFSample.zip.
By executing the program, you will get the PDF document as follows.
Refer here to explore the rich set of Syncfusion Essential PDF features.
See Also:
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.