Live Chat Icon For mobile
Live Chat Icon

How can I programmatically print XPS Files ?

Platform: WPF| Category: Printing

You can use one overload of the AddJob() method to print XML Paper Specification (XPS) files without opening a PrintDialog or in principle, any user interface (UI) at all. You can also print XML Paper Specification (XPS) files using the many Write() and WriteAsync() methods of the XPSDocumentWriter. Another way of printing XML Paper Specification (XPS) is to use the PrintDocument() or PrintVisual() methods of the PrintDialog control.

The main steps to use the three-parameter AddJob(String, String, Boolean) method are as follows.

The example below gives details.
1. Determine if the printer is an XPSDrv printer.

2. The printer is not an XPSDrv printer, set the thread’s apartment to a single thread.
3. Instantiate a print server and print queue object.
4. Call the method specifying a job name, the file to be printed and a Boolean flag indicating whether or not the printer is an XPSDrv printer.

class Program
    [System.MTAThreadAttribute()] // Added for clarity, but this line is redundant because MTA is the default.
    static void Main(string[] args)
        // Create the secondary thread and pass the printing method for 
        // the constructor’s ThreadStart delegate parameter. The BatchXPSPrinter
        // class is defined below.
        Thread printingThread = new Thread(BatchXPSPrinter.PrintXPS);

        // Set the thread that will use PrintQueue.AddJob to single threading.

        // Start the printing thread. The method passed to the Thread 
        // constructor will execute.

    }//end Main

}//end Program class

public class BatchXPSPrinter
    public static void PrintXPS()
        // Create print server and print queue.
        LocalPrintServer localPrintServer = new LocalPrintServer();
        PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

        // Prompt user to identify the directory, and then create the directory object.
        Console.Write('Enter the directory containing the XPS files: ');
        String directoryPath = Console.ReadLine();
        DirectoryInfo dir = new DirectoryInfo(directoryPath);

        // If the user mistyped, end the thread and return to the Main thread.
        if (!dir.Exists)
            Console.WriteLine('There is no such directory.');
            // If there are no XPS files in the directory, end the thread 
            // and return to the Main thread.
            if (dir.GetFiles('*.xps').Length == 0)
                Console.WriteLine('There are no XPS files in the directory.');
                Console.WriteLine('\nJobs will now be added to the print queue.');
                Console.WriteLine('If the queue is not paused and the printer is working, jobs will begin printing.');

                // Batch process all XPS files in the directory.
                foreach (FileInfo f in dir.GetFiles('*.xps'))
                    String nextFile = directoryPath + '\\' + f.Name;
                    Console.WriteLine('Adding {0} to queue.', nextFile);

                        // Print the Xps file while providing XPS validation and progress notifications.
                        PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(f.Name, nextFile, false);
                    catch (PrintJobException e)
                        Console.WriteLine('\n\t{0} could not be added to the print queue.', f.Name);
                        if (e.InnerException.Message == 'File contains corrupted data.')
                            Console.WriteLine('\tIt is not a valid XPS file. Use the isXPS Conformance Tool to debug it.');
                        Console.WriteLine('\tContinuing with next XPS file.\n');

                }// end for each XPS file

            }//end if there are no XPS files in the directory

        }//end if the directory does not exist

        Console.WriteLine('Press Enter to end program.');

    }// end PrintXPS method

}// end BatchXPSPrinter class

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.