问题描述
我正在使用textArea作为节点的80毫米热敏打印机上打印一串文本。但是在打印完之后,文本的高度被截断了。下面是我得到的输出图像:
TextArea textArea = new TextArea();
textArea.appendText("*********************************************" + "\n");
textArea.appendText(" " + companyName + " \n");
textArea.appendText("*********************************************" + "\n");
textArea.appendText("Location: " + companyLocation + "\n");
textArea.appendText("Contact Us: " + phoneNumber + "\n");
textArea.appendText("TIN: " + tinNumber + "\n");
textArea.appendText("Receipt No: " + transactionGeneratedId + "\n");
textArea.appendText("Date: " + getCurrentTimeStamp() + "\n");
textArea.appendText("---------------------------------------------" + "\n");
这是用于创建文本字符串并将其附加到textArea的方法:
//*** issue print command
public void printReceipt(){
// print(getPrintableText());
printNode(getPrintableText());
}
//*** prepare text for printing
public Node getPrintableText(){
TextArea textArea = new TextArea();
textArea.appendText("*********************************************" + "\n");
textArea.appendText(" " + companyName + " \n");
textArea.appendText("*********************************************" + "\n");
textArea.appendText("Location: " + companyLocation + "\n");
textArea.appendText("Contact Us: " + phoneNumber + "\n");
textArea.appendText("TIN: " + tinNumber + "\n");
textArea.appendText("Receipt No: " + transactionGeneratedId + "\n");
textArea.appendText("Date: " + getCurrentTimeStamp() + "\n");
textArea.appendText("---------------------------------------------" + "\n");
// get each product and add to printing list
for(Products product: productList){
if(product.getProduct_name().length() > 10){
// getting the first 11 characters of the product name
String name = product.getProduct_name().substring(0,9)+ "...";
textArea.appendText(product.getQuantity_purchased() + " x " + name + " " + currencySymbol + product.getUnit_selling_price() + "\n");
}else{
textArea.appendText(product.getQuantity_purchased() + " x " + product.getProduct_name()
+ " " + currencySymbol + product.getUnit_selling_price() + "\n");
}
textArea.appendText("Subtotal " + currencySymbol + (Double.parseDouble(product.getQuantity_purchased()) *
Double.parseDouble(product.getUnit_selling_price())) + "\n");
textArea.appendText("---------------------------------------------" + "\n");
}
// get overall total
textArea.appendText("Overall Total " + currencySymbol + totalPrice + "\n");
String cashReceived = fieldCashReceived.getText().trim();
// get total amount
textArea.appendText("Amount Paid " + currencySymbol + cashReceived + "\n");
double balance = totalPrice - Double.parseDouble(cashReceived);
textArea.appendText("_____________________________________________" + "\n");
// get balance
textArea.appendText("Balance " + currencySymbol + balance + "\n");
textArea.appendText("*********************************************" + "\n");
textArea.appendText("Thank you so much!" + "\n");
textArea.appendText("*********************************************");
return textArea;
}
这是打印方法:
public void printNode(Node node) {
Printer printer = Printer.getDefaultPrinter();
PrinterJob printerJob = PrinterJob.createPrinterJob(printer);
Paper paper = PrintHelper.createPaper("Roll80",80,590,Units.MM);
PageLayout pageLayout = printerJob.getPrinter().createPageLayout(paper,PageOrientation.PORTRAIT,0);
double height = node.getLayoutBounds().getHeight();
System.out.println("Height: " + height);
double scale = 0.791;
node.getTransforms().add(new Scale(scale,scale));
if (printerJob != null) {
boolean success = printerJob.printPage(pageLayout,node);
if (success) {
printerJob.endJob();
System.exit(0);
}
}
}
我在做什么不对?
解决方法
我终于用类似于@VGR的解决方案解决了这个问题。我希望这可以对可能遇到相同问题的人有所帮助。我只是使用StringBuilder类构造字符串并将其设置为标签,然后将标签作为节点返回。
在这里检查:
foo(c("A","B"),c(1,2))
# Error in integer(n) : invalid 'length' argument
foo(data$string,data$number)
# Error in integer(n) : invalid 'length' argument