加州旅馆吉他弹唱:JAVA 打印问题
来源:百度文库 编辑:中科新闻网 时间:2024/08/27 22:19:21
如何在JAVA中设置打印纸张大小、边距,
// 完税证打印类
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.util.*;
public class printWsz implements Printable
{
// 页面大小以点为计量单位,1点==0.353mm==1/72英寸,1mm==2.834点,1英寸==72点,A4纸大致为595x841点 == 210x297mm
public double inch = 72.00; // 一英寸
public double mm = 2.834; // 一毫米
public printWsz()
{
// 建立内容数组列表
printItemArray = new ArrayList();
}
public void set_wsz(ArrayList al)
{
this.printItemArray = al;
}
public boolean print_wsz()
{
/* Create a print job */
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pageFormat = pj.defaultPage();//得到默认页格式
Paper paper = pageFormat.getPaper(); // 得到页面格式的纸张
paper.setSize(190.00*mm, 127.00*mm); // 完税证大小为190x127mm
paper.setImageableArea(0.00, 0.00, 190.00*mm, 127.00*mm); // 打印区域为全部纸张
pageFormat.setPaper(paper); // 将该纸张作为格式
pj.setPrintable(this, pageFormat); // 按完税证纸张格式打印
try
{
//pj.pageDialog(pageFormat);
//if (pj.printDialog())
pj.print();
return true;
}
catch (PrinterException pe)
{
System.err.println(pe);
return false;
}
}
public int print(Graphics g, PageFormat pf, int pageIndex)
{
if (0 == pageIndex)
{
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY()); // 转换可打印区域左上角坐标 为 图形原点坐标
// example
//g2d.setColor(Color.black); // 设置打印颜色
g2d.drawRect(0, 0, (int)(5*mm), (int)(5*mm));
g2d.drawRect((int)(185*mm), 0, (int)(5*mm), (int)(5*mm));
g2d.drawRect(0, (int)(122*mm), (int)(5*mm), (int)(5*mm));
g2d.drawRect((int)(185*mm), (int)(122*mm), (int)(5*mm), (int)(5*mm));
}
return Printable.PAGE_EXISTS;
}
else
{
return Printable.NO_SUCH_PAGE;
}
}
public static void main(String arg[])
{
printWsz sp = new printWsz();
sp.print_wsz();
}
}
1 这代码不能正常打印出来,paper设置为300mm就能打出来,
2 打完后,走纸太长了,如何控制在设定的纸张高度。
// 完税证打印类
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.util.*;
public class printWsz implements Printable
{
// 页面大小以点为计量单位,1点==0.353mm==1/72英寸,1mm==2.834点,1英寸==72点,A4纸大致为595x841点 == 210x297mm
public double inch = 72.00; // 一英寸
public double mm = 2.834; // 一毫米
public printWsz()
{
// 建立内容数组列表
printItemArray = new ArrayList();
}
public void set_wsz(ArrayList al)
{
this.printItemArray = al;
}
public boolean print_wsz()
{
/* Create a print job */
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pageFormat = pj.defaultPage();//得到默认页格式
Paper paper = pageFormat.getPaper(); // 得到页面格式的纸张
paper.setSize(190.00*mm, 127.00*mm); // 完税证大小为190x127mm
paper.setImageableArea(0.00, 0.00, 190.00*mm, 127.00*mm); // 打印区域为全部纸张
pageFormat.setPaper(paper); // 将该纸张作为格式
pj.setPrintable(this, pageFormat); // 按完税证纸张格式打印
try
{
//pj.pageDialog(pageFormat);
//if (pj.printDialog())
pj.print();
return true;
}
catch (PrinterException pe)
{
System.err.println(pe);
return false;
}
}
public int print(Graphics g, PageFormat pf, int pageIndex)
{
if (0 == pageIndex)
{
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY()); // 转换可打印区域左上角坐标 为 图形原点坐标
// example
//g2d.setColor(Color.black); // 设置打印颜色
g2d.drawRect(0, 0, (int)(5*mm), (int)(5*mm));
g2d.drawRect((int)(185*mm), 0, (int)(5*mm), (int)(5*mm));
g2d.drawRect(0, (int)(122*mm), (int)(5*mm), (int)(5*mm));
g2d.drawRect((int)(185*mm), (int)(122*mm), (int)(5*mm), (int)(5*mm));
}
return Printable.PAGE_EXISTS;
}
else
{
return Printable.NO_SUCH_PAGE;
}
}
public static void main(String arg[])
{
printWsz sp = new printWsz();
sp.print_wsz();
}
}
1 这代码不能正常打印出来,paper设置为300mm就能打出来,
2 打完后,走纸太长了,如何控制在设定的纸张高度。