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
- 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
- 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:
- Load the input XPS file.
- Initialize the options object with the necessary parameters.
- Create an instance of the PdfDevice for rendering.
- 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:
- Initialize the XPS input stream.
- Load the XPS document from the stream.
- Create an instance of the PdfSaveOptions object.
- Specify the page numbers for conversion.
- 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:
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:
- Initialize the OXPS input stream.
- Load the OXPS file from the stream.
- Create an instance of the PdfSaveOptions class.
- 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:
- Load the OXPS file.
- Declare the PdfSaveOptions object.
- Specify the page number(s) you wish to convert.
- 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.