abc123
abc123

Reputation: 1

Print does not work Java

I want to print a document. This code below show the Print dialog, but when I click Print, nothing is printed.

PrinterJob job;
job = PrinterJob.getPrinterJob();
if (job.printDialog()){
  try{
     job.print();
  }catch(Exception e){
  }
}

Did I miss anything ? Like format page ?

Thanks

Upvotes: 0

Views: 188

Answers (1)

user489041
user489041

Reputation: 28294

You have to implement the Printable Interface for what you want to print and set that as the job:

job.setPrintable(printable);

I usually start my Printable code like this:

public int print(Graphics g, PageFormat pf, int i) throws PrinterException {
        if (i > getPrintableImages(pf).size() - 1) {
            //returning this stops printing
            return NO_SUCH_PAGE;
        }

        /*
         * User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

Upvotes: 2

Related Questions