在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);
}
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26417.html