How to convert PDF to XPS in C#?

About XPS

Like Adobe Systems's PDF format, XPS is a fixed-layout document format designed to preserve document fidelity, providing device-independent document appearance. To convert PDF to XPS is a common need for developers in their programming process.

Convert PDF to XPS

Spire.PDF for .NET is an easy- to- use component which can be used to convert PDF to XPS and vise versa. It supports complication of Console, Winform and ASP.net. This article is using an example of Console application and reveals 4 easy steps to realize this function. The following shows the original PDF file:

pdf to xps 01

Step 1: Download component package, to build a console program in VS and set its target framework as .NET Framwork 2.0.After that, you can reference spire.pdf.dll.

Step 2: Instanate an object of Spire.Pdf.PdfDocument, view C# code:

[C#]
PdfDocument doc = new PdfDocument();

Step 3: Load a PDF file

[C#]
doc.LoadFromFile("sample.pdf");

Step 4: Save Spire.Pdf.PdfDocument object as XPS format.

[C#]
doc.SaveToFile("sample.xps", FileFormat.XPS);

After the steps above, you can run it and convert PDF to XPS. The following screenshot shows that PDF file is saved as XPS format after converting:

pdf to xps 01

The following code snippet reveals all the code while converting PDf to XPS.

[C#]
using System;
using System.Collections.Generic;
using System.Text;
using Spire.Pdf;

namespace ConvertPdfToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //  Instatate an object of Spire.Pdf.PdfDocument
            PdfDocument doc = new PdfDocument();
            // Load PDF document 
            doc.LoadFromFile("sample.pdf");
            // Save it to XPS format 
            doc.SaveToFile("sample.xps", FileFormat.XPS);
            doc.Close();
            System.Diagnostics.Process.Start("sample.xps");
        }
    }
}