Thursday, September 2, 2010

How to print a receipt with the Java code generation

the code is as follows: PrintTest.java import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Grap ...
Code:
PrintTest.java
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;


import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.util.Date;
import jp.co.snjp.model.RentDetail;
import jp.co.snjp.parameter.RentRecordParameter;
public class PrintTest implements Printable
(
private String roomID;
private Date printDate;
private Integer rentType;
private Float waterFee;
private Float electricityFee;
private Float rent;
private String remark;
private static int RECEIPT_NO = 0;
public PrintTest ()
(
)
public PrintTest printTest;
public PrintTest (RentRecordParameter prentRecord, RentDetail prentDetail)
(
this.roomID = prentRecord.getRoomId () + "rooms";
this.printDate = prentRecord.getRegisterDate ();
this.rentType = prentRecord.getRentType ();
this.waterFee = prentDetail.getWaterBill ();
this.electricityFee = prentDetail.getElectricityBill ();
this.rent = prentDetail.getRent ();
this.remark = prentDetail.getRemark ();
)
/ ** * /
/ **
* @ Param Graphic specified print graphics environment
* @ Param PageFormat specified print page format (page size for units of measurement points, 1 point is 1 of excellence in the 1 / 72, 1 inch is 25.4 mm. A4 paper is approximately 595 × 842 points)
* @ Param pageIndex the specified page number
** /
public int print (Graphics graphics, PageFormat pf, int pageIndex)
throws PrinterException
(
/ / Print string
String receiptTitle = "Collection Receipt";
String content1 = "today received";
String feeItem = null;
Float rentTotal = 0f;
String content2 = "Total RMB (capital)";
/ / Collection of content (based on lease type, only "rent", "Rent, water, electricity," two kinds of value). Switch (this.rentType)
(
case 1:
feeItem = "rent deposit";
rentTotal = this.rent;
break;
case 2:
feeItem = "rent, deposit, water and electricity";
rentTotal = this.rent + this.electricityFee + this.waterFee;
break;
default:
feeItem = "unknown charges";
)
/ / Convert into a Graphics2D
Graphics2D graphics2D = (Graphics2D) graphics;
/ / Set print color as black
graphics2D.setColor (Color.black);
/ / Print the beginning coordinates x = 10
double x = pf.getImageableX ();
double y = pf.getImageableY ();
System.out.println ("Print the starting point coordinate value is (" + x +","+ y +"}。");
switch (pageIndex)
(
case 0:
/ / Set the print font (font name, style and point size) (font name can be physical or logical name)
/ / Java platform as defined by the five font family: Serif, SansSerif, Monospaced, Dialog, and DialogInput

New Times New Roman ", Font.TRUETYPE_FONT, 9);

graphics2D.setFont (font); / / set the font
BasicStroke basicStroke = new BasicStroke (0.5f);
float [] dash1 = (2.0f);
/ / Set the properties of print lines.
/ / 1. Linewidth; 2.; 3.; 4 blank width; 5. Dashed line width; 6. Offsets
/ / Graphics2D.setStroke (new BasicStroke (0.5f,
/ / BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
/ / 2.0f, dash1, 0.0f));
/ / Graphics2D.setStroke (new BasicStroke (0.5f));
graphics2D.setStroke (basicStroke); / / set width
float height = font.getSize2D ();// font height
System.out.println ("Font height is" + height);
/ / Draw the title receipt
graphics2D.drawString (receiptTitle, 120, (float) y + 1 * height);
/ / Draw the receipt number
String receiptNo = String.valueOf (RECEIPT_NO);
graphics2D.drawString ("No.:" + GlobalMethod.get0String (receiptNo, 7), (float) x * 21, (float) y + 1 * height);
/ / Draw printing time
SimpleDateFormat myDateFormat = new SimpleDateFormat ("yyyy month dd day in MM");
graphics2D.drawString (myDateFormat.format (printDate), (float) x * 21, (float) y + 2 * height);
/ / Draw the title following the receipt of the first horizontal line
graphics2D.drawLine (
115,
(Int) (y + 1 * height +5),
160,
(Int) (y + 1 * height +5)
);
/ / Draw the title following the receipt of the second horizontal line
graphics2D.drawLine (
115,
(Int) (y + 1 * height +7),
160,
(Int) (y + 1 * height +7)
);
/ / Body 1 - today received
graphics2D.drawString (content1, (float) x * 2, (float) y + 4 * height);
/ / Name of fees
graphics2D.drawString (this.roomID + feeItem, (float) x * 5, (float) y + 4 * height);
/ / Draw the body of an underscore
graphics2D.drawLine (
(Int) x * 5,
(Int) (y + 4 * height +4),
(Int) x +260,
(Int) (y + 4 * height +4)
);
/ / RMB (capital)
graphics2D.drawString (content2 + Money.NumToRMBStr (rentTotal), (float) x * 2, (float) y + 6 * height);
/ / Draw yuan (upper case) underscores
graphics2D.drawLine (
(Int) x * 12,
(Int) (y + 6 * height +4),
(Int) x +200,
(Int) (y + 6 * height +4)
);
/ / (Lower case)
graphics2D.drawString ("(lowercase)" + rentTotal + "Element", (float) x * 20, (float) y + 6 * height);
/ / Draw (lower case) underlined
graphics2D.drawLine (
(Int) x * 23,
(Int) (y + 6 * height +4),
(Int) x +260,
(Int) (y + 6 * height +4)
);
/ / Note
graphics2D.drawString ("Note:" + this.remark, (float) x * 2, (float) y + 8 * height);
/ / Draw notes underscore
graphics2D.drawLine (


nt) x * 4 +4,

(Int) (y + 8 * height +4),
(Int) x +260,
(Int) (y + 8 * height +4)
); / / Sign (the recipient)graphics2D.drawString ("signature (the recipient)", (float) x * 17 +3, (float) y + 11 * height);
/ / Sign (the recipient) underscore
graphics2D.drawLine (
(Int) x * 23,
(Int) (y + 11 * height +2),
(Int) x +260,
(Int) (y + 11 * height +2)
);
return PAGE_EXISTS;
default:
RECEIPT_NO = RECEIPT_NO-1;
return NO_SUCH_PAGE;
)
)
/ / Method calls
public void printReceipt (PrintTest printTest) (
/ / Popular understanding is that books, documents,
Book book = new Book ();
/ / Set vertical play
PageFormat pf = new PageFormat ();
pf.setOrientation (PageFormat.PORTRAIT);
/ / Paper Set by the blank page and the printable area margins. Must be consistent with the actual print paper size.
Paper p = new Paper ();
p.setSize (280, 120); / / paper size
p.setImageableArea (10, 10, 280, 120); / / A4 (595 X 842) set the print area, in fact, should be 72,72 0,0, because the default A4 paper, X, Y margin is 72
pf.setPaper (p);
/ / To PageFormat and Printable to the book, composed of a page
book.append (printTest, pf);
/ / Get the print service object
PrinterJob job = PrinterJob.getPrinterJob ();
/ / Set the print category
job.setPageable (book);
try
(
/ / Display print dialog box can be printDialog, the user confirmation print; also can print directly
/ / Boolean isShowing = job.printDialog ();
/ / If (isShowing)
/ / (
/ / System.out.println (this.printDate +"*************");
RECEIPT_NO = RECEIPT_NO +1;
job.print ();
/ /)
)
catch (PrinterException e)
(
e.printStackTrace ();
)
)
public static void main (String [] args)
(
/ / Popular understanding is that books, documents,
Book book = new Book ();
/ / Set vertical play
PageFormat pf = new PageFormat ();
pf.setOrientation (PageFormat.PORTRAIT);
/ / Paper Set by the blank page and the printable area margins. Must be consistent with the actual print paper size.
Paper p = new Paper ();
p.setSize (280, 120); / / paper size
p.setImageableArea (10, 10, 280, 120); / / A4 (595 X 842) set the print area, in fact, should be 72,72 0,0, because the default A4 paper, X, Y margin is 72
pf.setPaper (p);
/ / To PageFormat and Printable to the book, composed of a page
book.append (new PrintTest (), pf);
/ / Get the print service object
PrinterJob job = PrinterJob.getPrinterJob ();
/ / Set the print category
job.setPageable (book);
try
(
/ / Display print dialog box can be printDialog, the user confirmation print; also can print directly
/ / Boolean a = job.printDialog ();
/ / If (a)
/ / (
job.print ();
/ /)
)
catch (PrinterException e)
(
e.printStackTrace ();
)
)
)

1 comment: