概要

ソースコード

package com.snail.exam.printexam;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.ResolutionSyntax;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaName;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.standard.PrinterResolution;

public class DocPrintExam {

    public static void main(String[] args) {

        try {

            BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);

            Graphics2D g = image.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            g.setColor(Color.WHITE);
            g.fillRect(0, 0, image.getWidth(), image.getHeight());

            g.setColor(Color.GRAY);
            for (int x = 0; x < image.getWidth(); x += 5) {
                g.drawLine(x, 0, x, image.getHeight());
            }
            for (int y = 0; y < image.getHeight(); y += 5) {
                g.drawLine(0, y, image.getWidth(), y);
            }

            g.setColor(Color.BLACK);
            g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 24));
            g.drawString("20pt 123 ABC あいう アイウ", 0, 20);

            g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10));
            g.drawString("10pt 123 ABC あいう アイウ", 0, 40);

            g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 8));
            g.drawString("8pt 123 ABC あいう アイウ", 0, 60);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", bos);
            // JPEG だと、少しぼやけるので、可逆圧縮のPNGを使う

            FileOutputStream fout = new FileOutputStream("/tmp/a.png");
            fout.write(bos.toByteArray());
            fout.close();
            
            // ドキュメントの形式を設定
            DocFlavor myFormat = DocFlavor.BYTE_ARRAY.PNG;

            // Docオブジェクトの生成
            Doc myDoc = new SimpleDoc(bos.toByteArray(), myFormat, null);

            // プリンタの検索条件 兼 印刷パラメータを構築 (A4 600x600dpi以上)
            // (解像度は印刷時に「最高」を指定し直す)
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            aset.add(new Copies(1));
            aset.add(MediaName.ISO_A4_WHITE);
            aset.add(OrientationRequested.LANDSCAPE);
            aset.add(new PrinterResolution(600, 600, ResolutionSyntax.DPI));

            // 属性セットに従って印刷ができるプリンタを発見する
            PrintService[] services = PrintServiceLookup.lookupPrintServices(
                    myFormat, aset);

            // 最初に見つかったプリントサービスを使う
            if (services.length > 0) {

                PrintService ps = ServiceUI.printDialog(null, 50, 50, services, services[0], myFormat, aset);
                if (ps == null) {
                    return;    //キャンセルされた場合
                }

                DocPrintJob job = ps.createPrintJob();

                // 最高品位印刷をプリンタに指示
                aset.add(PrintQuality.HIGH);
                job.print(myDoc, aset);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

実行結果

imga.png

Java#Print


添付ファイル: fileerr.png 1896件 [詳細] fileimga.png 1869件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2011-05-24 (火) 00:34:38 (4714d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=5b8a0f3537
ISBN10
ISBN13
9784061426061