Inserts a new (generically named) print job, whose content is a Byte array, into the queue.
Assembly: System.Printing (in System.Printing.dll)
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 prints 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 is thrown when that thread ends because the spooler thread cannot gain control over the Stream object.
In the print queue's graphical user interface (GUI), the job has the name "Print System Document". To give the job a different name, use the AddJob(String) overload.
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 to send a Byte array to a print queue. This code only works with printers that can detect and print plain text. Some of them cannot.
// 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();
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.