PrintQueue.AddJob Metoda

Definice

Vloží novou tiskovou úlohu do fronty.

Přetížení

AddJob(String, String, Boolean, PrintTicket)

Vloží novou tiskovou úlohu pro dokument XPS (XML Paper Specification) do fronty, poskytne jí zadaný název a nastavení a určí, zda má být ověřena.

AddJob(String, PrintTicket)

Vloží novou tiskovou úlohu pro dokument XPS (XML Paper Specification) do fronty a poskytne jí zadaný název a nastavení.

AddJob(String, String, Boolean)

Vloží novou tiskovou úlohu pro dokument XPS (XML Paper Specification) do fronty, poskytne jí zadaný název a určí, zda má být ověřena.

AddJob()

Vloží do fronty novou (obecně pojmenovanou) tiskovou úlohu, jejímž obsahem Byte je pole.

AddJob(String)

Vloží do fronty novou tiskovou úlohu, jejímž obsahem Byte je pole.

Poznámky

Pokud není fronta pozastavená nebo v chybovém stavu, úloha se při dosažení horní části fronty vytiskne, takže se jedná o tiskovou funkci.

Další způsoby tisku v Windows Presentation Foundation (WPF) zahrnují metoduPrintDialog.PrintDocument, kterou lze použít s otevřením dialogového okna nebo bez otevření dialogového okna, a mnoho Write metod a WriteAsync metody XpsDocumentWriter.

AddJob(String, String, Boolean, PrintTicket)

Vloží novou tiskovou úlohu pro dokument XPS (XML Paper Specification) do fronty, poskytne jí zadaný název a nastavení a určí, zda má být ověřena.

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

Cesta a název dokumentu, který se tiskne.

documentPath
String

Cesta a název dokumentu, který se tiskne.

fastCopy
Boolean

truerychle zařazení bez zpětné vazby k průběhu stránky a bez ověření, že soubor je platný XPS; v opačném případě . false

printTicket
PrintTicket

Nastavení tiskové úlohy.

Návraty

A PrintSystemJobInfo , který představuje tiskovou úlohu a její stav.

Poznámky

Další informace naleznete v tématu AddJob(String, String, Boolean).

Platí pro

AddJob(String, PrintTicket)

Vloží novou tiskovou úlohu pro dokument XPS (XML Paper Specification) do fronty a poskytne jí zadaný název a nastavení.

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

Cesta a název dokumentu, který se tiskne.

printTicket
PrintTicket

Nastavení tiskové úlohy.

Návraty

A PrintSystemJobInfo , který představuje tiskovou úlohu a její stav.

Poznámky

Další informace naleznete v tématu AddJob(String).

Platí pro

AddJob(String, String, Boolean)

Vloží novou tiskovou úlohu pro dokument XPS (XML Paper Specification) do fronty, poskytne jí zadaný název a určí, zda má být ověřena.

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

Název tiskové úlohy.

documentPath
String

Cesta a název dokumentu, který se tiskne.

fastCopy
Boolean

truerychle zařazení bez zpětné vazby k průběhu stránky a bez ověření, že soubor je platný XPS; v opačném případě . false

Návraty

A PrintSystemJobInfo , který představuje tiskovou úlohu a její stav.

Příklady

Následující příklad ukazuje, jak použít AddJob(String, String, Boolean) k dávkovému tisku všech souborů XPS (XML Paper Specification) v adresáři.

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

Poznámky

Pokud fastCopy je true, musí být tiskárna přehledem tisku. Pokud není, AddJob(String, String, Boolean) metoda vyvolá výjimku.

Pokud fastCopy je false, není nutné používat tiskárnu XPSDrv. Soubor XPS, který se přidává do fronty, se převede na jazyk popisu stránky tiskárny, například PCL nebo Postscript. Tento druh tisku však vyvolá volání modelu COM (Component Object Model). Volání modelu COM vyžaduje, aby volající vlákno měl jednovláknový byt (STA) místo vícevláknového typu (MTA), což je výchozí hodnota v Microsoft .NET 2.0 a novějších. (Další informace o vláknech a stavech bytu najdete v tématu Spravované a nespravované vytváření vláken a ApartmentState.) Existují dva způsoby, jak to udělat:

  • Nejjednodušším způsobem je přidat STAThreadAttribute (tedy "[System.STAThreadAttribute()]") přímo nad první řádek metody aplikace Main (obvykle "static void Main(string[] args)").

  • Pokud potřebujete, aby MTAstav bytu vlákna Main byl , můžete volání AddJob(String, String, Boolean) umístit do samostatného vlákna, jehož stav bytu je nastaven na .SetApartmentStateSTA Následující příklad znázorňuje tuto druhou techniku.

Poznámka

Nelze použít na žádnou metodu STAThreadAttribute s výjimkou Main a nemůžete použít SetApartmentState pro vlákno Main .

Další způsoby tisku v Windows Presentation Foundation (WPF) zahrnují metoduPrintDialog.PrintDocument, kterou lze použít s otevřením dialogového okna nebo bez otevření dialogového okna, a mnoho Write metod a WriteAsync metody XpsDocumentWriter.

Viz také

Platí pro

AddJob()

Vloží do fronty novou (obecně pojmenovanou) tiskovou úlohu, jejímž obsahem Byte je pole.

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

Návraty

A PrintSystemJobInfo , který představuje tiskovou úlohu a její stav.

Příklady

Následující příklad ukazuje, jak použít AddJob() k odeslání Byte pole do tiskové fronty. Tento kód funguje jenom s tiskárnami, které umí rozpoznat a tisknout prostý text. Někteří z nich nemůžou.

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

Poznámky

Tato metoda slouží k zápisu informací specifických pro zařízení do zařazovacího souboru, který není automaticky součástí zařazování systému Microsoft Windows. Samozřejmě potřebujete vědět, jestli je soubor zařazování EMF (Enhanced Metafile) nebo XPS (XML Paper Specification). Pokud dáváte přednost práci s rozhraním Stream API, můžete místo této metody použít PrintQueueStream třídu .

Po zavolání AddJob metody je nutné zapsat Byte pole do JobStream vlastnosti PrintSystemJobInfo , která je vrácena AddJob , nebo není vytvořena žádná tisková úloha. Toto pole se vytiskne, pokud tiskárna funguje a není pozastavená.

Upozornění

JobStream Pokud není uzavřen s před koncem Close vlákna, ve kterém AddJob je volán, vyvolá InvalidOperationException se při konci tohoto vlákna, protože vlákno zařazování nemůže získat kontrolu nad objektemStream.

V grafickém uživatelském rozhraní (GUI) tiskové fronty má úloha název Tisk dokumentu systému. Pokud chcete úlohu pojmenovat jinak, použijte AddJob(String) přetížení.

Další způsoby tisku v Windows Presentation Foundation (WPF) zahrnují metoduPrintDialog.PrintDocument, kterou lze použít s otevřením dialogového okna nebo bez otevření dialogového okna, a mnoho Write metod a WriteAsync metody XpsDocumentWriter.

Platí pro

AddJob(String)

Vloží do fronty novou tiskovou úlohu, jejímž obsahem Byte je pole.

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

Název tiskové úlohy.

Návraty

A PrintSystemJobInfo , který představuje tiskovou úlohu a její stav.

Příklady

Následující příklad ukazuje, jak použít AddJob(String) ke čtení souboru do Byte pole a odeslání pole do tiskové fronty. Tento kód předpokládá, že v kořenovém adresáři jednotky C: existuje soubor s názvem test.txt. Tento kód funguje jenom s tiskárnami, které umí rozpoznat a tisknout prostý text. Někteří z nich nemůžou.

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

Poznámky

Tato metoda slouží k zápisu informací specifických pro zařízení do zařazovacího souboru, který není automaticky součástí zařazování systému Microsoft Windows. Samozřejmě potřebujete vědět, jestli je soubor zařazování EMF (Enhanced Metafile) nebo XPS (XML Paper Specification). Pokud dáváte přednost práci s rozhraním Stream API, můžete místo této metody použít PrintQueueStream třídu .

Po zavolání AddJob metody je nutné zapsat Byte pole do JobStream vlastnosti PrintSystemJobInfo , která je vrácena AddJob , nebo není vytvořena žádná tisková úloha. Toto pole se vytiskne, pokud tiskárna funguje a není pozastavená.

Upozornění

JobStream Pokud není uzavřen s před koncem Close vlákna, ve kterém AddJob je volán, vyvolá InvalidOperationException se při konci tohoto vlákna, protože vlákno zařazování nemůže získat kontrolu nad objektemStream.

Další způsoby tisku v Windows Presentation Foundation (WPF) zahrnují metoduPrintDialog.PrintDocument, kterou lze použít s otevřením dialogového okna nebo bez otevření dialogového okna, a mnoho Write metod a WriteAsync metody XpsDocumentWriter.

Platí pro