XPS (Open XML Paper Specification) and OXPS (OpenXPS) represent fixed-page documents. XPS documents are similar to PDF documents in that they both preserve the layout and formatting of the original document. However, XPS documents are not as widely supported as PDF documents. PDF (Portable Document Format) documents are widely supported across a variety of platforms and devices. In this article, we will show you how to convert XPS or OXPS to PDF in C#.
This article covers the following topics:
- C# API to Convert XPS to PDF
- Convert XPS to PDF using C#
- Convert Specific Pages of XPS to PDF
- OXPS to PDF Converter in C#
- Convert Specific Pages of OXPS to PDF
- Convert XPS File to PDF Online
- XPS to PDF - Free Learning Resources
C# API to Convert XPS to PDF
We will use the Aspose.Page for .NET API to convert an XPS or OXPS document to PDF format. It is a powerful API that enables developers to create, edit, manipulate, and convert XPS and EPS/PS documents. Aspose.Page for .NET is a reliable and standalone API that is easy to use and integrate into your .NET applications.
Please download the DLL of the API or install it using the following NuGet command:
PM> Install-Package Aspose.Page
Convert XPS to PDF using C#
We can convert a whole XPS document to PDF format by following the steps below:
- Load the input XPS file.
- Initialize the options object with the necessary parameters.
- Create an instance of the PdfDevice for rendering.
- Export XPS to a PDF document.
The code sample given below shows how to convert an XPS file to a PDF using C#.
Convert Specific Pages of XPS to PDF in C#
We can also convert selected pages of an XPS document to PDF format by following the steps below:
- Initialize the XPS input stream.
- Load an XPS document from the stream.
- Initialize the PdfSaveOptions object.
- Specify page numbers for conversion.
- Save the document as a PDF file.
The following code sample shows how to convert specific pages of an XPS to PDF in C#.
This code snippet uses an XPS document containing several pages as the input file. Whereas, only page numbers 1 and 3 are converted to PDF as specified in the code snippet. The following screenshot displays two pages rendered as a PDF document:
OXPS to PDF Converter in C#
The OXPS format is an updated and advanced form of the XPS file format. However, such files are not supported by some old operating systems. We can easily convert an OXPS document to PDF format by following the steps below:
- Initialize the OXPS input stream.
- Load the OXPS file from the stream.
- Instantiate an object of the PdfSaveOptions class
- Export OXPS to a PDF file
The following code sample shows how to convert OXPS to PDF in C#.
Convert Specific Pages of OXPS to PDF in C#
Similarly, we can also convert specific pages from an OXPS document to PDF format by following the steps below:
- Load an OXPS file
- Declare the PdfSaveOptions object.
- Set the page number(s) that you want to convert.
- Render OXPS to PDF.
The following code snippet shows how to convert specific pages of an OXPS to PDF in C#. It converts the first page of the OXPS file to PDF, as mentioned in the code snippet.
Get a Free License
You can get a free temporary license to test the library without any evaluation restrictions.
Convert XPS File to PDF Online
You can also convert an XPS file to PDF online using this free XPS to PDF converter app without installing any software or plugin.
Convert XPS Document to PDF - Learning Resources
Besides converting XPS or OXPS documents into PDF format, learn more about the library and explore various other features using the resources below:
Conclusion
In this article, we have learned how to programmatically convert XPS to PDF and OXPS to PDF using C#. We have also seen how to export specific pages of an XPS or OXPS document into PDF format. You can further explore the API and optimize your document-handling workflow. In case of any ambiguity, please feel free to contact us on our free support forum.