没做过,我手头有一个别人写的打印的源程序,给你参考一下吧
package print;/**
* 一个单页打印程序具体用法如下:
* new SinglePagePrint(this.WellCanvas).start();
*/
import java.awt.Component;
import java.awt.print.*;public class SinglePagePrint
extends Thread {
Component myComponent;
public SinglePagePrint(Component component)
{
this.myComponent = component;
} public void run()
{
PrinterJob job = PrinterJob.getPrinterJob();
Book book = new Book();
PageFormat pf = job.pageDialog(job.defaultPage());
SinglePrint pff = new SinglePrint(this.myComponent);
book.append(pff, pf, 1);
job.setPageable(book);
if (job.printDialog()) {
try {
job.print();
}
catch (PrinterException e) {
e.printStackTrace();
}
}
}
}
package print;import java.awt.*;
import java.awt.print.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class SinglePrint
implements Printable {
Component myComponent;
public SinglePrint(Component component)
{
this.myComponent = component;
} public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException
{
if (pageIndex > 1) {
return Printable.NO_SUCH_PAGE;
}
else {
Graphics2D g2 = (Graphics2D) g;
double scale = 1;
double sc1 = pf.getImageableWidth() / myComponent.getWidth();
double sc2 = pf.getImageableHeight() / myComponent.getHeight();
double sc = Math.min(sc1, sc2);
if (sc < 1) {
scale = sc;
}
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.scale(scale, scale);
this.myComponent.paint(g2);
//this.myComponent.paint(this.myComponent.getGraphics());
return Printable.PAGE_EXISTS;
}
}
}
package print;/**
* 一个单页打印程序具体用法如下:
* new SinglePagePrint(this.WellCanvas).start();
*/
import java.awt.Component;
import java.awt.print.*;public class SinglePagePrint
extends Thread {
Component myComponent;
public SinglePagePrint(Component component)
{
this.myComponent = component;
} public void run()
{
PrinterJob job = PrinterJob.getPrinterJob();
Book book = new Book();
PageFormat pf = job.pageDialog(job.defaultPage());
SinglePrint pff = new SinglePrint(this.myComponent);
book.append(pff, pf, 1);
job.setPageable(book);
if (job.printDialog()) {
try {
job.print();
}
catch (PrinterException e) {
e.printStackTrace();
}
}
}
}
package print;import java.awt.*;
import java.awt.print.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class SinglePrint
implements Printable {
Component myComponent;
public SinglePrint(Component component)
{
this.myComponent = component;
} public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException
{
if (pageIndex > 1) {
return Printable.NO_SUCH_PAGE;
}
else {
Graphics2D g2 = (Graphics2D) g;
double scale = 1;
double sc1 = pf.getImageableWidth() / myComponent.getWidth();
double sc2 = pf.getImageableHeight() / myComponent.getHeight();
double sc = Math.min(sc1, sc2);
if (sc < 1) {
scale = sc;
}
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.scale(scale, scale);
this.myComponent.paint(g2);
//this.myComponent.paint(this.myComponent.getGraphics());
return Printable.PAGE_EXISTS;
}
}
}
http://www-900.ibm.com/developerWorks/cn/java/l-javaprint/index.shtml