views:

142

answers:

1

I am using PersonaC30 card printer to print my Jpanel data on card. My print method is working fine with paper printer but when i print with card printer its not printing my data. Although its printing well externally but its not working with my application please someone help me out if any of you have work with card printing i am trying since 2 days but no result. here is my code.

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);

      }
}

Thanks

+3  A: 

Try this. I added some code to resize the image of the panel.

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);

 }
}
Emre
Thank you so much its absolutely work fine. Thank for your help Emre.
Talha Bin Shakir