通过


PrintQueue.AddJob 方法

定义

将新的打印作业插入队列。

重载

名称 说明
AddJob(String, String, Boolean, PrintTicket)

将 XML 纸张规范(XPS)文档的新打印作业插入队列,为其指定名称和设置,并指定是否应对其进行验证。

AddJob(String, PrintTicket)

将 XML 纸张规范 (XPS) 文档的新打印作业插入队列中,并为其指定名称和设置。

AddJob(String, String, Boolean)

将 XML 纸张规范(XPS)文档的新打印作业插入队列中,为其指定名称,并指定是否应对其进行验证。

AddJob()

将新的(一般命名)打印作业(其内容为 Byte 数组)插入队列。

AddJob(String)

将一个新的打印作业(其内容为 Byte 数组)插入队列。

注解

除非队列已暂停或处于错误状态,否则作业在到达队列顶部时打印,因此这是打印函数。

在 Windows Presentation Foundation(WPF)中打印的其他方法包括 PrintDialog.PrintDocument 该方法,该方法可用于或不打开对话框,以及 Write 许多方法和 WriteAsync 方法 XpsDocumentWriter

AddJob(String, String, Boolean, PrintTicket)

将 XML 纸张规范(XPS)文档的新打印作业插入队列,为其指定名称和设置,并指定是否应对其进行验证。

public:
 System::Printing::PrintSystemJobInfo ^ AddJob(System::String ^ jobName, System::String ^ documentPath, bool fastCopy, System::Printing::PrintTicket ^ printTicket);
public System.Printing.PrintSystemJobInfo AddJob(string jobName, string documentPath, bool fastCopy, System.Printing.PrintTicket printTicket);
member this.AddJob : string * string * bool * System.Printing.PrintTicket -> System.Printing.PrintSystemJobInfo
Public Function AddJob (jobName As String, documentPath As String, fastCopy As Boolean, printTicket As PrintTicket) As PrintSystemJobInfo

参数

jobName
String

正在打印的文档的路径和名称。

documentPath
String

正在打印的文档的路径和名称。

fastCopy
Boolean

true 在不逐页进度反馈的情况下快速后台处理,且不验证文件是否为有效的 XPS;否则,为 false.

printTicket
PrintTicket

打印作业的设置。

返回

表示打印作业及其状态的一个 PrintSystemJobInfo

注解

有关详细信息,请参阅 AddJob(String, String, Boolean)

适用于

AddJob(String, PrintTicket)

将 XML 纸张规范 (XPS) 文档的新打印作业插入队列中,并为其指定名称和设置。

public:
 System::Printing::PrintSystemJobInfo ^ AddJob(System::String ^ jobName, System::Printing::PrintTicket ^ printTicket);
public System.Printing.PrintSystemJobInfo AddJob(string jobName, System.Printing.PrintTicket printTicket);
member this.AddJob : string * System.Printing.PrintTicket -> System.Printing.PrintSystemJobInfo
Public Function AddJob (jobName As String, printTicket As PrintTicket) As PrintSystemJobInfo

参数

jobName
String

正在打印的文档的路径和名称。

printTicket
PrintTicket

打印作业的设置。

返回

表示打印作业及其状态的一个 PrintSystemJobInfo

注解

有关详细信息,请参阅 AddJob(String)

适用于

AddJob(String, String, Boolean)

将 XML 纸张规范(XPS)文档的新打印作业插入队列中,为其指定名称,并指定是否应对其进行验证。

public:
 System::Printing::PrintSystemJobInfo ^ AddJob(System::String ^ jobName, System::String ^ documentPath, bool fastCopy);
public System.Printing.PrintSystemJobInfo AddJob(string jobName, string documentPath, bool fastCopy);
member this.AddJob : string * string * bool -> System.Printing.PrintSystemJobInfo
Public Function AddJob (jobName As String, documentPath As String, fastCopy As Boolean) As PrintSystemJobInfo

参数

jobName
String

打印作业的名称。

documentPath
String

正在打印的文档的路径和名称。

fastCopy
Boolean

true 在不逐页进度反馈的情况下快速后台处理,且不验证文件是否为有效的 XPS;否则,为 false.

返回

表示打印作业及其状态的一个 PrintSystemJobInfo

示例

以下示例演示如何用于 AddJob(String, String, Boolean) 批处理打印目录中的所有 XML 纸张规范 (XPS) 文件。

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();
    }
}

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");
                    }
                }
            }
        }

        Console.WriteLine("Press Enter to end program.");
        Console.ReadLine();
    }
}
Friend Class Program
    <System.MTAThreadAttribute()>
    Shared Sub Main(ByVal args() As String) ' Added for clarity, but this line is redundant because MTA is the default.
        ' Create the secondary thread and pass the printing method for 
        ' the constructor's ThreadStart delegate parameter. The BatchXPSPrinter
        ' class is defined below.
        Dim printingThread As New Thread(AddressOf 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 Sub

End Class

Public Class BatchXPSPrinter
    Public Shared Sub PrintXPS()
        ' Create print server and print queue.
        Dim localPrintServer As New LocalPrintServer()
        Dim defaultPrintQueue As PrintQueue = LocalPrintServer.GetDefaultPrintQueue()

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

        ' If the user mistyped, end the thread and return to the Main thread.
        If Not dir.Exists Then
            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 Then
                Console.WriteLine("There are no XPS files in the directory.")
            Else
                Console.WriteLine(vbLf & "Jobs 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.
                For Each f As FileInfo In dir.GetFiles("*.xps")
                    Dim nextFile As String = directoryPath & "\" & f.Name
                    Console.WriteLine("Adding {0} to queue.", nextFile)

                    Try
                        ' Print the Xps file while providing XPS validation and progress notifications.
                        Dim xpsPrintJob As PrintSystemJobInfo = defaultPrintQueue.AddJob(f.Name, nextFile, False)
                    Catch e As PrintJobException
                        Console.WriteLine(vbLf & vbTab & "{0} could not be added to the print queue.", f.Name)
                        If e.InnerException.Message = "File contains corrupted data." Then
                            Console.WriteLine(vbTab & "It is not a valid XPS file. Use the isXPS Conformance Tool to debug it.")
                        End If
                        Console.WriteLine(vbTab & "Continuing with next XPS file." & vbLf)
                    End Try

                Next f ' end for each XPS file

            End If 'end if there are no XPS files in the directory

        End If 'end if the directory does not exist

        Console.WriteLine("Press Enter to end program.")
        Console.ReadLine()

    End Sub

End Class

注解

true如果是fastCopy,则打印机必须是打印概述。 如果不是,该方法 AddJob(String, String, Boolean) 将引发异常。

false如果是fastCopy,则不需要使用 XPSDrv 打印机。 要添加到队列中的 XPS 文件将转换为打印机的页面说明语言,例如 PCL 或 Postscript。 但是,此类打印调用组件对象模型(COM)。 对 COM 的调用要求调用线程具有单线程单元 (STA) 而不是多线程单元 (MTA),这是默认值。 有两种方法可以执行此作:

  • 最简单的方法是在应用程序方法(通常为“”)的第一行上方添加STAThreadAttribute(即“[System.STAThreadAttribute()])。static void Main(string[] args)Main

  • 如果需要Main线程的公寓状态MTA,则可以将调用AddJob(String, String, Boolean)放在其单元状态设置为STASetApartmentState的单独线程中。 下面的示例演示了第二种技术。

注释

不能应用于STAThreadAttribute除线程以外的Main任何方法,并且不能用于SetApartmentStateMain线程。

在 Windows Presentation Foundation(WPF)中打印的其他方法包括 PrintDialog.PrintDocument 该方法,该方法可用于或不打开对话框,以及 Write 许多方法和 WriteAsync 方法 XpsDocumentWriter

另请参阅

适用于

AddJob()

将新的(一般命名)打印作业(其内容为 Byte 数组)插入队列。

public:
 System::Printing::PrintSystemJobInfo ^ AddJob();
public System.Printing.PrintSystemJobInfo AddJob();
member this.AddJob : unit -> System.Printing.PrintSystemJobInfo
Public Function AddJob () As PrintSystemJobInfo

返回

表示打印作业及其状态的一个 PrintSystemJobInfo

示例

以下示例演示如何用于 AddJob() 将数组发送到 Byte 打印队列。 此代码仅适用于可以检测和打印纯文本的打印机。 其中一些人不能。

// 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();
' Create the printer server and print queue objects
Dim localPrintServer As New LocalPrintServer()
Dim defaultPrintQueue As PrintQueue = LocalPrintServer.GetDefaultPrintQueue()

' Call AddJob
Dim myPrintJob As PrintSystemJobInfo = defaultPrintQueue.AddJob()

' Write a Byte buffer to the JobStream and close the stream
Dim myStream As Stream = myPrintJob.JobStream
Dim myByteBuffer() As Byte = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream.")
myStream.Write(myByteBuffer, 0, myByteBuffer.Length)
myStream.Close()

注解

使用此方法可将设备特定的信息写入到后台处理程序文件中,Microsoft Windows 后台处理程序不会自动包含该文件。 当然,你需要知道后台处理程序文件是增强型图元文件(EMF)还是 XML 纸张规范(XPS)。 如果希望使用 Stream API,可以使用 PrintQueueStream 类而不是此方法。

AddJob调用该方法后,必须将数组JobStream写入ByteAddJob打印作业返回或未创建打印作业的属性PrintSystemJobInfo。 此数组是打印机正常工作且未暂停时打印的内容。

注意

JobStream如果在调用的线程AddJob末尾之前未关闭Close该线程,则当该线程结束时将引发一个InvalidOperationException,因为后台处理程序线程无法控制Stream该对象。

在打印队列的图形用户界面(GUI)中,作业的名称为“打印系统文档”。 若要为作业指定其他名称,请使用 AddJob(String) 重载。

在 Windows Presentation Foundation(WPF)中打印的其他方法包括 PrintDialog.PrintDocument 该方法,该方法可用于或不打开对话框,以及 Write 许多方法和 WriteAsync 方法 XpsDocumentWriter

适用于

AddJob(String)

将一个新的打印作业(其内容为 Byte 数组)插入队列。

public:
 System::Printing::PrintSystemJobInfo ^ AddJob(System::String ^ jobName);
public System.Printing.PrintSystemJobInfo AddJob(string jobName);
member this.AddJob : string -> System.Printing.PrintSystemJobInfo
Public Function AddJob (jobName As String) As PrintSystemJobInfo

参数

jobName
String

打印作业的名称。

返回

表示打印作业及其状态的一个 PrintSystemJobInfo

示例

以下示例演示如何用于 AddJob(String) 将文件读取到 Byte 数组中,并将该数组发送到打印队列。 此代码假定 C: 驱动器的根目录中有一个名为 test.txt 的文件。 此代码仅适用于可以检测和打印纯文本的打印机。 其中一些人不能。

// 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();
' Create the printer server and print queue objects
Dim localPrintServer2 As New LocalPrintServer()
Dim defaultPrintQueue2 As PrintQueue = LocalPrintServer.GetDefaultPrintQueue()

' Call AddJob 
Dim anotherPrintJob As PrintSystemJobInfo = defaultPrintQueue2.AddJob("MyJob")

' Read a file into a StreamReader
Dim myStreamReader As New StreamReader("C:\test.txt")

' Write a Byte buffer to the JobStream and close the stream
Dim anotherStream As Stream = anotherPrintJob.JobStream
Dim anotherByteBuffer() As Byte = UnicodeEncoding.Unicode.GetBytes(myStreamReader.ReadToEnd())
anotherStream.Write(anotherByteBuffer, 0, anotherByteBuffer.Length)
anotherStream.Close()

注解

使用此方法可将设备特定的信息写入到后台处理程序文件中,Microsoft Windows 后台处理程序不会自动包含该文件。 当然,你需要知道后台处理程序文件是增强型图元文件(EMF)还是 XML 纸张规范(XPS)。 如果希望使用 Stream API,可以使用 PrintQueueStream 类而不是此方法。

AddJob调用该方法后,必须将数组JobStream写入ByteAddJob打印作业返回或未创建打印作业的属性PrintSystemJobInfo。 此数组是打印机正常工作且未暂停时打印的内容。

注意

JobStream如果在调用的线程AddJob末尾之前未关闭Close该线程,则当该线程结束时将引发一个InvalidOperationException,因为后台处理程序线程无法控制Stream该对象。

在 Windows Presentation Foundation(WPF)中打印的其他方法包括 PrintDialog.PrintDocument 该方法,该方法可用于或不打开对话框,以及 Write 许多方法和 WriteAsync 方法 XpsDocumentWriter

适用于