0

I am building a Invoice program & I need to print whats on the Jpanel which is called contentPane in my code. But I can't get it to print anything other then blank pages.

Not sure what I am missing but this is the print code I am trying to use.

PrinterJob job = PrinterJob.getPrinterJob();
        job.setJobName("Print Data");

        job.setPrintable(new Printable(){
        public int print(Graphics pg,PageFormat pf, int pageNum){
                pf.setOrientation(PageFormat.LANDSCAPE);
             if(pageNum>0){
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2 = (Graphics2D)pg;
            g2.translate(pf.getImageableX(), pf.getImageableY());
            g2.scale(0.24,0.24);

            contentPane.paint(g2);


            return Printable.PAGE_EXISTS;


        }
});

    boolean ok = job.printDialog();
    if(ok){
    try{

    job.print();
    }
    catch (PrinterException ex){}
    }

I have tried contentPane.print(); as well as contentPane.printall(); Also commented the g2.scale to see what happeneds

Still prints a blank page?

Any idea's or suggestions. I am still learning Java so be easy on me lol

Sparish
  • 13
  • 5
  • 2
    First suggestion, fix this: `catch (PrinterException ex){}`. Code like this should ***never*** be in your code. – Hovercraft Full Of Eels Dec 18 '17 at 01:28
  • 1
    *"I am building a Invoice program & I need to print whats on the Jpanel which is called contentPane in my code."* - Honestly, invest the time in a good reporting engine, like Jasper Reports, printing manually really is very complicated and time consuming to get right – MadProgrammer Dec 18 '17 at 01:28
  • 1
    Never ever call `paint` directly. Instead, you should be using `print` or `printAll` – MadProgrammer Dec 18 '17 at 01:29
  • Well, [that's a runnable example](https://stackoverflow.com/questions/12764634/printing-a-jframe-and-its-components/12765916#12765916) you can use to compare to see what you're doing differently – MadProgrammer Dec 18 '17 at 02:39
  • Thanks for the feedback guys. I will for sure look into Jasper Reports & the runnable example & go from there. – Sparish Dec 18 '17 at 04:29

0 Answers0