Last active
February 24, 2019 06:20
-
-
Save gexiangdong/af18ff74b77e9529e4e8fb5c1dac4d1c to your computer and use it in GitHub Desktop.
Java调用默认打印机打印的例子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.devmgr.tutorial.client; | |
import java.awt.*; | |
import java.awt.print.*; | |
public class PrintToPrinter { | |
public static void main(String[] args) throws Exception { | |
Book book = new Book(); | |
PageFormat pf = new PageFormat(); | |
pf.setOrientation(PageFormat.PORTRAIT); | |
Paper p = new Paper(); | |
p.setSize(590, 840); | |
p.setImageableArea(10, 10, 590, 840); | |
pf.setPaper(p); | |
book.append(new OneLabel(), pf); | |
PrinterJob job = PrinterJob.getPrinterJob(); | |
job.setPageable(book); | |
job.print(); | |
} | |
/** | |
* 实现Printable即可打印 | |
*/ | |
public static class OneLabel implements Printable { | |
@Override | |
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { | |
if (pageIndex != 0) { | |
// 此例子只有一页; | |
return NO_SUCH_PAGE; | |
} | |
Graphics2D g2 = (Graphics2D) graphics; | |
double scale = 72.0 / 300; //屏幕DPI是72;打印机DPI:300; 可以实现打印的更精细,特别是图片 | |
g2.scale(scale, scale); | |
g2.setColor(Color.black); | |
double x = pageFormat.getImageableX(); | |
double y = pageFormat.getImageableY(); | |
System.out.println("左上角:" + x + "," + y + " 宽高: " + pageFormat.getWidth() + "," + pageFormat.getHeight()); | |
String str = "中文字符串"; | |
Font font = new Font("微软雅黑", Font.PLAIN, 10); | |
g2.setFont(font); | |
g2.drawString(str, (float) x, (float) (y + 20)); | |
Font font2 = new Font("微软雅黑", Font.PLAIN, 20); | |
g2.setFont(font2); | |
g2.drawString(str, (float) x, (float) (y + 80)); | |
return PAGE_EXISTS; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment