java

位置:IT落伍者 >> java >> 浏览文章

在Java程序中实现高精度打印


发布日期:2018年06月27日
 
在Java程序中实现高精度打印

在Java环境中可以使用 javaawtToolkitgetScreenResolution()可以得到屏幕每英寸的象素数但是好像没有什么方法能知道某一台打印机的分辨率更别提去控制打印粒度了于是可耻的使用着丑陋的缺省打印精度几年后终于找到了解决方法不知道该高兴还是悲伤其原理说出来也是非常的简单

提高打印精度其实就是把本来是A纸的内容往A纸里画也就是说打印区域(这里对应着Java里的Graphics对象)需要缩小然后由于缺省情况下打印是照DPI来打的不做改变的话打印内容也会跟着变小这样就不是我们想要的效果了所以还得把打印内容成比例放大一个缩小一个放大于是画完后在指定大小的纸张内便容纳了比以往更多象素的内容这下世界总算完美了

以上做法形象的说应该是这样把需要产生的图形对象先放大画在一张纸上然后整体缩小这样精度就提高了

tips 在一般企业报表表格打印中使用DPI得到的表格线的宽度看起来最舒服

tips 现在号称DPI的打印机其实是DPI如果想使用这个分辨率的精度需要用好一点的纸张因为已经到极限了纸张稍差点打印墨粉就沾不上导致线体残缺

附源码(修改分辨率就改动变量iResMul就好)

import javaawt*;

import javaawtprint*;

public class MyPrintableObject implements Printable {

public int iResMul = ; // = dpi; = dpi

public int print(Graphics g PageFormat pf int iPage)

throws PrinterException {

final int FONTSIZE = ;

final double PNT_MM = / ;

if ( != iPage)

return NO_SUCH_PAGE;

try {

int iPosX = ;

int iPosY = ;

int iAddY = FONTSIZE * / * iResMul;

int iWdth = (int) Mathround(pfgetImageableWidth() * iResMul) ;

int iHght = (int) Mathround(pfgetImageableHeight() * iResMul) ;

int iCrcl = Mathmin(iWdth iHght) * iResMul;

GraphicsD g = (GraphicsD) g;

PrinterJob prjob = ((PrinterGraphics) g)getPrinterJob();

gtranslate(pfgetImageableX() pfgetImageableY());

gscale( / iResMul / iResMul);

gsetFont(new Font(SansSerif FontPLAIN FONTSIZE * iResMul));

gsetColor(Colorblack);

gdrawRect(iPosX iPosY iWdth iHght);

gdrawLine(iPosX iHght / + iWdth / iPosX + iWdth iHght /

iWdth / );

gdrawLine(iPosX iHght / iWdth / iPosX + iWdth iHght /

+ iWdth / );

gdrawOval(iPosX + * iResMul iHght iCrcl * iResMul

iCrcl iCrcl);

iPosX += iAddY;

iPosY += iAddY / ;

gdrawString(PrinterJobUserName: + prjobgetUserName() iPosX

iPosY += iAddY);

gdrawString(Betriebssystem: + SystemgetProperty(osname)

+ + SystemgetProperty(osversion) iPosX

iPosY += iAddY);

gdrawString(JavaVersion: JDK

+ SystemgetProperty(javaversion) iPosX

iPosY += iAddY);

gdrawString(Width/Height: + dbldgt(pfgetWidth()) + /

+ dbldgt(pfgetHeight()) + points =

+ dbldgt(pfgetWidth() * PNT_MM) + /

+ dbldgt(pfgetHeight() * PNT_MM) + mm iPosX

iPosY += iAddY);

gdrawString(Imageable Width/Height:

+ dbldgt(pfgetImageableWidth()) + /

+ dbldgt(pfgetImageableHeight()) + points =

+ dbldgt(pfgetImageableWidth() * PNT_MM) + /

+ dbldgt(pfgetImageableHeight() * PNT_MM) + mm iPosX

iPosY += iAddY);

gdrawString(Imageable X/Y: + dbldgt(pfgetImageableX())

+ / + dbldgt(pfgetImageableY()) + points =

+ dbldgt(pfgetImageableX() * PNT_MM) + /

+ dbldgt(pfgetImageableY() * PNT_MM) + mm iPosX

iPosY += iAddY);

gdrawString(versuchte Druckaufl sung: + * iResMul + dpi

iPosX iPosY += iAddY);

} catch (Exception ex) {

throw new PrinterException(exgetMessage());

}

return PAGE_EXISTS;

}

private static double dbldgt(double d) {

return Mathround(d * ) / ; // show one digit after point

}

public static void main(String[] args) {

PrinterJob pj = PrinterJobgetPrinterJob();

pjsetPrintable(new MyPrintableObject());

if (pjprintDialog()) {

try {

pjprint();

} catch (PrinterException e) {

Systemoutprintln(e);

}

}

}

}

               

上一篇:Spring AOP面向方面编程原理:AOP概念

下一篇:Java字符串比较注意点