PrintQueue.AddJob Metoda

Definicja

Wstawia nowe zadanie drukowania do kolejki.

Przeciążenia

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 podaje 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 Windows Presentation Foundation (WPF) obejmują metodęPrintDialog.PrintDocument, która może być używana z lub bez otwierania okna dialogowego oraz wiele Write metod i WriteAsync .XpsDocumentWriter

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 w celu szybkiego buforowania bez opinii o postępie strony i bez sprawdzania poprawności pliku 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 podaje 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 w celu szybkiego buforowania bez opinii o postępie strony i bez sprawdzania poprawności pliku 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 jest to true, drukarka musi być omówieniem drukowania. Jeśli tak nie jest, AddJob(String, String, Boolean) metoda zgłasza wyjątek.

Jeśli fastCopy jest to false, nie trzeba używać 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 mieszkania wielowątkowego (MTA), który jest wartością domyślną w programie Microsoft .NET 2.0 lub nowszym. (Aby uzyskać więcej informacji na temat wątków i stanów mieszkania, zobacz Managed and Unmanaged Threading i ApartmentState.) Istnieją dwa sposoby wykonywania następujących czynności:

  • 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 , MTAmożesz pomieścić wywołanie w AddJob(String, String, Boolean) osobnym wątku, którego stan mieszkania jest ustawiony na STA .SetApartmentState W poniższym przykładzie pokazano tę drugą technikę.

Uwaga

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

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

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 używać AddJob() funkcji wysyłania Byte tablicy 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 napisać tablicę Byte do JobStream właściwości PrintSystemJobInfo zwracanej przez AddJob zadanie drukowania lub nie zostanie utworzone żadne zadanie drukowania. Ta tablica jest tym, co drukuje, jeśli drukarka działa i nie jest wstrzymana.

Przestroga

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

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

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

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 AddJob(String) odczytać plik w Byte tablicy i wysłać tablicę do kolejki wydruku. W tym kodzie przyjęto założenie, że istnieje plik o nazwie test.txt w katalogu głównym dysku C: . 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 napisać tablicę Byte do JobStream właściwości PrintSystemJobInfo zwracanej przez AddJob zadanie drukowania lub nie zostanie utworzone żadne zadanie drukowania. Ta tablica jest tym, co drukuje, jeśli drukarka działa i nie jest wstrzymana.

Przestroga

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

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

Dotyczy