Search code examples
c#.netxpsprintqueue

Print a XPS file from a byte stream


I want to print a XPS file in C#. I'm getting the XPS file as a byte stream. I tried to achieve this with different AddJob methods provided in PrintQueue class and encountered following issues

  • AddJob(String)

This accepts a byte stream and do the printing. But this method keeps the file spooling for a long time so cannot use this.

  • AddJob(String, PrintTicket)

This required a physical file location of the XPS file. since my file is a byte stream cannot use this one

  • AddJob(String, String, Boolean)

This method also requires a physical file location

  • AddJob(String, String, Boolean, PrintTicket)

Requires a physical file location

Therefore how can I print a XPS file from a byte stream. I don't want to use any 3rd party library for this


Solution

  • Have you tried the following overload:

    AddJob() - Inserts a new (generically named) print job, whose content is a Byte array, into the queue.

    Some Sample code as obtained from MSDN is as follows

    // Create the printer server and print queue objects
    LocalPrintServer localPrintServer = new LocalPrintServer();
    PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();
    
    // Call AddJob
    PrintSystemJobInfo myPrintJob = defaultPrintQueue.AddJob();
    
    // Write a Byte buffer to the JobStream and close the stream
    Stream myStream = myPrintJob.JobStream;
    Byte[] myByteBuffer = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream.");
    myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
    myStream.Close();