PrintQueue.AddJob メソッド

定義

キューに新しい印刷ジョブを挿入します。

オーバーロード

AddJob(String, String, Boolean, PrintTicket)

XML Paper Specification (XPS) ドキュメントの新しい印刷ジョブをキューに挿入し、指定した名前と設定を指定し、検証するかどうかを指定します。

AddJob(String, PrintTicket)

XML Paper Specification (XPS) ドキュメントの新しい印刷ジョブをキューに挿入し、指定した名前と設定を指定します。

AddJob(String, String, Boolean)

XML Paper Specification (XPS) ドキュメントの新しい印刷ジョブをキューに挿入し、指定した名前を指定し、検証するかどうかを指定します。

AddJob()

Byte 配列の内容を持つ新しい (汎用的な名前が付いた) 印刷ジョブをキューに挿入します。

AddJob(String)

Byte 配列の内容を持つ新しい印刷ジョブをキューに挿入します。

注釈

キューが一時停止またはエラー状態でない限り、ジョブはキューの一番上に達したときに出力されるため、これは印刷機能です。

Windows Presentation Foundation (WPF) で印刷するその他の方法には、 メソッドが含まれますPrintDialog.PrintDocument。このメソッドは、ダイアログを開くかどうかにかかわらず使用できます。また、 の多くの Write メソッドと WriteAsync メソッドもXpsDocumentWriter使用できます。

AddJob(String, String, Boolean, PrintTicket)

XML Paper Specification (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

印刷ジョブの設定。

戻り値

印刷ジョブおよびそのステータスを表す PrintSystemJobInfo

注釈

詳細については、「AddJob(String, String, Boolean)」を参照してください。

適用対象

AddJob(String, PrintTicket)

XML Paper Specification (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

印刷ジョブの設定。

戻り値

印刷ジョブおよびそのステータスを表す PrintSystemJobInfo

注釈

詳細については、「AddJob(String)」を参照してください。

適用対象

AddJob(String, String, Boolean)

XML Paper Specification (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

戻り値

印刷ジョブおよびそのステータスを表す PrintSystemJobInfo

次の例では、 を使用 AddJob(String, String, Boolean) して、ディレクトリ内のすべての XML Paper Specification (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

注釈

が の場合fastCopy、プリンターは印刷の概要である必要があります。true そうでない場合、メソッドは AddJob(String, String, Boolean) 例外をスローします。

falseの場合fastCopy、XPSDrv プリンターを使用する必要はありません。 キューに追加される XPS ファイルは、PCL や Postscript などのプリンターのページ記述言語に変換されます。 ただし、この種の印刷では、コンポーネント オブジェクト モデル (COM) を呼び出します。 COM の呼び出しでは、呼び出し元スレッドに、Microsoft .NET 2.0 以降の既定値であるマルチスレッド アパートメント () ではなく、シングルSTAスレッド アパートメント (MTA) が必要です。 (スレッドとアパートメントの状態の詳細については、「 マネージドスレッドとアンマネージドスレッド」および ApartmentState「」を参照してください)。これを行う方法は 2 つあります。

  • 最も簡単な方法は、アプリケーションのSTAThreadAttributeメソッドの最初の行 (通常は "[System.STAThreadAttribute()]") のすぐ上に ( "static void Main(string[] args)") を追加Mainすることです。

  • スレッドのアパートメント状態を Main にする必要がある場合はMTA、 でアパートメント状態が に設定されている別のスレッドで の呼び出しAddJob(String, String, Boolean)STASetApartmentState格納できます。 次の例は、この 2 番目の手法を示しています。

注意

を以外MainSTAThreadAttributeメソッドに適用することはできません。また、スレッドに Main を使用SetApartmentStateすることはできません。

Windows Presentation Foundation (WPF) で印刷するその他の方法には、 メソッドが含まれますPrintDialog.PrintDocument。このメソッドは、ダイアログを開くかどうかにかかわらず使用できます。また、 の多くの Write メソッドと WriteAsync メソッドもXpsDocumentWriter使用できます。

こちらもご覧ください

適用対象

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) かを知る必要があります。 API を操作 Stream する場合は、このメソッドの代わりに クラスを PrintQueueStream 使用できます。

メソッドがAddJob呼び出されたら、 によってAddJob返される の PrintSystemJobInfo プロパティに配列をJobStream書き込Byteむ必要があります。印刷ジョブは作成されません。 この配列は、プリンターが動作していて、一時停止していない場合に印刷されます。

注意事項

JobStreamが呼び出されるスレッドの終了前に が でCloseAddJob閉じられない場合、InvalidOperationExceptionスプーラー スレッドがオブジェクトを制御できないため、そのスレッドが終了したときに がStreamスローされます。

印刷キューのグラフィカル ユーザー インターフェイス (GUI) では、ジョブの名前は "Print System Document" です。 ジョブに別の名前を付けるには、 オーバーロードを使用します AddJob(String)

Windows Presentation Foundation (WPF) で印刷するその他の方法には、 メソッドが含まれますPrintDialog.PrintDocument。このメソッドは、ダイアログを開くかどうかにかかわらず使用できます。また、 の多くの Write メソッドと WriteAsync メソッドもXpsDocumentWriter使用できます。

適用対象

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) かを知る必要があります。 API を操作 Stream する場合は、このメソッドの代わりに クラスを PrintQueueStream 使用できます。

メソッドがAddJob呼び出されたら、 によってAddJob返される の PrintSystemJobInfo プロパティに配列をJobStream書き込Byteむ必要があります。印刷ジョブは作成されません。 この配列は、プリンターが動作していて、一時停止していない場合に印刷されます。

注意事項

JobStreamが呼び出されるスレッドの終了前に が でCloseAddJob閉じられない場合、InvalidOperationExceptionスプーラー スレッドがオブジェクトを制御できないため、そのスレッドが終了したときに がStreamスローされます。

Windows Presentation Foundation (WPF) で印刷するその他の方法には、 メソッドが含まれますPrintDialog.PrintDocument。このメソッドは、ダイアログを開くかどうかにかかわらず使用できます。また、 の多くの Write メソッドと WriteAsync メソッドもXpsDocumentWriter使用できます。

適用対象