0

Hey I'm trying to make an invoice software everything is fine while I'm printing bill then it is printing according to standard printer size but I want to print it through billing printer I'm not using jesper report or any other reporting software I'm just print JFrame by swing code.

Here is my code. . .

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

  } 

Is there any Idea how to print it fit the printer roll because there is no any option to print paper fit to printing roll paper.

Thnkx in advance.

Charith Prabhagya
  • 189
  • 1
  • 3
  • 11
Numan Pathan
  • 149
  • 7
  • I'd really, really advise trying to do this manually - but, if you're trying to print a component directly, then you change the size of the component to meet the size of the paper - the problem with this is you need to determine how many pages you'd need to fit the component onto, not an easy task – MadProgrammer Sep 30 '17 at 11:48
  • [This is one example](https://stackoverflow.com/questions/29070689/printing-a-2-pages-of-jframe-in-java/29137211#29137211) of printing a component across multiple pages, [this is another](https://stackoverflow.com/questions/32000888/how-to-print-whole-jpanel-with-a-footer-in-every-page-in-java-swing/32001433#32001433) – MadProgrammer Sep 30 '17 at 11:52
  • You may also want to have a look at [this example](https://stackoverflow.com/questions/13558152/how-can-i-print-a-custom-paper-size-cheques-8-x-4/13558335#13558335) which defines a custom page size – MadProgrammer Sep 30 '17 at 11:54
  • But I'd recommend Jasper Reports :P – MadProgrammer Sep 30 '17 at 11:54

0 Answers0