有如下一个java打印的程序,当纸张尺寸设成如下值时出现不同的效果:
int H = 287, W = 195; 打印正常
int H = 287, W = 190; 纸张最下面的线打印不出来
int H = 287, W = 160; 纸张最下面的线打印不出来
int H = 287, W = 150; 打印正常
int H = 255, W = 127; 下边和右边的线都打印不出来
用的HP激光打印机,请问这是什么原因?
import java.awt.print.*;
import java.awt.*;public class CustomPaper
{
public static void main(String[] args) throws Exception {
int H = 287, W = 180;
PrinterJob pj = PrinterJob.getPrinterJob();
double w = W * 72 / 25.4;
double h = H * 72 /25.4;
Paper p = new Paper();
p.setSize( w, h );
final double x = 15*72/25.4;
final double y = 20*72/25.4;
w = ( W - 30 ) * 72 / 25.4;
h = ( H - 40 ) * 72 / 25.4;
p.setImageableArea( x, y, w, h );
PageFormat pf = new PageFormat();
pf.setPaper(p);
final int w1 = (int)w, h1 = (int)h;
Book book = new Book();
book.append( new Printable() {
public int print(Graphics g, PageFormat pf, int pageIndex) {
g.drawRect( (int)x + 1, (int)y + 1, w1 - 2, h1 - 2 );
g.drawOval( (int)x + 5, (int)y + 5, 8, 8 );
return Printable.PAGE_EXISTS;
} }, pf );
pj.setPageable( book );
pj.print();
}
}
int H = 287, W = 195; 打印正常
int H = 287, W = 190; 纸张最下面的线打印不出来
int H = 287, W = 160; 纸张最下面的线打印不出来
int H = 287, W = 150; 打印正常
int H = 255, W = 127; 下边和右边的线都打印不出来
用的HP激光打印机,请问这是什么原因?
import java.awt.print.*;
import java.awt.*;public class CustomPaper
{
public static void main(String[] args) throws Exception {
int H = 287, W = 180;
PrinterJob pj = PrinterJob.getPrinterJob();
double w = W * 72 / 25.4;
double h = H * 72 /25.4;
Paper p = new Paper();
p.setSize( w, h );
final double x = 15*72/25.4;
final double y = 20*72/25.4;
w = ( W - 30 ) * 72 / 25.4;
h = ( H - 40 ) * 72 / 25.4;
p.setImageableArea( x, y, w, h );
PageFormat pf = new PageFormat();
pf.setPaper(p);
final int w1 = (int)w, h1 = (int)h;
Book book = new Book();
book.append( new Printable() {
public int print(Graphics g, PageFormat pf, int pageIndex) {
g.drawRect( (int)x + 1, (int)y + 1, w1 - 2, h1 - 2 );
g.drawOval( (int)x + 5, (int)y + 5, 8, 8 );
return Printable.PAGE_EXISTS;
} }, pf );
pj.setPageable( book );
pj.print();
}
}
这个我试过可以的,用的是hp 1010 A4纸张
程序我看不出什么问题。
还是不行你可以试着图形化打印(解决有的打印机可能会出现打印不完全的情况,原因我也不清楚)类似下面的代码
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws
PrinterException {
Graphics2D g2d = (Graphics2D) graphics;
BufferedImage image = new BufferedImage(595,836,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d1 = image.createGraphics();
g2d1.setBackground(Color.WHITE);
g2d1.setPaint(Color.black);
Rectangle2D page = new Rectangle2D.Double(0, 0, 595, 836);
g2d1.setPaint(Color.white);
g2d1.fill(page);
g2d1.setPaint(Color.black);
g2d1.draw(page);
//省略填充图像代码
g2d.drawImage(image, null,0, 0);
return Printable.PAGE_EXISTS;
}