0
我试图从我的java应用程序打印到收据打印机, 收据的宽度为58mm,似乎边距不正确,并且打印的边距为1英寸任何一边。这导致只打印3个字母/数字而不是整行。 我可以成功地从记事本打印,因为我已经手动调整边缘到1.97毫米在任何一方似乎做的伎俩。Java:设置可打印宽度(Pageformat)
我的代码如下;
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
Font font = new Font("MONOSPACED", Font.PLAIN, 10);
FontMetrics metrics = g.getFontMetrics(font);
int lineHeight = metrics.getHeight();
if (pageBreaks == null) {
initTextLines();
int linesPerPage = (int)(pf.getImageableHeight()/lineHeight);
int numBreaks = (textLines.length-1)/linesPerPage;
pageBreaks = new int[numBreaks];
for (int b=0; b<numBreaks; b++) {
pageBreaks[b] = (b+1)*linesPerPage;
}
}
if (pageIndex > pageBreaks.length) {
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
* Since we are drawing text we
*/
Graphics2D g2d = (Graphics2D)g;
g2d.setFont(new Font("MONOSPACED", Font.PLAIN, 10));
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Draw each line that is on this page.
* Increment 'y' position by lineHeight for each line.
*/
int y = 0;
int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex-1];
int end = (pageIndex == pageBreaks.length)
? textLines.length : pageBreaks[pageIndex];
for (int line=start; line<end; line++) {
y += lineHeight;
g.drawString(textLines[line], 0, y);
}
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
我也很感激,如果你能帮助我对齐文本到收货的右手侧,以保持它穿制服用了其它系统,但是我的主要问题是,如果是分类,我将保证金超过月亮:)
谢谢!
p.s.我是新来的java打印和努力,可能有复制在线来源的冗余代码。我已经调整了字体,使它更小,这并没有多大帮助。