2

I need to print an image. When I set orientation like

printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);

all works fine.

But when I set orientation inside print() method of Printable:

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex >= images.size()) 
                return Printable.NO_SUCH_PAGE;

            image = images.get(pageIndex);
            // if image width>height --> Landscape, else --> Protrait
            if (image.getWidth(null) > image.getHeight(null)) 
                pageFormat.setOrientation(PageFormat.LANDSCAPE);
            else 
                pageFormat.setOrientation(PageFormat.PORTRAIT);

            graphics2D = (Graphics2D) graphics;
            graphics.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null),   null);

            return PAGE_EXISTS;
};

it doesn't work with first page. i.e. it prints all pages in Landscape mode except 1-st page.

WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • 1
    Duplicated question. Look at [Page printing help on first page](http://stackoverflow.com/questions/10495800/page-printing-help-on-first-page) – cubanacan Aug 30 '12 at 08:26
  • `.... pageFormat.setOrientation(PageFormat.LANDSCAPE);` What code is in the `....` part? For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Aug 30 '12 at 08:26
  • @AndrewThompson code is simple. See updated answer – WelcomeTo Aug 30 '12 at 08:53
  • 1
    You can not change the orientation when your already trying to print the page. If you need to provide a number of pages with different orientations, you will want to look at the [Book](http://docs.oracle.com/javase/7/docs/api/java/awt/print/Book.html) [Pageable](http://docs.oracle.com/javase/7/docs/api/java/awt/print/Pageable.html) interfaces, see [Printing in Java](http://docs.oracle.com/javase/7/docs/api/java/awt/print/Book.html) for examples – MadProgrammer Aug 30 '12 at 09:20
  • You can pass `PageFormat` argument into `setPrintable` method of `PrinterJob` class. So it will be initialized before printing. – cubanacan Aug 30 '12 at 09:25
  • @cubanacan and it will print ALL pages (inside Printable) in certain format? – WelcomeTo Aug 30 '12 at 09:39
  • @MyTitle yes, look at answer I have just posted on [Page printing help on first page](http://stackoverflow.com/questions/10495800/page-printing-help-on-first-page/12193974#12193974) – cubanacan Aug 30 '12 at 09:44
  • @MadProgrammer i.e. I need create `Book` object, and add to it as many `Printable` objects as I need? For example I need to print _page1_ in `Landscape` mode, _page2_ in `Portrait`, _page3_ again in `Landscape` and so on.. So I need to create 3 Printable objects, and set to first `PageFormat` with `Landscape` orientation, to second with `Portrait`, to third again with `Landscape` and so on? and then add 3 `Printable` objects to book, and create `new SimpleDoc(book, flavor, attr)`? – WelcomeTo Aug 30 '12 at 10:41
  • @MyTitle that or rotate the images in the printable ;) – MadProgrammer Aug 30 '12 at 11:06
  • @MadProgrammer ok..not good... b.t.w. thanks for your help from day to day. Write first comment as answer, I'll accept it – WelcomeTo Aug 30 '12 at 11:09

2 Answers2

4

You can not change the orientation when your already trying to print the page.

If you need to provide a number of pages with different orientations, you will want to look at the Book and Pageable interfaces, see Printing in Java for examples.

The only other solution you have is to rotate the image in Printable, which is troublesome at best.

ps - Printing is fun...when it works ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Sorry for being late, but I decide to use your solution. So I have a question: `Book` and `Pageable` interfaces are interchangeable? i.e. if I use Book then I don't need to use `Pageable`? – WelcomeTo Apr 16 '13 at 05:34
  • Well, `Book` implements `Pagable`. So you could create a `Book` and pass it to methods that were expected `Pagable`, but not the other way round. – MadProgrammer Apr 16 '13 at 05:48
0

Place pf.setOrientation(PageFormat.LANDSCAPE) before calling printJob

ex.

if(pay_type.getSelectedItem().equals("EMI"))
  {
      EMI();
  }
  else{
       validation();  
  }
   PrinterJob job = PrinterJob.getPrinterJob();
     job.setPrintable((Printable) this);
     boolean ok = job.printDialog();
     if (ok) {
         try {
             //here set page oriatetion
              job.print();
         } catch (PrinterException ex) {
          /* The job did not successfully complete */
         }
     }
user2807083
  • 2,962
  • 4
  • 29
  • 37
Kalpesh A. Nikam
  • 324
  • 3
  • 10