PrintQueue.AddJob 方法

定义

在队列中插入一个新的打印作业。

重载

AddJob(String, String, Boolean, PrintTicket)

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

AddJob(String, PrintTicket)

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

AddJob(String, String, Boolean)

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

AddJob()

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

AddJob(String)

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

注解

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

在 Windows Presentation Foundation (WPF) 中打印的其他方法包括 PrintDialog.PrintDocument 方法(可用于或不打开对话框)以及 的XpsDocumentWriter多个 WriteWriteAsync 方法。

AddJob(String, String, Boolean, PrintTicket)

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

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) Document 插入队列,并为其指定名称和设置。

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) Document 的新打印作业插入队列,为其指定名称,并指定是否应对其进行验证。

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

注解

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

如果 fastCopyfalse,则无需使用 XPSDrv 打印机。 要添加到队列的 XPS 文件将转换为打印机的页面描述语言,例如 PCL 或 Postscript。 但是,这种打印会调用组件对象模型 (COM) 。 调用 COM 要求调用线程具有单线程单元 (STA) ,而不是多线程单元 (MTA) 这是 Microsoft .NET 2.0 及更高版本中的默认单元。 (有关线程和单元状态的详细信息,请参阅 托管和非托管线程处理和 ApartmentState.) 有两种方法可以执行此操作:

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

  • 如果需要 Main 线程的单元状态为 MTA,则可以将对 AddJob(String, String, Boolean) 的调用托管在单独的线程中,该线程的单元状态设置为 STASetApartmentState。 下面的示例演示了第二种方法。

注意

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

在 Windows Presentation Foundation (WPF) 中打印的其他方法包括 PrintDialog.PrintDocument 方法(可用于或不打开对话框)以及 的XpsDocumentWriter多个 WriteWriteAsync 方法。

另请参阅

适用于

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调用 方法后,必须将数组写入ByteJobStream返回AddJob的 的 PrintSystemJobInfo 属性,否则不会创建打印作业。 如果打印机正常工作且未暂停,则此数组是打印的内容。

注意

JobStream如果 未在调用的AddJob线程结束之前关闭 Close ,则在InvalidOperationException线程结束时会引发 ,因为后台处理程序线程无法获取对 Stream 对象的控制。

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

在 Windows Presentation Foundation (WPF) 中打印的其他方法包括 PrintDialog.PrintDocument 方法(可用于或不打开对话框)以及 的XpsDocumentWriter多个 WriteWriteAsync 方法。

适用于

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调用 方法后,必须将数组写入ByteJobStream返回AddJob的 的 PrintSystemJobInfo 属性,否则不会创建打印作业。 如果打印机正常工作且未暂停,则此数组是打印的内容。

注意

JobStream如果 未在调用的AddJob线程结束之前关闭 Close ,则在InvalidOperationException线程结束时会引发 ,因为后台处理程序线程无法获取对 Stream 对象的控制。

在 Windows Presentation Foundation (WPF) 中打印的其他方法包括 PrintDialog.PrintDocument 方法(可用于或不打开对话框)以及 的XpsDocumentWriter多个 WriteWriteAsync 方法。

适用于