new Printable() { @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO_SUCH_PAGE; } // graphics を使って描画処理 return PAGE_EXISTS } }印刷範囲外は、NO_SUCH_PAGE を返し、印刷範囲内であれば、引数の graphics を使って描画処理を行い PAGE_EXISTS を返す。
1000px x 1000px の画像を作成し、それを 451px x 698px に縮小して印刷
package com.snail.exam.printexam;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
public class AWTPrintExam {
private static final double CM = 72.0 / 2.54;
public static void main(String[] args) {
try {
// A4横
Paper PAPER = new Paper();
PAPER.setSize(21.0 * CM, 29.5 * CM);
PAPER.setImageableArea(
1.5 * CM,
1.5 * CM,
17.0 * CM,
27.0 * CM);
PageFormat pageFormat = new PageFormat();
pageFormat.setPaper(PAPER);
// pageFormat.setOrientation(PageFormat.LANDSCAPE); // こっちで指定しても反映されない PrintRequestAttributeSet に指定
// 描画クラス
Printable printable = new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return NO_SUCH_PAGE;
}
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);
System.out.println("余白(左): " + (int) pageFormat.getImageableX() + "px");
System.out.println("余白(上): " + (int) pageFormat.getImageableY() + "px");
System.out.println("印刷領域: "
+ (int) pageFormat.getImageableWidth() + "px x "
+ (int) pageFormat.getImageableHeight() + "px");
graphics.drawImage(
image
, (int) pageFormat.getImageableX()
, (int) pageFormat.getImageableY()
, (int) pageFormat.getImageableHeight()
, (int) pageFormat.getImageableWidth()
, null);
return PAGE_EXISTS;
} catch (Exception e) {
return NO_SUCH_PAGE;
}
}
};
// プリンタを列挙
PrintService[] services = PrinterJob.lookupPrintServices();
// 最初に見つかったプリンタをデフォルト値として印刷ダイアログを立ち上げる
if (services.length > 0) {
// PrintJob
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(printable, pageFormat);
printJob.setPrintService(services[0]);
printJob.setCopies(1);
// aset に指定した属性のうち、用紙の向きだけ有効になる
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.LANDSCAPE);
if (printJob.printDialog(aset)) {
printJob.print(aset);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ぼやけている。10ptの文字は判別できない