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
)

Parameters

jobName
Type: System.String

The name of the print job.

documentPath
Type: System.String

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

fastCopy
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.

NoteNote

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.
        printingThread.SetApartmentState(ApartmentState.STA);

        // Start the printing thread. The method passed to the Thread  
        // constructor will execute.
        printingThread.Start();

    }//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.");
        }
        else
        {
            // 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.");
            }
            else
            {
                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);

                    try
                    {
                        // 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.");
        Console.ReadLine();

    }// end PrintXPS method

}// end BatchXPSPrinter class

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft