How to convert XAML to PDF in WPF platform?
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
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the 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 a trial message.
Conclusion:
I hope you enjoyed learning about how to convert XAML to PDF in WPF platform.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples 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 explore 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 forums or feedback portal. We are always happy to assist you!