Java: 设置可打印宽度(页面格式)
Java: Setting printable width (Pageformat)
我正在尝试从我的 java 应用程序打印到收据打印机,
收据的宽度是58mm,好像边距不对,打印两边各1英寸的边距。这导致只打印 3 letters/numbers 而不是整行。
我可以从记事本成功打印,因为我手动将两边的边距调整为 1.97mm,这似乎可以解决问题。
我的代码如下;
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 打印并且一直在努力,可能有复制在线资源的冗余代码。我已经调整了字体,所以它变小了,但没有太大帮助。
我已经想出了一个解决方法来获得想要的结果,只是在文本前添加空格似乎效果很好,我还调整了代码如下;
public void print() throws PrintException, IOException {
String defaultPrinter =
PrintServiceLookup.lookupDefaultPrintService().getName();
System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
InputStream is = new ByteArrayInputStream(printableAmounts.getBytes("UTF8"));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);
DocPrintJob job = service.createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();
is.close();
}
似乎是一个临时解决方案,但如果没有其他问题,它将成为永久解决方案。
我正在尝试从我的 java 应用程序打印到收据打印机, 收据的宽度是58mm,好像边距不对,打印两边各1英寸的边距。这导致只打印 3 letters/numbers 而不是整行。 我可以从记事本成功打印,因为我手动将两边的边距调整为 1.97mm,这似乎可以解决问题。
我的代码如下;
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 打印并且一直在努力,可能有复制在线资源的冗余代码。我已经调整了字体,所以它变小了,但没有太大帮助。
我已经想出了一个解决方法来获得想要的结果,只是在文本前添加空格似乎效果很好,我还调整了代码如下;
public void print() throws PrintException, IOException {
String defaultPrinter =
PrintServiceLookup.lookupDefaultPrintService().getName();
System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
InputStream is = new ByteArrayInputStream(printableAmounts.getBytes("UTF8"));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);
DocPrintJob job = service.createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();
is.close();
}
似乎是一个临时解决方案,但如果没有其他问题,它将成为永久解决方案。