我正在使用PersonaC30卡片打印机在卡片上打印我的Jpanel数据。我的打印方法适用于纸质打印机,但是当我用卡片打印机打印时,它不打印我的数据。它外部打印很好,但它不适用于我的应用程序。
这是我的代码:
private void printCard(){
PrinterJob printjob = PrinterJob.getPrinterJob();
printjob.setJobName(" TESSCO CUSTOMER CARD ");
printjob.setPrintable (new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum){
Paper card = pf.getPaper();
// card.setImageableArea(0, 0, 153, 243);
card.setSize(243, 154);
pf.setPaper(card);
pf.setOrientation(PageFormat.LANDSCAPE);
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.translate( 0f, 0f );
jLayeredPane2.paint(g2);
return Printable.PAGE_EXISTS;
}
});
if (printjob.printDialog() == false)
return;
try {
printjob.print();
}
catch (PrinterException ex) {
System.out.println("NO PAGE FOUND."+ex);
}
}
答案 0 :(得分:5)
试试这个。我添加了一些代码来调整面板图像的大小。
private void printCard() {
PrinterJob printjob = PrinterJob.getPrinterJob();
printjob.setJobName(" TESSCO CUSTOMER CARD ");
Printable printable = new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum) {
if (pageNum > 0) {
return Printable.NO_SUCH_PAGE;
}
Dimension size = jLayeredPane2.getSize();
BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
jLayeredPane2.print(bufferedImage.getGraphics());
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);
return Printable.PAGE_EXISTS;
}
};
Paper paper = new Paper();
paper.setImageableArea(0, 0, 153, 243);
paper.setSize(243, 154);
PageFormat format = new PageFormat();
format.setPaper(paper);
format.setOrientation(PageFormat.LANDSCAPE);
printjob.setPrintable(printable, format);
if (printjob.printDialog() == false)
return;
try {
printjob.print();
} catch (PrinterException ex) {
System.out.println("NO PAGE FOUND." + ex);
}
}