我必须使用Java通过热敏打印机打印收据.我做了一切.我的程序从数据库中获取数据,并使用特殊字符,制表符和\n转换为一个字符串.然后将字符串传递给另一个将其转换为图形的方法.
问题是当我点击打印按钮时,会出现白纸.我注意到我的String的前4-5个字符被打印在纸张末端右角的纸币的最后一行.我的打印机是Epson TM - T81.
public void printThisBill()
{
DefaultTableModel mod = (DefaultTableModel) jTable1.getModel();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
//get current date time with Date()
Date date = new Date();
Date time = new Date();
String Date = dateFormat.format(date);
String Time = timeFormat.format(time);
String Header =
" ****Super Market**** \n"
+ "Date: "+Date+" Time: "+Time+"\n"
+ "---------------------------------\n"
+ "Name Qty Rate Amt\n"
+ "---------------------------------\n";
String amt =
"\n \n \nTotal Amount = "+ amt() +"\n" …Run Code Online (Sandbox Code Playgroud) 我正在尝试向打印机发送一些文本。我只需要打印文本,在页边距处包裹并在必要时流到另一页。
这是我现在正在做的一个最小的例子:
@FXML
private void print() {
TextArea printArea = new TextArea(textArea.getText());
printArea.setWrapText(true);
printArea.getChildrenUnmodifiable().forEach(node -> node.setStyle("-fx-background-color: transparent"));
printArea.setStyle("-fx-background-color: transparent");
PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null && printerJob.showPrintDialog(textArea.getScene().getWindow())) {
if (printerJob.printPage(printArea)) {
printerJob.endJob();
// done printing
} else {
// failed to print
}
} else {
// failed to get printer job or failed to show print dialog
}
}
Run Code Online (Sandbox Code Playgroud)
最终打印的是灰色背景,它似乎是控件本身以及滚动条。我是否以错误的方式接近这个?我觉得我正在通过调整和打印控件而不是仅仅发送要打印的文本来与 API 作斗争。
下面的示例图像是从我的手机相机中拍摄的,因此白纸最终看起来有点浅灰色,但您仍然可以从控件和滚动条中看到灰色背景。