8

次のコードを使用して、「Hello World」を出力する単純な Java アプリケーションの作成を開始しました。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;

public class HelloWorldPrinter implements Printable, ActionListener { 

public int print(Graphics g, PageFormat pf, int page) throws
                                                    PrinterException {

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    /* Now we perform our rendering */
    g.drawString("Hello world!", 100, 100);

    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

public void actionPerformed(ActionEvent e) {
     PrinterJob job = PrinterJob.getPrinterJob();
     job.setPrintable(this);
     boolean ok = job.printDialog();
     if (ok) {
         try {
              job.print();
         } catch (PrinterException ex) {
          /* The job did not successfully complete */
         }
     }
}

public static void main(String args[]) {

    UIManager.put("swing.boldMetal", Boolean.FALSE);
    JFrame f = new JFrame("Hello World Printer");
    f.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    JButton printButton = new JButton("Print Hello World");
    printButton.addActionListener(new HelloWorldPrinter());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);
}
} 

これは機能しません。「印刷サービスが見つかりません」という警告メッセージが表示されます。

Ubuntu 12.04 を使用しており、Java バージョンは 1.7.0_25 です。

このアラートを取り除くにはどうすればよいですか?

4

3 に答える 3

9

cups-pdf をインストールすると、PDF ファイルを作成する仮想プリンターがインストールされます。

sudo apt-get install cups-pdf
于 2014-05-04T05:41:12.870 に答える
0

MacOS の場合、私は PDFWriter を使用しました:

  1. インストーラーはこちらから入手できます

2. パッケージをインストールした後、戻ってそれをクリックします -> [パッケージの内容を表示]。3. そこから Archive.pax.gz を解凍し、PDFwriter.ppd ファイルを /Library/Printers/PPDs/Contents/Resources にコピーします。最後の 3 つのフォルダーを作成する必要がある場合があります。また、Library フォルダーが見つからない場合は、デフォルトで非表示になっているため、Shift + Command + "." を押してください。4. [設定] の [プリンター] ダイアログに移動し、プリンターを追加します。

于 2021-09-27T14:34:11.257 に答える