目次||

PrinterJobを使用したグラフィックスの印刷またはストリーム配信

java.awt.printのAPIは、次のPrinterJobメソッドで構成されます: printDialogおよびpageDialogメソッドは属性セットを取得するため、ユーザーはダイアログからinitial属性設定を編集できます。

アプリケーションでは、PrinterJobを使用して、2Dグラフィックをプリンタまたは出力ストリームに印刷できます。 lookupPrintServicesメソッドは、PrintServiceオブジェクトの配列を返します。各オブジェクトは、2Dグラフィックを印刷できるプリンタを表します。 lookupStreamPrintServicesメソッドは、StreamPrintServiceFactoryオブジェクトの配列を返します。各オブジェクトの配列は、StreamPrintServiceを返します。 アプリケーションは、StreamPrintServiceを使用して出力ストリームに印刷データを送信します。 印刷ドキュメントと同様に、アプリケーションはStreamPrintServiceを使用して、2Dグラフィックを他のフォーマットにトランスコードできます。 この項では、PrinterJobを使用して、2Dグラフィックをプリンタおよび出力ストリームに送信する方法について説明します。


2Dグラフィックスの印刷

pageDialogprintDialogおよびprintメソッドを使用すると、アプリケーションで印刷設定を初期化し、これらの設定をダイアログに渡すことができるため、ユーザーは、次のコード・サンプルで示すように、印刷リクエストを送信する前に設定を更新できます:

// Step 1: Set up initial print settings.
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// Step 2: Obtain a print job.
PrinterJob pj = PrinterJob.getPrinterJob();
// Step 3: Find print services.
PrintService []services = PrinterJob.lookupPrintServices();
if (services.length > 0) {
        System.out.println("selected printer: " + services[0]);
        try {
        pj.setPrintService(service[0]); 
        // Step 2: Pass the settings to a page dialog and print dialog.
        pj.pageDialog(aset);
        if (pj.printDialog(aset)) {
                // Step 4: Update the settings made by the user in the dialogs.
                // Step 5: Pass the final settings into the print request.
                pj.print(aset);
        }
        } catch (PrinterException(pe)) {
                System.err.println(pe):
        }
}
アプリケーション全体については、「例: Print2DPrinterJob.java」を参照してください。

このコード例のステップ4は、どの特定のコード行にも対応していないように見えます。 実際、ユーザーはダイアログの印刷設定を更新し、更新された設定はPrintRequestAttributeSetasetに保存されます。

Java 2DとJava印刷サービスを一緒に使用した場合の1つの問題として、印刷部数などの一部の属性が両方のAPIで定義されることがあります。 このような属性がPrintRequestAttributeSetに指定されている場合は、PrinterJobで指定された属性よりも優先されます。 ユーザーが印刷ダイアログのコピー数を更新すると、PrinterJobが自動的に更新され、既存の動作が再確認されます。

PageFormat指定は、Java Print ServiceのMediaMediaPrintableAreaおよびOrientationRequested属性とも重複します。 アプリケーションがPrintableインタフェースおよびprint(PrintRequestAttributeSet)メソッドを使用する場合、属性セットに含まれるメディア、方向およびイメージ可能領域属性が新しいPageFormatに追加され、Printableオブジェクトのprintメソッドに渡されます。 アプリケーションでPageableインタフェースが使用されている場合、PageFormatは変更されません。


2Dグラフィックスのストリーム配信

アプリケーションは、PrinterJobおよびStreamPrintServiceを使用して、出力データを出力ストリームに送信することもできます。 この例は、前の項の例に似ていますが、PrintServiceのかわりにStreamPrintServiceが使用されています:

PrinterJob job = PrinterJob.getPrinterJob();
String psMimeType = "application/postscript";
FileOutputStream outstream; 
StreamPrintService psPrinter; 
StreamPrintServiceFactory []spsFactories =
        PrinterJob.lookupStreamPrintServices(psMimeType);
if (factories.length > 0) { 
        try { 
                outstream = new File("out.ps"); 
                psPrinter = factories[0].getPrintService(fos); 
                // psPrinter can now be set as the service on a PrinterJob 
        } catch (FileNotFoundException e) { } 
} 
job.setPrintService(service[0]); // if app wants to specify this printer.
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(2));
job.print(aset);
}


目次||

Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved.