Printing Pages with Different Formats - Java 2D Graphics

Java examples for 2D Graphics:Print

Description

Printing Pages with Different Formats

Demo Code

import java.awt.Graphics;
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;

public class Main {
  public static void main(String[] args) {
    PrinterJob pjob = PrinterJob.getPrinterJob();
    Book book = new Book();

    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);/* w  w  w .j  av a 2s  .  c o m*/
    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 Tutorials