Java高精度打印

    在Java环境中,可以使用 java.awt.Toolkit.getScreenResolution()可以得到屏幕每英寸的象素数,但是好像没有什么方法能知道某一台打印机的分辨率,更别提去控制打印粒度了。于是可耻的使用着丑陋的缺省打印精度几年后,终于找到了解决方法,不知道该高兴还是悲伤,其原理说出来也是非常的简单:
    提高打印精度,其实就是把本来是A3纸的内容往A4纸里画,也就是说,打印区域(这里对应着Java里的Graphics对象)需要缩小,然后由于缺省情况下打印是照72DPI来打的,不做改变的话,打印内容也会跟着变小。这样就不是我们想要的效果了,所以还得把打印内容成比例放大。一个缩小,一个放大,于是画完后,在指定大小的纸张内,便容纳了比以往更多象素的内容,这下世界总算完美了。

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

    tips 1:在一般企业报表表格打印中,使用144DPI得到的表格线的宽度看起来最舒服。
    tips 2:现在号称600DPI的打印机其实是576DPI,如果想使用这个分辨率的精度,需要用好一点的纸张,因为已经到极限了,纸张稍差点,打印墨粉就沾不上,导致线体残缺。

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

 

None.gifimport java.awt.*;
None.gif
import java.awt.print.*;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class MyPrintableObject implements Printable dot.gif{
InBlock.gif 
public int iResMul = 1// 1 = 72 dpi; 4 = 288 dpidot.gif
InBlock.gif

InBlock.gif 
public int print(Graphics g, PageFormat pf, int iPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
throws PrinterException dot.gif{
InBlock.gif  
final int FONTSIZE = 12;
InBlock.gif  
final double PNT_MM = 25.4 / 72.;
InBlock.gif  
if (0 != iPage)
InBlock.gif   
return NO_SUCH_PAGE;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
try dot.gif{
InBlock.gif   
int iPosX = 1;
InBlock.gif   
int iPosY = 1;
InBlock.gif   
int iAddY = FONTSIZE * 3 / 2 * iResMul;
InBlock.gif   
int iWdth = (int) Math.round(pf.getImageableWidth() * iResMul) - 3;
InBlock.gif   
int iHght = (int) Math.round(pf.getImageableHeight() * iResMul) - 3;
InBlock.gif   
int iCrcl = Math.min(iWdth, iHght) - 4 * iResMul;
InBlock.gif   Graphics2D g2 
= (Graphics2D) g;
InBlock.gif   PrinterJob prjob 
= ((PrinterGraphics) g2).getPrinterJob();
InBlock.gif   g2.translate(pf.getImageableX(), pf.getImageableY());
InBlock.gif   g2.scale(
1.0 / iResMul, 1.0 / iResMul);
InBlock.gif   g2.setFont(
new Font("SansSerif", Font.PLAIN, FONTSIZE * iResMul));
InBlock.gif   g2.setColor(Color.black);
InBlock.gif   g2.drawRect(iPosX, iPosY, iWdth, iHght);
InBlock.gif   g2.drawLine(iPosX, iHght 
/ 2 + iWdth / 50, iPosX + iWdth, iHght / 2
InBlock.gif     
- iWdth / 50);
InBlock.gif   g2.drawLine(iPosX, iHght 
/ 2 - iWdth / 50, iPosX + iWdth, iHght / 2
InBlock.gif     
+ iWdth / 50);
InBlock.gif   g2.drawOval(iPosX 
+ 2 * iResMul, iHght - iCrcl - 2 * iResMul,
InBlock.gif     iCrcl, iCrcl);
InBlock.gif   iPosX 
+= iAddY;
InBlock.gif   iPosY 
+= iAddY / 2;
InBlock.gif   g2.drawString(
"PrinterJob-UserName: " + prjob.getUserName(), iPosX,
InBlock.gif     iPosY 
+= iAddY);
InBlock.gif   g2.drawString(
"Betriebssystem: " + System.getProperty("os.name")
InBlock.gif     
+ " " + System.getProperty("os.version"), iPosX,
InBlock.gif     iPosY 
+= iAddY);
InBlock.gif   g2
InBlock.gif     .drawString(
"Java-Version: JDK "
InBlock.gif       
+ System.getProperty("java.version"), iPosX,
InBlock.gif       iPosY 
+= iAddY);
InBlock.gif   g2.drawString(
"Width/Height: " + dbldgt(pf.getWidth()) + " / "
InBlock.gif     
+ dbldgt(pf.getHeight()) + " points = "
InBlock.gif     
+ dbldgt(pf.getWidth() * PNT_MM) + " / "
InBlock.gif     
+ dbldgt(pf.getHeight() * PNT_MM) + " mm", iPosX,
InBlock.gif     iPosY 
+= iAddY);
InBlock.gif   g2.drawString(
"Imageable Width/Height: "
InBlock.gif     
+ dbldgt(pf.getImageableWidth()) + " / "
InBlock.gif     
+ dbldgt(pf.getImageableHeight()) + " points = "
InBlock.gif     
+ dbldgt(pf.getImageableWidth() * PNT_MM) + " / "
InBlock.gif     
+ dbldgt(pf.getImageableHeight() * PNT_MM) + " mm", iPosX,
InBlock.gif     iPosY 
+= iAddY);
InBlock.gif   g2.drawString(
"Imageable X/Y: " + dbldgt(pf.getImageableX())
InBlock.gif     
+ " / " + dbldgt(pf.getImageableY()) + " points = "
InBlock.gif     
+ dbldgt(pf.getImageableX() * PNT_MM) + " / "
InBlock.gif     
+ dbldgt(pf.getImageableY() * PNT_MM) + " mm", iPosX,
InBlock.gif     iPosY 
+= iAddY);
InBlock.gif   g2.drawString(
"versuchte Druckaufl sung: " + 72 * iResMul + " dpi",
InBlock.gif     iPosX, iPosY 
+= iAddY);
ExpandedSubBlockStart.gifContractedSubBlock.gif  }
 catch (Exception ex) dot.gif{
InBlock.gif   
throw new PrinterException(ex.getMessage());
ExpandedSubBlockEnd.gif  }

InBlock.gif  
return PAGE_EXISTS;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
private static double dbldgt(double d) dot.gif{
InBlock.gif  
return Math.round(d * 10.) / 10.; // show one digit after point
ExpandedSubBlockEnd.gif
 }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
public static void main(String[] args) dot.gif{
InBlock.gif  PrinterJob pj 
= PrinterJob.getPrinterJob();
InBlock.gif  pj.setPrintable(
new MyPrintableObject());
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if (pj.printDialog()) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
try dot.gif{
InBlock.gif    pj.print();
ExpandedSubBlockStart.gifContractedSubBlock.gif   }
 catch (PrinterException e) dot.gif{
InBlock.gif    System.out.println(e);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif


全文完)   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值