2016-05-20 12 views
0

ファイルを印刷したいと思います。このコードを作成しましたが、ファイルを印刷しません。印刷用ファイルが印刷されていません。Javaのプリンタデバイス経由で

 String printerName = "Canon MF4320-4350".toLowerCase(); 
    PrintService service = null; 

    PrintService[] services = PrinterJob.lookupPrintServices(); 

    // Retrieve a print service from the array 
    for (int index = 0; service == null && index < services.length; index++) { 

     if (services[index].getName().toLowerCase().indexOf(printerName) >= 0) { 
      service = services[index]; 
     } 
    } 

:: - Wordがdocxファイルを出力するとき、それはプリンタにファイルの生のバイトを送信しません

byte[] bytes = null; 
    Path path = FileSystems.getDefault().getPath("D:\\Test.docx"); 
    bytes = Files.readAllBytes(path); 
    DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; 
    PrintRequestAttributeSet faset = new HashPrintRequestAttributeSet(); 
    faset.add(new Copies(5)); 
    faset.add(Sides.ONE_SIDED); 
    DocAttributeSet daset = new HashDocAttributeSet(); 
    daset.add(OrientationRequested.LANDSCAPE); 
    daset.add(Sides.ONE_SIDED); 
    Doc myDoc = new SimpleDoc(bytes, docFlavor, daset); 
    //create the DocPrintJob 
    DocPrintJob job = service.createPrintJob(); 
    job.print(myDoc, faset); 

答えて

1

印刷の仕組みではないサービスを選択のための 。代わりに、ファイルを低レベルの印刷コマンドに変換し、印刷コマンドを印刷ドライバに送信します。

Java印刷APIは2d rendering via drawing commandsを処理できます。または、プリンタに未処理のバイトストリームを送信することはできますが、プリンタが認識できるバイト数を設定するのはあなたの責任です。

this questionも参照してください。docx4jをご覧ください。

+0

ありがとう、アドリアン、しかし、私がここに.txtファイルがあれば、それはうまくいきません。 – John

+1

バイトストリームをプリンタに送信して印刷する場合は、お使いのプリンタ用のLinux印刷(https://wiki.debian.org/PrinterDriver/Canon/UFR-II)で使用されるフィルタが最適です。キャノンのインクジェットは、UFRへの翻訳なしで平文を印刷するとは思わない。 –

+0

あなたより:)、別の方法がありますが、プリンタにバイトストリームを送信する以外にはありますか? – John

関連する問題