共用方式為


PrintQueue.AddJob 方法

定義

將新的列印工作插入佇列。

多載

名稱 Description
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方法,可搭配或不開啟對話框使用,以及 WriteAsyncWriteXpsDocumentWriter.

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

列印工作的設定。

傳回

代表列印工作及其狀態的 A 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

列印工作的設定。

傳回

代表列印工作及其狀態的 A 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

傳回

代表列印工作及其狀態的 A 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),後者是預設的。 有兩種方法可以做到這一點:

  • 最簡單的方法是在應用程式Main方法的第一行(通常是「」static void Main(string[] args))上方加上STAThreadAttribute(即[System.STAThreadAttribute()]「」)。

  • 如果你需要 Main 執行緒的公寓狀態是 MTA,你可以將呼叫 AddJob(String, String, Boolean) 存放在一個獨立執行緒,該執行緒的公寓狀態設 STASetApartmentState。 以下範例說明了第二種技巧。

備註

你不能將 套STAThreadAttribute用到任何方法,除了Main且不能用於SetApartmentStateMain該線程。

在 Windows Presentation Foundation(WPF)中,其他列印方式包括PrintDialog.PrintDocument方法,可搭配或不開啟對話框使用,以及 WriteAsyncWriteXpsDocumentWriter.

另請參閱

適用於

AddJob()

將一個新的(通用命名的)列印工作插入佇列,其內容為 Byte 陣列。

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

傳回

代表列印工作及其狀態的 A 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()

備註

使用此方法將裝置專屬資訊寫入 spool 檔案,該檔案不會被 Microsoft Windows spooler 自動包含。 當然,你需要知道這個卷軸檔案是增強元檔案(EMF)還是 XML 紙張規範(XPS)。 如果你比較喜歡用 Stream API,可以用這個 PrintQueueStream 類別代替這個方法。

方法被呼叫後AddJob,你必須寫Byte一個屬性的JobStreamPrintSystemJobInfo陣列,AddJob否則無法建立列印工作。 這個陣列是印表機正常且未暫停時的列印。

謹慎

如果在JobStream被呼叫的執行緒AddJob結束前未關閉 ,Close則當該執行緒結束時會拋出 aInvalidOperationException,因為排檔執行緒無法取得對物件Stream的控制權。

在列印佇列的圖形使用者介面(GUI)中,該工作名稱為「列印系統文件」。 要給這個工作換個名字,可以用超載(overload)。AddJob(String)

在 Windows Presentation Foundation(WPF)中,其他列印方式包括PrintDialog.PrintDocument方法,可搭配或不開啟對話框使用,以及 WriteAsyncWriteXpsDocumentWriter.

適用於

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

列印任務的名稱。

傳回

代表列印工作及其狀態的 A 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()

備註

使用此方法將裝置專屬資訊寫入 spool 檔案,該檔案不會被 Microsoft Windows spooler 自動包含。 當然,你需要知道這個卷軸檔案是增強元檔案(EMF)還是 XML 紙張規範(XPS)。 如果你比較喜歡用 Stream API,可以用這個 PrintQueueStream 類別代替這個方法。

方法被呼叫後AddJob,你必須寫Byte一個屬性的JobStreamPrintSystemJobInfo陣列,AddJob否則無法建立列印工作。 這個陣列是印表機正常且未暫停時的列印。

謹慎

如果在JobStream被呼叫的執行緒AddJob結束前未關閉 ,Close則當該執行緒結束時會拋出 aInvalidOperationException,因為排檔執行緒無法取得對物件Stream的控制權。

在 Windows Presentation Foundation(WPF)中,其他列印方式包括PrintDialog.PrintDocument方法,可搭配或不開啟對話框使用,以及 WriteAsyncWriteXpsDocumentWriter.

適用於