AddJob Method (String, String, Boolean)

PrintQueue.AddJob Method (String, String, Boolean)


Inserts a new print job for an XML Paper Specification (XPS) Document into the queue, gives it the specified name, and specifies whether or not it should be validated.

Namespace:   System.Printing
Assembly:  System.Printing (in System.Printing.dll)

public PrintSystemJobInfo AddJob(
	string jobName,
	string documentPath,
	bool fastCopy


Type: System.String

The name of the print job.

Type: System.String

The path and name of the document that is being printed.

Type: System.Boolean

true to spool quickly without page-by-page progress feedback and without validating that the file is valid XPS; otherwise, false.

Return Value

Type: System.Printing.PrintSystemJobInfo

A PrintSystemJobInfo that represents the print job and its status.

If fastCopy is true, then the printer must be an Printing Overview. If it is not, the AddJob(String, String, Boolean) method throws an exception.

If fastCopy is false, then it is not necessary to use an XPSDrv printer. The XPS file being added to the queue is converted to the printer's page description language, such as PCL or Postscript. However, this kind of printing makes a call to Component Object Model (COM). The call to COM requires that the calling thread have a single-threaded apartment (STA) instead of multiple-threaded apartment (MTA) which is the default in Microsoft .NET 2.0 and later. (For more on threading and apartment states, see Managed and Unmanaged Threading, and ApartmentState.) There are two ways of doing this: 

  • The simplest way is to add the STAThreadAttribute (that is, "[System.STAThreadAttribute()]") just above the first line of the application's Main method (usually "static void Main(string[] args)").

  • If you need your Main thread's apartment state to be MTA, you can house the call to AddJob(String, String, Boolean) in a separate thread whose apartment state is set to STA with SetApartmentState. The example below illustrates this second technique.


You cannot apply the STAThreadAttribute to any method except Main and you cannot use SetApartmentState for the Main thread.

Other ways to print in Windows Presentation Foundation (WPF) include the PrintDialog.PrintDocument method, which can be used with or without opening the dialog, and the many Write and WriteAsync methods of the XpsDocumentWriter.

The following example shows how to use AddJob(String, String, Boolean) to batch print all the XML Paper Specification (XPS) files in a directory.  

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

.NET Framework
Available since 3.0
Return to top
© 2015 Microsoft