首先来看看效果如下图
代码如下
PrintTestjava
import javaawtBasicStroke;
import javaawtColor;
import javaawtFont;
import javaawtGraphics;
import javaawtGraphicsD;
import javaawtprintBook;
import javaawtprintPageFormat;
import javaawtprintPaper;
import javaawtprintPrintable;
import javaawtprintPrinterException;
import javaawtprintPrinterJob;
import javatextSimpleDateFormat;
import javautilDate;
import snjpmodelRentDetail;
import snjpparameterRentRecordParameter;
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 =;
public PrintTest()
{
}
public PrintTest printTest;
public PrintTest(RentRecordParameter prentRecordRentDetail prentDetail)
{
thisroomID = prentRecordgetRoomId()+房间;
thisprintDate = prentRecordgetRegisterDate();
thisrentType = prentRecordgetRentType();
thiswaterFee = prentDetailgetWaterBill();
thiselectricityFee = prentDetailgetElectricityBill();
thisrent = prentDetailgetRent();
thisremark = prentDetailgetRemark();
}
/** */
/**
* @param Graphic指明打印的图形环境
* @param PageFormat指明打印页格式(页面大小以点为计量单位点为英才的/英寸为毫米A纸大致为×点)
* @param pageIndex指明页号
**/
public int print(Graphics graphics PageFormat pf int pageIndex)
throws PrinterException
{
//print string
String receiptTitle = 收款收据;
String content=今收到;
String feeItem = null;
Float rentTotal = f;
String content=总计人民币(大写);
//收款的内容(根据租赁类型只有房租房租水费电费两种值)
switch(thisrentType)
{
case :
feeItem = 房租押金;
rentTotal=thisrent;
break;
case :
feeItem = 房租押金水费电费;
rentTotal=thisrent+thiselectricityFee+thiswaterFee;
break;
default:
feeItem = 未知收费项目;
}
//转换成GraphicsD
GraphicsD graphicsD = (GraphicsD) graphics;
//设置打印颜色为黑色
graphicsDsetColor(Colorblack);
//打印起点坐标x=
double x = pfgetImageableX();
double y = pfgetImageableY();
Systemoutprintln(打印起点坐标值是{+x++y+});
switch (pageIndex)
{
case :
//设置打印字体(字体名称样式和点大小)(字体名称可以是物理或者逻辑名称)
//Java平台所定义的五种字体系列SerifSansSerifMonospacedDialog 和 DialogInput
Font font = new Font(新宋体 FontTRUETYPE_FONT );
graphicsDsetFont(font);//设置字体
BasicStroke basicStroke=new BasicStroke(f);
float[] dash = { f };
//设置打印线的属性
//线宽; ;;空白的宽度;虚线的宽度;偏移量
// graphicsDsetStroke(new BasicStroke(f
// BasicStrokeCAP_BUTT BasicStrokeJOIN_MITER
// f dash f));
//graphicsDsetStroke(new BasicStroke(f));
graphicsDsetStroke(basicStroke);//设置线宽
float height = fontgetSizeD();//字体高度
Systemoutprintln(字体高度是+height);
//绘制收据标题
graphicsDdrawString(receiptTitle (float) y + * height);
//绘制收据编号
String receiptNo = StringvalueOf(RECEIPT_NO);
graphicsDdrawString(No:+GlobalMethodgetString(receiptNo) (float)x* (float) y + * height);
//绘制打印时间
SimpleDateFormat myDateFormat = new SimpleDateFormat(yyyy年MM月dd日);
graphicsDdrawString(myDateFormatformat(printDate) (float)x* (float) y + * height);
//绘制收据标题下面的第一条横线
graphicsDdrawLine(
(int) (y + * height+)
(int) (y + * height+)
);
//绘制收据标题下面的第二条横线
graphicsDdrawLine(
(int) (y + * height+)
(int) (y + * height+)
);
//正文——今收到
graphicsDdrawString(content(float) x* (float) y + * height);
//收费项目名称
graphicsDdrawString(thisroomID+feeItem(float) x* (float) y + * height);
//绘制正文下划线
graphicsDdrawLine(
(int)x*
(int) (y + * height+)
(int)x+
(int) (y + * height+)
);
//人民币(大写)
graphicsDdrawString(content+MoneyNumToRMBStr(rentTotal)(float) x* (float) y + * height);
//绘制人民币(大写)下划线
graphicsDdrawLine(
(int)x*
(int) (y + * height+)
(int)x+
(int) (y + * height+)
);
//(小写)
graphicsDdrawString((小写)+rentTotal+元(float) x* (float) y + * height);
//绘制(小写)下划线
graphicsDdrawLine(
(int)x*
(int) (y + * height+)
(int)x+
(int) (y + * height+)
);
//附注
graphicsDdrawString(附注:+thisremark(float) x* (float) y + * height);
//绘制附注下划线
graphicsDdrawLine(
(int)x*+
(int) (y + * height+)
(int)x+
(int) (y + * height+)
);
// 签字(收款人)
graphicsDdrawString(签字(收款人)(float) x*+ (float) y + * height);
//签字(收款人)下划线
graphicsDdrawLine(
(int)x*
(int) (y + * height+)
(int)x+
(int) (y + * height+)
);
return PAGE_EXISTS;
default:
RECEIPT_NO=RECEIPT_NO;
return NO_SUCH_PAGE;
}
}
//方法调用
public void printReceipt(PrintTest printTest){
//通俗理解就是书文档
Book book = new Book();
//设置成竖打
PageFormat pf = new PageFormat();
pfsetOrientation(PageFormatPORTRAIT);
//通过Paper设置页面的空白边距和可打印区域必须与实际打印纸张大小相符
Paper p = new Paper();
psetSize( );//纸张大小
psetImageableArea( );//A( X )设置打印区域其实应该是因为A纸的默认XY边距是
pfsetPaper(p);
//把 PageFormat 和 Printable 添加到书中组成一个页面
bookappend(printTest pf);
//获取打印服务对象
PrinterJob job = PrinterJobgetPrinterJob();
//设置打印类
jobsetPageable(book);
try
{
//可以用printDialog显示打印对话框在用户确认后打印也可以直接打印
// boolean isShowing=jobprintDialog();
// if(isShowing)
// {
// Systemoutprintln(thisprintDate+*************);
RECEIPT_NO=RECEIPT_NO+;
jobprint();
// }
}
catch (PrinterException e)
{
eprintStackTrace();
}
}
public static void main(String[] args)
{
// 通俗理解就是书文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pfsetOrientation(PageFormatPORTRAIT);
// 通过Paper设置页面的空白边距和可打印区域必须与实际打印纸张大小相符
Paper p = new Paper();
psetSize( );//纸张大小
psetImageableArea( );//A( X )设置打印区域其实应该是因为A纸的默认XY边距是
pfsetPaper(p);
// 把 PageFormat 和 Printable 添加到书中组成一个页面
bookappend(new PrintTest() pf);
//获取打印服务对象
PrinterJob job = PrinterJobgetPrinterJob();
// 设置打印类
jobsetPageable(book);
try
{
//可以用printDialog显示打印对话框在用户确认后打印也可以直接打印
//boolean a=jobprintDialog();
//if(a)
//{
jobprint();
//}
}
catch (PrinterException e)
{
eprintStackTrace();
}
}
}