2

私はUbuntuでデータを印刷するための小さなアプリを開発しています.問題は、私のアプリが以下を使用してWindowsで正常に動作することです:

PrintService service = PrintServiceLookup.lookupDefaultPrintService();

FileInputStream fis = new FileInputStream(myfile);

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob job = service.createPrintJob();

Doc doc = new SimpleDoc(fis, flavor, null);
job.print(doc, null);
fis.close();

ただし、Ubuntuでは、印刷されません。使用している印刷 API 用の Linux 印刷用の特別な構成はありますか? それとも、他に何か不足していますか?

4

1 に答える 1

2

お使いのプリンターは、OS のデフォルトとしてインストールされていないと思います。あなたの「サービス」が何であるかを確認してください。次のように、印刷ダイアログからプリンターを選択することもできます。

PrintRequestAttributeSet pras =
                new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
        PrintRequestAttributeSet aset =
                new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(new Copies(1));
        aset.add(Sides.ONE_SIDED);
        aset.add(Finishings.STAPLE);

        PrintService printService[] =
                PrintServiceLookup.lookupPrintServices(flavor, pras);
        PrintService defaultService =
                PrintServiceLookup.lookupDefaultPrintService();
        PrintService service = ServiceUI.printDialog(null, 200, 200,
                printService, defaultService, flavor, pras);
        if (service != null) {
            try {
                FileInputStream fis = new FileInputStream("c://test.txt");
                DocAttributeSet das = new HashDocAttributeSet();
                Doc doc1 = new SimpleDoc(fis, flavor, das);

                DocPrintJob job1 = service.createPrintJob();

                try {
                    job1.print(doc1, pras);
                } catch (PrintException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

一部のプリンターは、テキスト DocFlavor をサポートせず、画像のみをサポートします。また、次のような OS ネイティブ メソッドを使用して、html ファイルを単純に印刷することもできます。

if (Desktop.isDesktopSupported()){
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.PRINT))
    {
        try {
            File html1 = new File("c://file1.html");
            desktop.print(html1);
            desktop.print(html2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

于 2012-04-11T14:45:50.020 に答える