This article is written to explain how to convert XPS to PDF in C#. It includes the step-wise procedure as well as a runnable sample code to change XPS to PDF in C#. You can load the source XPS file and export it as a PDF file with a few lines of code.
Steps to Convert XPS to PDF using C#
- Configure the environment by installing Aspose.Page to convert XPS files
- Load the input XPS file using a Stream class object
- Initialize a PdfSaveOptions class object and set the necessary parameters
- Create a rendering device for output PDF format
- Save the output PDF file after converting the XPS file
These steps precisely summarize the process to convert XPS file to PDF in C#. Use the Stream class object to load the input XPS file and then specify different properties for the output PDF document like the image or text compression, etc. Subsequently, create a rendering device like PdfDevice and export the output PDF file.
Code to Convert XPS to PDF in C#
using System.IO; | |
using Aspose.Page.XPS; | |
using Aspose.Page.XPS.Presentation.Pdf; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to extract image from a PDF | |
{ | |
// Load the license | |
Aspose.Page.License lic = new Aspose.Page.License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Initialize PDF output stream | |
using (Stream pdfStream = File.Open("XPStoPDF.pdf", FileMode.OpenOrCreate, FileAccess.Write)) | |
// Initialize XPS input stream | |
using (Stream xpsStream = File.Open("input.xps", FileMode.Open)) | |
{ | |
// Load XPS document | |
XpsDocument XPSdocument = new XpsDocument(xpsStream, new XpsLoadOptions()); | |
// Initialize PdfSaveOptions object | |
PdfSaveOptions options = new PdfSaveOptions() | |
{ | |
JpegQualityLevel = 100, | |
ImageCompression = PdfImageCompression.Jpeg, | |
TextCompression = PdfTextCompression.Flate, | |
PageNumbers = new int[] { 1 } | |
}; | |
// Create rendering device | |
PdfDevice device = new PdfDevice(pdfStream); | |
// Save the output PDF file | |
XPSdocument.Save(device, options); | |
} | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
This sample code can be used to convert XPS document to PDF in C#. Moreover, you can change the value of any property like the quality of images, page numbers that you need to convert to PDF format, encryption details, etc. as per your requirements. Similarly, you can read and write the files using the file streams or memory streams with simple API calls.
This tutorial has provided the details pertaining to the conversion of XPS file to PDF in C#. If you want to learn EPS to TIFF conversion, read the article on how to convert EPS to TIFF in C#.