在Java环境中,可以使用 java.awt.Toolkit.getScreenResolution()可以得到屏幕每英寸的象素数,但是好像没有什么方法能知道某一台打印机的分辨率,更别提去控制打印粒度了。于是可耻的使用着丑陋的缺省打印精度几年后,终于找到了解决方法,不知道该高兴还是悲伤,其原理说出来也是非常的简单:
提高打印精度,其实就是把本来是A3纸的内容往A4纸里画,也就是说,打印区域(这里对应着Java里的Graphics对象)需要缩小,然后由于缺省情况下打印是照72DPI来打的,不做改变的话,打印内容也会跟着变小。这样就不是我们想要的效果了,所以还得把打印内容成比例放大。一个缩小,一个放大,于是画完后,在指定大小的纸张内,便容纳了比以往更多象素的内容,这下世界总算完美了。
以上做法形象的说应该是这样:把需要产生的图形对象先放大,画在一张“纸上”,然后整体缩小,这样精度就提高了。
tips 1:在一般企业报表表格打印中,使用144DPI得到的表格线的宽度看起来最舒服。
tips 2:现在号称600DPI的打印机其实是576DPI,如果想使用这个分辨率的精度,需要用好一点的纸张,因为已经到极限了,纸张稍差点,打印墨粉就沾不上,导致线体残缺。
附源码(修改分辨率就改动变量iResMul就好):
import java.awt.*;
import java.awt.print.*;
public class MyPrintableObject implements Printable {
public int iResMul = 1; // 1 = 72 dpi; 4 = 288 dpi
public int print(Graphics g, PageFormat pf, int iPage)
throws PrinterException {
final int FONTSIZE = 12;
final double PNT_MM = 25.4 / 72.;
if (0 != iPage)
return NO_SUCH_PAGE;
try {
int iPosX = 1;
int iPosY = 1;
int iAddY = FONTSIZE * 3 / 2 * iResMul;
int iWdth = (int) Math.round(pf.getImageableWidth() * iResMul) - 3;
int iHght = (int) Math.round(pf.getImageableHeight() * iResMul) - 3;
int iCrcl = Math.min(iWdth, iHght) - 4 * iResMul;
Graphics2D g2 = (Graphics2D) g;
PrinterJob prjob = ((PrinterGraphics) g2).getPrinterJob();
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.scale(1.0 / iResMul, 1.0 / iResMul);
g2.setFont(new Font("SansSerif", Font.PLAIN, FONTSIZE * iResMul));
g2.setColor(Color.black);
g2.drawRect(iPosX, iPosY, iWdth, iHght);
g2.drawLine(iPosX, iHght / 2 + iWdth / 50, iPosX + iWdth, iHght / 2
- iWdth / 50);
g2.drawLine(iPosX, iHght / 2 - iWdth / 50, iPosX + iWdth, iHght / 2
+ iWdth / 50);
g2.drawOval(iPosX + 2 * iResMul, iHght - iCrcl - 2 * iResMul,
iCrcl, iCrcl);
iPosX += iAddY;
iPosY += iAddY / 2;
g2.drawString("PrinterJob-UserName: " + prjob.getUserName(), iPosX,
iPosY += iAddY);
g2.drawString("Betriebssystem: " + System.getProperty("os.name")
+ " " + System.getProperty("os.version"), iPosX,
iPosY += iAddY);
g2
.drawString("Java-Version: JDK "
+ System.getProperty("java.version"), iPosX,
iPosY += iAddY);
g2.drawString("Width/Height: " + dbldgt(pf.getWidth()) + " / "
+ dbldgt(pf.getHeight()) + " points = "
+ dbldgt(pf.getWidth() * PNT_MM) + " / "
+ dbldgt(pf.getHeight() * PNT_MM) + " mm", iPosX,
iPosY += iAddY);
g2.drawString("Imageable Width/Height: "
+ dbldgt(pf.getImageableWidth()) + " / "
+ dbldgt(pf.getImageableHeight()) + " points = "
+ dbldgt(pf.getImageableWidth() * PNT_MM) + " / "
+ dbldgt(pf.getImageableHeight() * PNT_MM) + " mm", iPosX,
iPosY += iAddY);
g2.drawString("Imageable X/Y: " + dbldgt(pf.getImageableX())
+ " / " + dbldgt(pf.getImageableY()) + " points = "
+ dbldgt(pf.getImageableX() * PNT_MM) + " / "
+ dbldgt(pf.getImageableY() * PNT_MM) + " mm", iPosX,
iPosY += iAddY);
g2.drawString("versuchte Druckaufl sung: " + 72 * iResMul + " dpi",
iPosX, iPosY += iAddY);
} catch (Exception ex) {
throw new PrinterException(ex.getMessage());
}
return PAGE_EXISTS;
}
private static double dbldgt(double d) {
return Math.round(d * 10.) / 10.; // show one digit after point
}
public static void main(String[] args) {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new MyPrintableObject());
if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException e) {
System.out.println(e);
}
}
}
}
查看本文来源