我完成了这个编程,它对我来说很好。你可能会从中得到一些帮助。检查一下。
public class Receipt
{
PageFormat format = new PageFormat();
Paper paper = new Paper();
double paperWidth = 3;//3.25
double paperHeight = 500.69;//11.69
double leftMargin = 0.12;
double rightMargin = 0.10;
double topMargin = 0;
double bottomMargin = 0.01;
paper.setSize(paperWidth * 200, paperHeight * 200);
paper.setImageableArea(leftMargin * 200, topMargin * 200,
(paperWidth - leftMargin - rightMargin) * 200,
(paperHeight - topMargin - bottomMargin) * 200);
format.setPaper(paper);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
//testing
PrinterJob printerJob = PrinterJob.getPrinterJob();
Printable printable = new ReceiptPrint();
format = printerJob.validatePage(format);
printerJob.setPrintable(printable, format);
try {
printerJob.print(aset);
}
catch (Exception e) {
e.printStackTrace();
}
class ReceiptPrint implements Printable {
SimpleDateFormat df = new SimpleDateFormat();
String receiptDetailLine ;
public static final String pspace=" ";//15-spaces
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
df.applyPattern("dd/MM/yyyy HH:mm:ss");
String strText =null;
final String LF = "\n";// text string to output
int lineStart; // start index of line in textarea
int lineEnd; // end index of line in textarea
int lineNumber;
int lineCount;
final String SPACE = " ";//10 spaces
final String SPACES = " ";//9
final String uline ="________________________________________";
final String dline ="----------------------------------------";
String greetings ="THANKS FOR YOUR VISIT";
receiptDetailLine= Integer.toString(SystemConfig.currentLocationCode) + "-"
+ Integer.toString(SystemConfig.tillNumber) + "-" +
Integer.toString(EPOSFrame.mainFrame.currentUser.data.userID) + "-" +
Receipt.order.name;
Graphics2D g2d = (Graphics2D) graphics;
Font font = new Font("MONOSPACED",Font.BOLD, 9);
if (pageIndex < 0 || pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
JTextArea textarea = new JTextArea(10,40);
//testing
if(SystemConfig.receiptAddressLine1!="")
{
textarea.append(SPACES+SystemConfig.receiptAddressLine1 +"\n");
}
if(SystemConfig.receiptAddressLine2!="")
{
textarea.append(" "+SPACES+SystemConfig.receiptAddressLine2 +"\n");
}
if(SystemConfig.receiptAddressLine3!="")
{
textarea.append(SPACES+SystemConfig.receiptAddressLine3 +"\n");
}
if(SystemConfig.receiptAddressLine5!="")
{
textarea.append(""+SPACES+SystemConfig.receiptAddressLine5 +"\n");
}
if(SystemConfig.receiptAddressLine4!="")
{
textarea.append(SPACES+SystemConfig.receiptAddressLine4 +"\n");
}
textarea.append(uline+"\n");
textarea.append("Order Ref:" +" "+receiptDetailLine+"\n");
textarea.append(dline+"\n");
textarea.append(" Qty Description"+SPACES+" Price"+LF);
textarea.append(dline+"\n");
System.out.println(2);
TransactionItemPayment payment = null;
Enumeration transEnumeration = Receipt.order.m_transactions.elements();
int qty= 0;
String desc= "";
while (transEnumeration.hasMoreElements())
{
TransactionItem ti = (TransactionItem) transEnumeration.nextElement();
StringBuffer price = new StringBuffer(String.valueOf(ti.getValue()));
String printLine = ti.getReceiptLine();
if (printLine!=null)
{
qty=ti.getQuantity();
desc = ti.getTopDisplayLine();
if (price.length()>=2)
{
price.insert(price.length() - 2, '.');
}
textarea.append( printLine+LF );
qty=0;
desc="";
}
else if (ti instanceof TransactionItemPayment)
{
payment = (TransactionItemPayment) ti;
}
}
long serviceCharge = Receipt.order.calcServiceCharge();
if (serviceCharge>0)
{
double charge = (double) serviceCharge / 100;
String valueString =
SystemConfig.decimalFormat.format(charge).replace((char)163,' ');
String printedLine = " Service Charge " +SPACE+ valueString;
textarea.append( printedLine + LF);
}
else if (SystemConfig.serviceCharge)
{
String printedLine = " Service Charge Complimentary";
textarea.append( printedLine + LF);
}
long autoDiscount = Receipt.order.calcAutoDiscount();
if (autoDiscount>0)
{
double discount = (double) autoDiscount / 100;
String valueString =
SystemConfig.decimalFormat.format(discount).replace((char)163,' ');
String printedLine = " Discount:"+pspace+valueString;
textarea.append( printedLine + LF);
}
long total = (long)Receipt.order.getOrderTotal() +
Receipt.order.calcServiceCharge() - Receipt.order.calcAutoDiscount();
String formattedTotal = SystemConfig.decimalFormat.format((double)total /
100D);
textarea.append(LF+" SubTotal:"+pspace+formattedTotal+LF);
textarea.append(" Gratuity:______________________"+LF);//12 space
textarea.append(" Total:_______________________"+LF);
textarea.append(" Signature:_____________________"+LF);
textarea.append(LF+SPACES+" Your Reciept\n"+ SPACE+greetings+LF);
textarea.append(df.format(new Date()) + LF);
textarea.setEditable(false);
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setFont(font);
lineNumber = 0;
lineCount = textarea.getLineCount();
strText = textarea.getText();
/*MediaTracker mt = new MediaTracker(textarea);
URL imageURL = null;
try {
imageURL = new URL(mainDirectory+"quindell.png");
} catch (MalformedURLException me) {
me.printStackTrace();
}
//--- Load the image and wait for it to load
Image image = Toolkit.getDefaultToolkit().getImage(imageURL);
mt.addImage(image, 0);
*/
while (lineCount!=0){
try {
lineStart = textarea.getLineStartOffset(lineNumber);
lineEnd = textarea.getLineEndOffset(lineNumber);
strText = textarea.getText(lineStart, lineEnd-lineStart);
} catch( Exception exception)
{
System.out.println("Printing error:" + exception); // have to catch BadLocationException
}
g2d.drawString(strText,1,(lineNumber + 1) *18);
//spacing between lines
lineNumber = lineNumber + 1;
lineCount--;
}
return Printable.PAGE_EXISTS;
}
}