PrintQueue.AddJob Method (String)
Assembly: System.Printing (in system.printing.dll)
The name of the print job.
Return ValueA PrintSystemJobInfo that represents the print job and its status.
Use this method to write device specific information, to a spool file, that is not automatically included by the Microsoft Windows spooler. Of course, you need to know whether the spool file is Enhanced Metafile (EMF) or XML Paper Specification (XPS). If you prefer to work with the Stream API, you can use the PrintQueueStream class instead of this method.
After the AddJob method has been called, you must write a Byte array to the JobStream property of the PrintSystemJobInfo that is returned by AddJob or no print job is created. This array is what will print if the printer is working and is not paused.
If the JobStream is not closed with Close before the end of the thread in which AddJob is called, then an InvalidOperationException will occur when that thread ends because the spooler thread cannot gain control over the Stream object.
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) to read a file into a Byte array and send the array to a print queue. This code assumes that there is a file called test.txt in the root of the C: drive. This code will only work with printers that can detect and print plain text. Some of them cannot.
// Create the printer server and print queue objects LocalPrintServer localPrintServer2 = new LocalPrintServer(); PrintQueue defaultPrintQueue2 = LocalPrintServer.GetDefaultPrintQueue(); // Call AddJob PrintSystemJobInfo anotherPrintJob = defaultPrintQueue2.AddJob("MyJob"); // Read a file into a StreamReader StreamReader myStreamReader = new StreamReader("C:\\test.txt"); // Write a Byte buffer to the JobStream and close the stream Stream anotherStream = anotherPrintJob.JobStream; Byte anotherByteBuffer = UnicodeEncoding.Unicode.GetBytes(myStreamReader.ReadToEnd()); anotherStream.Write(anotherByteBuffer, 0, anotherByteBuffer.Length); anotherStream.Close();
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.