如何根据计费打印机尺寸

时间:2017-09-30 11:16:49

标签: java swing printing jframe

嘿我试图制作发票软件一切都很好而我打印帐单然后根据标准打印机尺寸进行打印但是我想通过计费打印机打印它我不是使用jesper报告或任何其他报告软件,我只需通过swing代码打印JFrame。

这是我的代码。 。 。

final Component comp;
public testing(Component comp){

this.comp=comp;
} 

  @Override
public int print(Graphics g, PageFormat format, int page_index) 
        throws PrinterException {
    if (page_index > 0) {
        return Printable.NO_SUCH_PAGE;
    }

    // get the bounds of the component
    Dimension dim = comp.getSize();
    double cHeight = dim.getHeight();
    double cWidth = dim.getWidth();

    // get the bounds of the printable area
    double pHeight = format.getImageableHeight();
    double pWidth = format.getImageableWidth();

    double pXStart = format.getImageableX();
    double pYStart = format.getImageableY();

    double xRatio = pWidth / cWidth;
    double yRatio = pHeight / cHeight;


    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pXStart, pYStart);
    g2.scale(xRatio, yRatio);
    comp.paint(g2);

    return Printable.PAGE_EXISTS;
   }



  //_____________________________________pint Method
  public static void printing(){

  PrinterJob pjob = PrinterJob.getPrinterJob();
  PageFormat preformat = pjob.defaultPage();
  preformat.setOrientation(PageFormat.PORTRAIT);
  PageFormat postformat = pjob.defaultPage();
  //If user does not hit cancel then print.
  if (preformat != postformat) {
  //Set print component
   pjob.setPrintable(new testing(frame), postformat);
    //  if (pjob.printDialog()) {
    try {
        pjob.print();

    } catch (PrinterException ex) {
        Logger.getLogger(testing.class.getName()).log(Level.SEVERE, null, ex);
    }
//}
 }

  } 

有没有想法如何打印它适合打印机卷筒,因为没有任何选项可以打印适合打印卷纸的纸张。

Thnkx提前。

0 个答案:

没有答案