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 陣列。

備註

除非佇列已暫停或處於錯誤狀態,否則作業會在到達佇列頂端時列印,因此這是列印函式。

其他在 WPF Windows Presentation Foundation (列印的方式) 包含 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 的呼叫需要呼叫線程具有單個線程 Apartment () STA ,而不是多線程 Apartment (MTA) 這是 Microsoft .NET 2.0 和更新版本中的預設值。 (如需線程和 Apartment 狀態的詳細資訊,請參閱 Managed 和 Unmanaged ThreadingApartmentState.) 有兩種方式可執行此動作:

  • 最簡單的方法是新增 STAThreadAttribute (,也就是 “[System.STAThreadAttribute()]”“) 在應用程式 Main 方法的第一行上方 (通常是 ”static void Main(string[] args)“) 。

  • 如果您需要線程Main的 Apartment 狀態為 MTA,您可以將 呼叫AddJob(String, String, Boolean)置於具有 的 Apartment 狀態設定STASetApartmentState為 的個別線程中。 下列範例說明第二個技術。

注意

您無法將 套用 STAThreadAttribute 至任何方法,但 Main 不能 SetApartmentState 用於 Main 線程。

其他在 WPF Windows Presentation Foundation (列印的方式) 包含 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呼叫 方法之後,您必須將Byte陣列印入所JobStreamAddJob傳回的 PrintSystemJobInfo 屬性,或未建立列印作業。 此陣列印機正常運作且未暫停時所列印的內容。

警告

JobStream如果 未在呼叫的線程AddJob結尾之前關閉 Close ,當該線程結束時,就會擲回 ,InvalidOperationException因為多任務緩衝處理程式線程無法控制Stream物件。

在列印佇列的圖形使用者介面 (GUI) 中,作業的名稱為 “Print System Document”。 若要為作業指定不同的名稱,請使用 AddJob(String) 多載。

其他在 WPF Windows Presentation Foundation (列印的方式) 包含 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呼叫 方法之後,您必須將Byte陣列印入所JobStreamAddJob傳回的 PrintSystemJobInfo 屬性,或未建立列印作業。 此陣列印機正常運作且未暫停時所列印的內容。

警告

JobStream如果 未在呼叫的線程AddJob結尾之前關閉 Close ,當該線程結束時,就會擲回 ,InvalidOperationException因為多任務緩衝處理程式線程無法控制Stream物件。

其他在 WPF Windows Presentation Foundation (列印的方式) 包含 PrintDialog.PrintDocument 方法,可以搭配或不使用開啟對話方塊,以及的XpsDocumentWriter許多 WriteWriteAsync 方法。

適用於