C# Convert XPS to PDF | Convert OXPS to PDF in C#

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:

  1. C# API to Convert XPS to PDF
  2. Convert XPS to PDF using C#
  3. Convert Specific Pages of XPS to PDF
  4. OXPS to PDF Converter in C#
  5. Convert Specific Pages of OXPS to PDF
  6. Convert XPS File to PDF Online
  7. 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:

  1. Load the input XPS file.
  2. Initialize the options object with the necessary parameters.
  3. Create an instance of the PdfDevice for rendering.
  4. 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:

  1. Initialize the XPS input stream.
  2. Load an XPS document from the stream.
  3. Initialize the PdfSaveOptions object.
  4. Specify page numbers for conversion.
  5. 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:

XPS to PDF

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:

  1. Initialize the OXPS input stream.
  2. Load the OXPS file from the stream.
  3. Instantiate an object of the PdfSaveOptions class
  4. 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:

  1. Load an OXPS file
  2. Declare the PdfSaveOptions object.
  3. Set the page number(s) that you want to convert.
  4. 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.

See Also