Link to home
Start Free TrialLog in
Avatar of AIndy
AIndy

asked on

java.awt.print.Book -> wrong pages

Hi.

I am using the java.awt.print.Book to print out a quite complex document. Generating the book-object works fine, the preview which is displayed to the user before showing the PrintDialog does show the desired document.

The problem is when the user confirms the PrintDialog, always only the first 7 pages out of about 16 are printed. Even if I select "Pages 1 to 1" in the PrintDialog, I get the first 7 pages. I guess the error must be where I set and call the PrintDialog. This code is posted below.

Any hints are greatly appreciated.

     private void printBook(Book book, int selectedPage)
     {
         PrinterJob pj = PrinterJob.getPrinterJob();
         pj.setPageable(book);
         pj.setCopies(1);
         pj.setJobName("MyPrintJob");
         
         int[][] pageRange = {{(selectedPage==0)?1:selectedPage, (selectedPage==0)?book.getNumberOfPages():selectedPage}};        
         
         HashPrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
         attributeSet.add(new PageRanges(pageRange));
         attributeSet.add(OrientationRequested.PORTRAIT);
         
         if (pj.printDialog(attributeSet))
         {
              try
                  {
                    pj.print();
              }
              catch (PrinterException pe)
                  {
                   pe.printStackTrace();
                  }
         }
         
     }
ASKER CERTIFIED SOLUTION
Avatar of In-D
In-D

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial