
首蠢信先 来看看效果 如下图
带激轮
代码如下
PrintTest java
import java awt BasicStroke
铅穗import java awt Color
import java awt Font
import java awt Graphics
import java awt Graphics D
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 snjp model RentDetail
import 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 =
public PrintTest()
{
}
public PrintTest printTest
public PrintTest(RentRecordParameter prentRecord RentDetail prentDetail)
{
this roomID = prentRecord getRoomId()+ 房间
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指明打印的图形环境
* @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(this rentType)
{
case :
feeItem = 房租 押金
rentTotal=this rent
break
case :
feeItem = 房租 押金 水费 电费
rentTotal=this rent+this electricityFee+this waterFee
break
default:
feeItem = 未知收费项目
}
//转换成Graphics D
Graphics D graphics D = (Graphics D) graphics
//设置打印颜色为黑色
graphics D setColor(Color black)
//打印起点坐标x=
double x = pf getImageableX()
double y = pf getImageableY()
System out println( 打印起点坐标值是{ +x+ +y+ } )
switch (pageIndex)
{
case :
//设置打印字体(字体名称 样式和点大小)(字体名称可以是物理或者逻辑名称)
//Java平台所定义的五种字体系列 Serif SansSerif Monospaced Dialog 和 DialogInput
Font font = new Font( 新宋体 Font TRUETYPE_FONT )
graphics D setFont(font)//设置字体
BasicStroke basicStroke=new BasicStroke( f)
float[] dash = { f }
//设置打印线的属性
// 线宽空白的宽度虚线的宽度偏移量
// graphics D setStroke(new BasicStroke( f
// BasicStroke CAP_BUTT BasicStroke JOIN_MITER
// f dash f))
//graphics D setStroke(new BasicStroke( f))
graphics D setStroke(basicStroke)//设置线宽
float height = font getSize D()//字体高度
System out println( 字体高度是 +height)
//绘制收据标题
graphics D drawString(receiptTitle (float) y + * height)
//绘制收据编号
String receiptNo = String valueOf(RECEIPT_NO)
graphics D drawString( No : +GlobalMethod get String(receiptNo ) (float)x* (float) y + * height)
//绘制打印时间
SimpleDateFormat myDateFormat = new SimpleDateFormat( yyyy年MM月dd日 )
graphics D drawString(myDateFormat format(printDate) (float)x* (float) y + * height)
//绘制收据标题下面的第一条横线
graphics D drawLine(
(int) (y + * height+ )
(int) (y + * height+ )
)
//绘制收据标题下面的第二条横线
graphics D drawLine(
(int) (y + * height+ )
(int) (y + * height+ )
)
//正文 ——今收到
graphics D drawString(content (float) x* (float) y + * height)
//收费项目名称
graphics D drawString(this roomID+feeItem (float) x* (float) y + * height)
//绘制正文 下划线
graphics D drawLine(
(int)x*
(int) (y + * height+ )
(int)x+
(int) (y + * height+ )
)
//人民币(大写)
graphics D drawString(content +Money NumToRMBStr(rentTotal) (float) x* (float) y + * height)
//绘制人民币(大写)下划线
graphics D drawLine(
(int)x*
(int) (y + * height+ )
(int)x+
(int) (y + * height+ )
)
//(小写)
graphics D drawString( (小写) +rentTotal+ 元 (float) x* (float) y + * height)
//绘制(小写)下划线
graphics D drawLine(
(int)x*
(int) (y + * height+ )
(int)x+
(int) (y + * height+ )
)
//附注
graphics D drawString( 附注: +this remark (float) x* (float) y + * height)
//绘制附注下划线
graphics D drawLine(
(int)x* +
(int) (y + * height+ )
(int)x+
(int) (y + * height+ )
)
// 签字(收款人)
graphics D drawString( 签字(收款人) (float) x* + (float) y + * height)
//签字(收款人)下划线
graphics D drawLine(
(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()
pf setOrientation(PageFormat PORTRAIT)
//通过Paper设置页面的空白边距和可打印区域 必须与实际打印纸张大小相符
Paper p = new Paper()
p setSize( )//纸张大小
p setImageableArea( )//A ( X )设置打印区域 其实 应该是 因为A 纸的默认X Y边距是
pf setPaper(p)
//把 PageFormat 和 Printable 添加到书中 组成一个页面
book append(printTest pf)
//获取打印服务对象
PrinterJob job = PrinterJob getPrinterJob()
//设置打印类
job setPageable(book)
try
{
//可以用printDialog显示打印对话框 在用户确认后打印 也可以直接打印
// boolean isShowing=job printDialog()
// if(isShowing)
// {
// System out println(this printDate+ ************* )
RECEIPT_NO=RECEIPT_NO+
job print()
// }
}
catch (PrinterException e)
{
e printStackTrace()
}
}
public static void main(String[] args)
{
// 通俗理解就是书 文档
Book book = new Book()
// 设置成竖打
PageFormat pf = new PageFormat()
pf setOrientation(PageFormat PORTRAIT)
// 通过Paper设置页面的空白边距和可打印区域 必须与实际打印纸张大小相符
Paper p = new Paper()
p setSize( )//纸张大小
p setImageableArea( )//A ( X )设置打印区域 其实 应该是 因为A 纸的默认X Y边距是
pf setPaper(p)
// 把 PageFormat 和 Printable 添加到书中 组成一个页面
book append(new PrintTest() pf)
//获取打印服务对象
PrinterJob job = PrinterJob getPrinterJob()
// 设置打印类
job setPageable(book)
try
{
//可以用printDialog显示打印对话框 在用户确认后打印 也可以直接打印
//boolean a=job printDialog()
//if(a)
//{
job print()
//}
}
catch (PrinterException e)
{
e printStackTrace()
}
}
lishixinzhi/Article/program/Java/hx/201311/25778public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
System.out.println("pageIndex=" + pageIndex)
Component c = null
//print string
String str = "
Hello Word!
"
//转换成Graphics2D
Graphics2D g2 = (Graphics2D) gra
//设置打印颜色为黑色
g2.setColor(Color.black)
//打印起点坐标
double x = pf.getImageableX()
double y = pf.getImageableY()
switch (pageIndex) {
case 0:
//设置打印字体(字体名称、样式和点大小)毁禅(字体名称可以是物理或者逻辑名称)
//Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput
Font font = new Font("新宋体"纤配尘, Font.PLAIN, 9)
g2.setFont(font)//设置字体
//BasicStroke bs_3=new BasicStroke(0.5f)
float[] dash1 = {2.0f}
//设置打印线的属性卖并。
//1.线宽 2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量
g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f))
//g2.setStroke(bs_3)//设置线宽
float heigth = font.getSize2D()//字体高度
System.out.println("x=" + x)
// -1- 用Graphics2D直接输出
//首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
//g2.drawLine(10,10,200,300)
Image src = Toolkit.getDefaultToolkit().getImage("F:\\workspace\\QQ.png")
g2.drawImage(src, (int) x, (int) y, c)
int img_Height = src.getHeight(c)
int img_width = src.getWidth(c)
//System.out.println("img_Height="+img_Height+"img_width="+img_width)
g2.drawString(str, (float) x, (float) y + 1 * heigth + img_Height)
g2.drawLine((int) x, (int) (y + 1 * heigth + img_Height + 10), (int) x + 200, (int) (y + 1 * heigth + img_Height + 10))
g2.drawImage(src, (int) x, (int) (y + 1 * heigth + img_Height + 11), c)
return PAGE_EXISTS
default:
return NO_SUCH_PAGE
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)