How to Print PDF to XPS in C#/VB.NET

Windows 7 comes with a Microsoft XPS Document Writer (MXDW) printer which can be used to print any document to an XPS file. It is also included in later releases, such as Windows 10. This article will introduce how to programmatically connect MXDW and print a PDF file into a XPS file.

To use following code snippets to print PDF file, you need to install Spire.PDF via NuGet and add the dll files into .NET assemblies.

Code Snippets

using Spire.Pdf;
namespace PrintPdfToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //load a Pdf document
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"example.pdf");

            //specify printer 
            doc.PrintDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
            //print Pdf to a file instead a port
            doc.PrintDocument.PrinterSettings.PrintToFile = true;
            doc.PrintDocument.PrinterSettings.PrintFileName = "PrintToXps.xps";

            //start printing process
            doc.PrintDocument.Print();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s