The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt.print  [6 examples]

e684. Printing Pages with Different Formats

A Book object is used when printing pages with different page formats. This example prints the first page in landscape and five more pages in portrait.
    public class PrintBook {
        public static void main(String[] args) {
            PrinterJob pjob = PrinterJob.getPrinterJob();
            Book book = new Book();
    
            // First part.
            PageFormat landscape = pjob.defaultPage();
            landscape.setOrientation(PageFormat.LANDSCAPE);
            book.append(new Printable1(), landscape);
    
            // Second part.
            PageFormat portrait = pjob.defaultPage();
            portrait.setOrientation(PageFormat.PORTRAIT);
            book.append(new Printable2(), portrait, 5);
    
            pjob.setPageable(book);
            try {
                pjob.print();
            } catch (PrinterException e) {
            }
        }
        static class Printable1 implements Printable {
            public int print(Graphics g, PageFormat pf, int pageIndex) {
                drawGraphics(g, pf);
                return Printable.PAGE_EXISTS;
            }
        }
        static class Printable2 implements Printable {
            public int print(Graphics g, PageFormat pf, int pageIndex) {
                drawGraphics(g, pf);
                return Printable.PAGE_EXISTS;
            }
        }
    }

 Related Examples
e681. The Quintessential Printing Program
e682. Getting the Dimensions of a Printed Page
e683. Setting the Orientation of a Printed Page
e685. Displaying the Page Format Dialog
e686. Displaying the Print Dialog


© 2002 Addison-Wesley.