Udostępnij za pośrednictwem


PrintQueue.AddJob Metoda

Definicja

Wstawia nowe zadanie drukowania do kolejki.

Przeciążenia

Nazwa Opis
AddJob(String, String, Boolean, PrintTicket)

Wstawia nowe zadanie drukowania dokumentu specyfikacji papieru XML (XPS) do kolejki, podaje określoną nazwę i ustawienia oraz określa, czy ma zostać zweryfikowany.

AddJob(String, PrintTicket)

Wstawia nowe zadanie drukowania dokumentu specyfikacji papieru XML (XPS) do kolejki i nadaje mu określoną nazwę i ustawienia.

AddJob(String, String, Boolean)

Wstawia nowe zadanie drukowania dokumentu specyfikacji papieru XML (XPS) do kolejki, podaje określoną nazwę i określa, czy ma zostać zweryfikowany.

AddJob()

Wstawia nowe (ogólnie nazwane) zadanie drukowania, którego zawartość jest tablicą Byte , do kolejki.

AddJob(String)

Wstawia nowe zadanie drukowania, którego zawartość jest tablicą Byte , do kolejki.

Uwagi

Jeśli kolejka nie zostanie wstrzymana lub w stanie błędu, zadanie zostanie wydrukowane po osiągnięciu górnej części kolejki, więc jest to funkcja drukowania.

Inne sposoby drukowania w programie Windows Presentation Foundation (WPF) obejmują metodę PrintDialog.PrintDocument , która może być używana z lub bez otwierania okna dialogowego XpsDocumentWriteroraz wiele Write metod i WriteAsync .

AddJob(String, String, Boolean, PrintTicket)

Wstawia nowe zadanie drukowania dokumentu specyfikacji papieru XML (XPS) do kolejki, podaje określoną nazwę i ustawienia oraz określa, czy ma zostać zweryfikowany.

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

Parametry

jobName
String

Ścieżka i nazwa drukowanego dokumentu.

documentPath
String

Ścieżka i nazwa drukowanego dokumentu.

fastCopy
Boolean

true aby szybko buforować bez opinii o postępie strony po stronie i bez sprawdzania poprawności pliku jest prawidłowy XPS; w przeciwnym razie, false.

printTicket
PrintTicket

Ustawienia zadania drukowania.

Zwraca

Element PrintSystemJobInfo reprezentujący zadanie drukowania i jego stan.

Uwagi

Aby uzyskać więcej informacji, zobacz AddJob(String, String, Boolean).

Dotyczy

AddJob(String, PrintTicket)

Wstawia nowe zadanie drukowania dokumentu specyfikacji papieru XML (XPS) do kolejki i nadaje mu określoną nazwę i ustawienia.

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

Parametry

jobName
String

Ścieżka i nazwa drukowanego dokumentu.

printTicket
PrintTicket

Ustawienia zadania drukowania.

Zwraca

Element PrintSystemJobInfo reprezentujący zadanie drukowania i jego stan.

Uwagi

Aby uzyskać więcej informacji, zobacz AddJob(String).

Dotyczy

AddJob(String, String, Boolean)

Wstawia nowe zadanie drukowania dokumentu specyfikacji papieru XML (XPS) do kolejki, podaje określoną nazwę i określa, czy ma zostać zweryfikowany.

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

Parametry

jobName
String

Nazwa zadania drukowania.

documentPath
String

Ścieżka i nazwa drukowanego dokumentu.

fastCopy
Boolean

true aby szybko buforować bez opinii o postępie strony po stronie i bez sprawdzania poprawności pliku jest prawidłowy XPS; w przeciwnym razie, false.

Zwraca

Element PrintSystemJobInfo reprezentujący zadanie drukowania i jego stan.

Przykłady

W poniższym przykładzie pokazano, jak używać AddJob(String, String, Boolean) metody do drukowania wsadowego wszystkich plików specyfikacji papieru XML (XPS) w katalogu.

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

Uwagi

Jeśli fastCopy ma truewartość , drukarka musi być omówieniem drukowania. Jeśli tak nie jest, AddJob(String, String, Boolean) metoda zgłasza wyjątek.

Jeśli fastCopy jest falseto , nie jest konieczne użycie drukarki XPSDrv. Plik XPS dodawany do kolejki jest konwertowany na język opisu strony drukarki, taki jak PCL lub Postscript. Jednak tego rodzaju drukowanie powoduje wywołanie modelu obiektów składników (COM). Wywołanie do modelu COM wymaga, aby wątek wywołujący miał jednowątkowy apartament (STA) zamiast wielowątkowego mieszkania (MTA), który jest domyślny. Istnieją dwa sposoby wykonania tego:

  • Najprostszym sposobem jest dodanie STAThreadAttribute (czyli "[System.STAThreadAttribute()]") tuż nad pierwszym wierszem metody aplikacji Main (zwykle "static void Main(string[] args)").

  • Jeśli potrzebujesz Main stanu mieszkania wątku do MTA, możesz pomieścić wywołanie w AddJob(String, String, Boolean) osobnym wątku, którego stan mieszkania jest ustawiony na STA .SetApartmentState Poniższy przykład ilustruje tę drugą technikę.

Uwaga / Notatka

Nie można zastosować STAThreadAttribute elementu do żadnej metody z wyjątkiem Main metody i nie można jej użyć SetApartmentState dla wątku Main .

Inne sposoby drukowania w programie Windows Presentation Foundation (WPF) obejmują metodę PrintDialog.PrintDocument , która może być używana z lub bez otwierania okna dialogowego XpsDocumentWriteroraz wiele Write metod i WriteAsync .

Zobacz też

Dotyczy

AddJob()

Wstawia nowe (ogólnie nazwane) zadanie drukowania, którego zawartość jest tablicą Byte , do kolejki.

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

Zwraca

Element PrintSystemJobInfo reprezentujący zadanie drukowania i jego stan.

Przykłady

W poniższym przykładzie pokazano, jak wysyłać AddJob() tablicę Byte do kolejki wydruku. Ten kod działa tylko z drukarkami, które mogą wykrywać i drukować zwykły tekst. Niektóre z nich nie mogą.

// 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()

Uwagi

Ta metoda służy do zapisywania informacji specyficznych dla urządzenia w pliku buforu, który nie jest automatycznie dołączany przez bufor systemu Microsoft Windows. Oczywiście musisz wiedzieć, czy plik buforu ma wartość Enhanced Metafile (EMF) lub XML Paper Specification (XPS). Jeśli wolisz pracować z interfejsem Stream API, możesz użyć PrintQueueStream klasy zamiast tej metody.

Po wywołaniu AddJob metody należy zapisać tablicę Byte we JobStream właściwości PrintSystemJobInfo zwracanej przez AddJob program lub nie utworzono zadania drukowania. Ta tablica jest tym, co drukuje, jeśli drukarka działa i nie jest wstrzymana.

Ostrzeżenie

JobStream Jeśli element nie jest zamknięty Close przed końcem wątku, w którym AddJob jest wywoływany, jest zgłaszany, gdy ten wątek kończy się, InvalidOperationException ponieważ wątek buforu nie może przejąć kontroli nad obiektemStream.

W graficznym interfejsie użytkownika (GUI) kolejki wydruku zadanie ma nazwę "Print System Document". Aby nadać zadaniu inną nazwę, użyj AddJob(String) przeciążenia.

Inne sposoby drukowania w programie Windows Presentation Foundation (WPF) obejmują metodę PrintDialog.PrintDocument , która może być używana z lub bez otwierania okna dialogowego XpsDocumentWriteroraz wiele Write metod i WriteAsync .

Dotyczy

AddJob(String)

Wstawia nowe zadanie drukowania, którego zawartość jest tablicą Byte , do kolejki.

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

Parametry

jobName
String

Nazwa zadania drukowania.

Zwraca

Element PrintSystemJobInfo reprezentujący zadanie drukowania i jego stan.

Przykłady

W poniższym przykładzie pokazano, jak odczytywać AddJob(String) plik do Byte tablicy i wysyłać tablicę do kolejki wydruku. W tym kodzie przyjęto założenie, że w katalogu głównym dysku C: istnieje plik o nazwie test.txt. Ten kod działa tylko z drukarkami, które mogą wykrywać i drukować zwykły tekst. Niektóre z nich nie mogą.

// 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()

Uwagi

Ta metoda służy do zapisywania informacji specyficznych dla urządzenia w pliku buforu, który nie jest automatycznie dołączany przez bufor systemu Microsoft Windows. Oczywiście musisz wiedzieć, czy plik buforu ma wartość Enhanced Metafile (EMF) lub XML Paper Specification (XPS). Jeśli wolisz pracować z interfejsem Stream API, możesz użyć PrintQueueStream klasy zamiast tej metody.

Po wywołaniu AddJob metody należy zapisać tablicę Byte we JobStream właściwości PrintSystemJobInfo zwracanej przez AddJob program lub nie utworzono zadania drukowania. Ta tablica jest tym, co drukuje, jeśli drukarka działa i nie jest wstrzymana.

Ostrzeżenie

JobStream Jeśli element nie jest zamknięty Close przed końcem wątku, w którym AddJob jest wywoływany, jest zgłaszany, gdy ten wątek kończy się, InvalidOperationException ponieważ wątek buforu nie może przejąć kontroli nad obiektemStream.

Inne sposoby drukowania w programie Windows Presentation Foundation (WPF) obejmują metodę PrintDialog.PrintDocument , która może być używana z lub bez otwierania okna dialogowego XpsDocumentWriteroraz wiele Write metod i WriteAsync .

Dotyczy