Title: how to use Pageable Interface to avoid printing range 1-9999
Tags: java;swing;netbeans;printing
Question: I am trying to print a ```JPanel``` called ```print_p```.It contains a Table and some labels.

The error is in print dialog its range 1-9999

How can I fix this issue?

```private void printCard(){

    PrinterJob printjob = PrinterJob.getPrinterJob();
    printjob.setJobName(" Test Report ");        

    printjob.setPrintable (new Printable() {      

        @Override             
        public int print(Graphics pg, PageFormat pf, int pageNum){               
            pf.setOrientation(PageFormat.LANDSCAPE);

            if (pageNum &gt; 0){
               return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2 = (Graphics2D) pg;
            g2.translate(pf.getImageableX(), pf.getImageableY());
            g2.translate(0f, 0f);
            print_p.paint(g2);

            return Printable.PAGE_EXISTS;
        }
    });        

    if (printjob.printDialog() == false)
       return;

    try {
       printjob.print();
    } catch (PrinterException ex) {
       System.out.println("NO PAGE FOUND."+ex);
    }
}
```

Comment: Using `Printable`, you can't, it's not an error, until the `Printerable` is printed, it has no way to determine the number of pages that might be printed.  Take a look at the [`java.awt.print.Book`](http://docs.oracle.com/javase/7/docs/api/java/awt/print/Book.html) API instead...

Here is the accepted answer: When using ```Printable```, this is expected behaviour, as the dialog has no idea of how many pages might be printed, as nothing about the print job has been processed.

You need to use a ```Pagable``` interface.  This allows you to collect a series of ```Printable```s, each which represents a single page within the virtual book.

For a ready made implementation, you can take a look at ```java.awt.print.Book```

Updated with example



```import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PrintTest {

    public static void main(String[] args) {
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setJobName("Book 'em Danno");

        PageFormat pf = pj.defaultPage();
        pf.setOrientation(PageFormat.LANDSCAPE);
        Book book = new Book();
        for (int index = 0; index < 10; index++) {
            book.append(new Page(index + 1), pf);
        }
        pj.setPageable(book);
        if (pj.printDialog()) {
            try {
                pj.print();
            } catch (PrinterException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static class Page implements Printable {

        private int page;

        public Page(int page) {

            this.page = page;

        }

        @Override
        public int print(Graphics graphics, PageFormat pf, int pageIndex) throws PrinterException {

            Graphics2D g2 = (Graphics2D) graphics;
            g2.translate(pf.getImageableX(), pf.getImageableY());
            g2.translate(0f, 0f);
            FontMetrics fm = g2.getFontMetrics();
            String text = Integer.toString(page);

            double y = (pf.getImageableHeight() - fm.getHeight()) + fm.getAscent();
            double x = (pf.getImageableWidth() - fm.stringWidth(text)) / 2d;

            g2.drawString(text, (float)x, (float)y);

            System.out.println(pageIndex);

            return PAGE_EXISTS;
        }

    }

}
```

Comment for this answer: I got the point , but until now I am trying to insert  Pagable interface to my code 
I added 
           Book bk = new Book();
          bk.append(this, pageFormat);
          printJob.setPageable(bk);
put the dialog wasn't run.
Comment for this answer: it's work good now , but the tables had some columns out of the page , can I solve this without resizing the Jpanels contents  ?
Comment for this answer: @HabibAl-Ghoul Are you still having problems?  I've updated the answer with a runnable example which seems to work just fine for me...

Here is another answer: You can do this with Printable as well just use 

```PrintRequestAttributeSet attribs = new HashPrintRequestAttributeSet();
attribs.add(new PageRanges(firstPageIndex + 1, lastPageIndex + 1));
```

ant when you calling PrintDialog

```pj.printDialog(attribs)
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:09/30/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><swing><netbeans><printing>', 'Title': 'how to use Pageable Interface to avoid printing range 1-9999', 'ViewCount': 2789, 'id': 26131405, 'user_AboutMe': None, 'user_AccountId': 5109865.0, 'user_DisplayName': 'Habib Al-Ghoul', 'user_Reputation': 3.0, 'user_Views': 7.0, 'user_id': 4096831.0}}