在热敏打印机上打印Java
我使用以下代码在80毫米卷筒纸热敏打印机上打印一些文本:在热敏打印机上打印Java,java,printing,Java,Printing,我使用以下代码在80毫米卷筒纸热敏打印机上打印一些文本: public class printnow { public static void printCard(final String bill) { final PrinterJob job = PrinterJob.getPrinterJob(); Printable contentToPrint = new Printable() { @Override
public class printnow {
public static void printCard(final String bill) {
final PrinterJob job = PrinterJob.getPrinterJob();
Printable contentToPrint = new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setFont(new Font("Monospaced", Font.BOLD, 7));
pageFormat.setOrientation(PageFormat.PORTRAIT);
Paper pPaper = pageFormat.getPaper();
pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
pageFormat.setPaper(pPaper);
if (pageIndex > 0)
return NO_SUCH_PAGE; //Only one page
String Bill [] = bill.split(";");
int y = 0;
for (int i = 0; i < Bill.length; i++) {
g2d.drawString(Bill[i], 0, y);
y = y + 15;
}
return PAGE_EXISTS;
}
};
boolean don = job.printDialog();
job.setPrintable(contentToPrint);
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}
}
}
你知道为什么会发生这种情况以及如何解决吗?经过大量的研究和运用一点大脑,我找到了解决办法。这是一个很小但很愚蠢的错误。请阅读以下源代码:
public class printnow {
public static void printCard(final String bill ) {
final PrinterJob job = PrinterJob.getPrinterJob();
Printable contentToPrint = new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setFont(new Font("Monospaced", Font.BOLD, 7));
if (pageIndex > 0) {
return NO_SUCH_PAGE;
} //Only one page
String Bill [] = bill.split(";");
int y = 0;
for (int i = 0; i < Bill.length; i++) {
g2d.drawString(Bill[i], 0, y);
y = y + 15;
}
return PAGE_EXISTS;
}
};
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.PORTRAIT);
Paper pPaper = pageFormat.getPaper();
pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
pageFormat.setPaper(pPaper);
job.setPrintable(contentToPrint, pageFormat);
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}
}
}
请注意,第二个参数被传递给上述方法。
希望这对每个人都有帮助。:) 我遇到了完全相同的问题,但没有得到答案。这是光栅rnp打印机吗?不是。这是Epson TM-T81。尽管如此,请尝试使用我的代码,我有一个正常打印机在没有打印对话框的情况下工作,但在我的打印机上不工作,因为它是ptr打印机。希望它能起作用。
public class printnow {
public static void printCard(final String bill ) {
final PrinterJob job = PrinterJob.getPrinterJob();
Printable contentToPrint = new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setFont(new Font("Monospaced", Font.BOLD, 7));
if (pageIndex > 0) {
return NO_SUCH_PAGE;
} //Only one page
String Bill [] = bill.split(";");
int y = 0;
for (int i = 0; i < Bill.length; i++) {
g2d.drawString(Bill[i], 0, y);
y = y + 15;
}
return PAGE_EXISTS;
}
};
PageFormat pageFormat = new PageFormat();
pageFormat.setOrientation(PageFormat.PORTRAIT);
Paper pPaper = pageFormat.getPaper();
pPaper.setImageableArea(0, 0, pPaper.getWidth() , pPaper.getHeight() -2);
pageFormat.setPaper(pPaper);
job.setPrintable(contentToPrint, pageFormat);
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}
}
}
job.setPrintable(contentToPrint, pageFormat);