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

XPS (Open XML Paper Specification) and OXPS (OpenXPS) are fixed-page document formats that preserve the layout and formatting of the original content. While XPS documents are similar to PDF (Portable Document Format) files, they are not as widely supported across various platforms and devices. In this article, we will demonstrate how to convert XPS or OXPS to PDF in C#, providing a comprehensive guide for developers looking to enhance their document management capabilities.

Table of Contents

  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. Learning Resources for XPS to PDF Conversion

C# API to Convert XPS to PDF

To achieve efficient XPS to PDF conversion in C#, we will utilize the Aspose.Page for .NET API. This powerful library allows developers to create, edit, manipulate, and convert both XPS and EPS/PS documents seamlessly. Aspose.Page for .NET is a reliable, standalone API that is easy to integrate into your .NET applications.

You can either download the DLL directly or install the API using the following NuGet command:

PM> Install-Package Aspose.Page

Convert XPS to PDF using C#

To convert an entire XPS document to PDF format, follow these steps:

  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 the XPS document to a PDF file.

Here is a code sample demonstrating how to convert an XPS file to a PDF using C#:

Convert Specific Pages of XPS to PDF in C#

If you need to convert specific pages from an XPS document to PDF format, follow these steps:

  1. Initialize the XPS input stream.
  2. Load the XPS document from the stream.
  3. Create an instance of the PdfSaveOptions object.
  4. Specify the page numbers for conversion.
  5. Save the document as a PDF file.

The following code sample illustrates how to convert specific pages of an XPS to PDF in C#:

This snippet uses an XPS document with multiple pages, converting only pages 1 and 3 to PDF, as specified. The screenshot below showcases the two pages rendered as a PDF document:

XPS to PDF

OXPS to PDF Converter in C#

The OXPS format is an advanced version of the XPS file format, yet it may not be supported by older operating systems. Converting an OXPS document to PDF is straightforward:

  1. Initialize the OXPS input stream.
  2. Load the OXPS file from the stream.
  3. Create an instance of the PdfSaveOptions class.
  4. Export the OXPS document to a PDF file.

Here’s a code sample showing how to convert OXPS to PDF in C#:

Convert Specific Pages of OXPS to PDF in C#

You can also convert specific pages from an OXPS document to PDF format. Follow these steps:

  1. Load the OXPS file.
  2. Declare the PdfSaveOptions object.
  3. Specify the page number(s) you wish to convert.
  4. Render the OXPS document to PDF.

The code snippet below demonstrates how to convert specific pages of an OXPS to PDF in C#, focusing on converting the first page:

Get a Free License

You can get a free temporary license to test the Aspose.Page library without any evaluation restrictions.

Learning Resources for XPS to PDF Conversion

Beyond converting XPS or OXPS documents into PDF format, explore the library’s diverse features with these resources:

Conclusion

In this article, we have explored how to programmatically convert XPS to PDF and OXPS to PDF using C#. We also covered how to export specific pages from both XPS and OXPS documents into PDF format. By leveraging the best C# library for XPS to PDF, you can optimize your document-handling workflow. If you have any questions, feel free to reach out on our free support forum.

See Also