Java by API/java.awt.print/PageFormat — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:33, 31 мая 2010

PageFormat: getImageableHeight()

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
        - margin * 2);
    pf.setPaper(paper);
    pj.setPrintable(new MyPrintable(), pf);
    if (pj.printDialog()) {
      try {
        pj.print();
      } catch (PrinterException e) {
        System.out.println(e);
      }
    }
  }
}
class MyPrintable implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0)
      return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 36));
    g2.setPaint(Color.black);
    g2.drawString("www.jexp.ru", 100, 100);
    Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf
        .getImageableWidth(), pf.getImageableHeight());
    g2.draw(outline);
    return PAGE_EXISTS;
  }
}





PageFormat: getImageableWidth()

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
        - margin * 2);
    pf.setPaper(paper);
    pj.setPrintable(new MyPrintable(), pf);
    if (pj.printDialog()) {
      try {
        pj.print();
      } catch (PrinterException e) {
        System.out.println(e);
      }
    }
  }
}
class MyPrintable implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0)
      return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 36));
    g2.setPaint(Color.black);
    g2.drawString("www.jexp.ru", 100, 100);
    Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf
        .getImageableWidth(), pf.getImageableHeight());
    g2.draw(outline);
    return PAGE_EXISTS;
  }
}





PageFormat: getImageableX()

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
        - margin * 2);
    pf.setPaper(paper);
    pj.setPrintable(new MyPrintable(), pf);
    if (pj.printDialog()) {
      try {
        pj.print();
      } catch (PrinterException e) {
        System.out.println(e);
      }
    }
  }
}
class MyPrintable implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0)
      return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 36));
    g2.setPaint(Color.black);
    g2.drawString("www.jexp.ru", 100, 100);
    Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf
        .getImageableWidth(), pf.getImageableHeight());
    g2.draw(outline);
    return PAGE_EXISTS;
  }
}





PageFormat: getImageableY()

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
        - margin * 2);
    pf.setPaper(paper);
    pj.setPrintable(new MyPrintable(), pf);
    if (pj.printDialog()) {
      try {
        pj.print();
      } catch (PrinterException e) {
        System.out.println(e);
      }
    }
  }
}
class MyPrintable implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0)
      return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 36));
    g2.setPaint(Color.black);
    g2.drawString("www.jexp.ru", 100, 100);
    Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf
        .getImageableWidth(), pf.getImageableHeight());
    g2.draw(outline);
    return PAGE_EXISTS;
  }
}





PageFormat.LANDSCAPE

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterGraphics;
import java.awt.print.PrinterJob;
public class MainClass {
  public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    Book bk = new Book();
    bk.append(new paintCover(), pf);
    bk.append(new paintContent(), job.defaultPage(), 1);
    job.setPageable(bk);
    job.setJobName("My book");
    if (job.printDialog()) {
      try {
        job.print();
      } catch (PrinterException e) {
        System.out.println(e);
      }
    }
  }
}
class paintContent implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    System.out.println("Page index = " + pageIndex);
    // pageIndex 1 corresponds to page number 2.
    if (pageIndex > 2)
      return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();
    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);
    g2.setColor(Color.red);
    g2.draw(r);
    return Printable.PAGE_EXISTS;
  }
}
class paintCover implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();
    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);
    g2.setColor(Color.red);
    g2.draw(r);
    PrinterGraphics p = (PrinterGraphics) g2;
    String s = p.getPrinterJob().getJobName();
    g2.setPaint(Color.black);
    g2.drawString(s, 0, 0);
    return Printable.PAGE_EXISTS;
  }
}





PageFormat: setOrientation(int orientation)

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterGraphics;
import java.awt.print.PrinterJob;
public class MainClass {
  public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    Book bk = new Book();
    bk.append(new paintCover(), pf);
    bk.append(new paintContent(), job.defaultPage(), 1);
    job.setPageable(bk);
    job.setJobName("My book");
    if (job.printDialog()) {
      try {
        job.print();
      } catch (PrinterException e) {
        System.out.println(e);
      }
    }
  }
}
class paintContent implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    System.out.println("Page index = " + pageIndex);
    // pageIndex 1 corresponds to page number 2.
    if (pageIndex > 2)
      return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();
    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);
    g2.setColor(Color.red);
    g2.draw(r);
    return Printable.PAGE_EXISTS;
  }
}
class paintCover implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    int xo = (int) pf.getImageableX();
    int yo = (int) pf.getImageableY();
    Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h);
    g2.setColor(Color.red);
    g2.draw(r);
    PrinterGraphics p = (PrinterGraphics) g2;
    String s = p.getPrinterJob().getJobName();
    g2.setPaint(Color.black);
    g2.drawString(s, 0, 0);
    return Printable.PAGE_EXISTS;
  }
}





PageFormat: setPaper(Paper paper)

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
        - margin * 2);
    pf.setPaper(paper);
    pj.setPrintable(new MyPrintable(), pf);
    if (pj.printDialog()) {
      try {
        pj.print();
      } catch (PrinterException e) {
        System.out.println(e);
      }
    }
  }
}
class MyPrintable implements Printable {
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0)
      return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setFont(new Font("Serif", Font.PLAIN, 36));
    g2.setPaint(Color.black);
    g2.drawString("www.jexp.ru", 100, 100);
    Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf
        .getImageableWidth(), pf.getImageableHeight());
    g2.draw(outline);
    return PAGE_EXISTS;
  }
}