Articles in this section
Category / Section

How to convert XAML to PDF in WPF platform

1 min read

Essential PDF allows to convert a XAML document into PDF.

We can achieve XAML document to PDF by following steps

  • Convert XAML to XPS
  • Convert XPS to PDF

While, converting a XAML document to PDF, the following assemblies are needed to be add reference to your application.

Syncfusion assemblies:

  • Syncfusion.Pdf.Base.dll
  • Syncfusion.Compression.Base.dll

System assemblies:

  • ReachFramework.dll
  • System.priniting.dll

Please refer the below code snippet to convert XAML document to PDF.

C#:

private void button_Click(object sender, RoutedEventArgs e)
   {
       //Input XAML file location
       string fileName = "../../Data/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("output.pdf");
 
                //Open the Pdf document
                System.Diagnostics.Process.Start("output.pdf");
 
                //Close the Pdf document.
                document.Close(true);
            }
    }
 
/// <summary>
/// Convert the XAML flow document into XPS file
/// </summary>
/// <param name="fileName">Input (XAML) document path</param>
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 System.Windows.Documents.FixedDocument();
     PageContent pageContent = new System.Windows.Documents.PageContent();
     FixedPage fixedPage = new System.Windows.Documents.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 stream = new MemoryStream();
 
     XpsDocument xpsdocument = new XpsDocument(System.IO.Packaging.Package.Open(stream, FileMode.Create));
     XpsDocumentWriter xpswriter = XpsDocument.CreateXpsDocumentWriter(xpsdocument);
 
     //Write the XPS document.
     xpswriter.Write(doc);
 
     //Close the XPS document.
     xpsdocument.Close();
 
     return stream;
}

 

Sample Link:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/WpfApplication1-954981644.zip

 

 

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