Example usage for java.awt.print Book Book

List of usage examples for java.awt.print Book Book

Introduction

In this page you can find the example usage for java.awt.print Book Book.

Prototype

public Book() 

Source Link

Document

Creates a new, empty Book .

Usage

From source file:PrintBook.java

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);

    PageFormat portrait = pjob.defaultPage();
    portrait.setOrientation(PageFormat.PORTRAIT);
    book.append(new Printable2(), portrait, 5);

    pjob.setPageable(book);//from w ww . j av a 2s. c om
    try {
        pjob.print();
    } catch (PrinterException e) {
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    Book book = new Book();
    PageFormat defaultFormat = new PageFormat();
    defaultFormat = pj.defaultPage(defaultFormat);
    PageFormat landscapeFormat = new PageFormat();
    landscapeFormat.setOrientation(PageFormat.LANDSCAPE);
    PagePrinter[] page = new PagePrinter[2];
    int pageWidth = (int) defaultFormat.getImageableWidth();
    int pageHeight = (int) defaultFormat.getImageableHeight();
    Font font = new Font("Helvetica", Font.BOLD, 18);
    page[0] = new PagePrinter();
    page[0].addPrintElement(new MyItem("AAA", font, 100, pageHeight / 2));
    page[0].addPrintElement(new MyItem("line", 0, pageHeight, pageWidth, pageHeight));

    page[1] = new PagePrinter();
    page[1].addPrintElement(new MyItem("rectangle", 100, 100, pageWidth - 200, pageHeight - 200));
    page[1].addPrintElement(new MyItem("oval", 120, 120, pageWidth - 240, pageHeight - 240));

    book.append(page[0], defaultFormat);
    book.append(page[1], landscapeFormat);

    pj.setPageable(book);//from  w w w . j  a v a2  s .  co m
    pj.print();
}

From source file:MainClass.java

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);/*  w w w. j a  v a  2 s. com*/
    job.setJobName("My book");
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }
}

From source file:JavaWorldPrintExample3.java

/**
 * Constructor: Example3// ww w. jav  a  2 s.com
 * <p>
 *  
 */
public JavaWorldPrintExample3() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Add a third page using the same painter
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }

}

From source file:JavaWorldPrintExample2.java

/**
 * Constructor: Example2//from ww w  . j a v a 2  s .c  o m
 * <p>
 *  
 */
public JavaWorldPrintExample2() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }
}

From source file:JavaWorldPrintExample4.java

/**
 * Constructor: Example4//from  w  w  w . j  ava2  s  .  c  o m
 * <p>
 *  
 */
public JavaWorldPrintExample4() {

    //--- Create a new PrinterJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    //--- Create a new book to add pages to
    Book book = new Book();

    //--- Add the cover page using the default page format for this print
    // job
    book.append(new IntroPage(), printJob.defaultPage());

    //--- Add the document page using a landscape page format
    PageFormat documentPageFormat = new PageFormat();
    documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
    book.append(new Document(), documentPageFormat);

    //--- Tell the printJob to use the book as the pageable object
    printJob.setPageable(book);

    //--- Show the print dialog box. If the user click the
    //--- print button we then proceed to print else we cancel
    //--- the process.
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception PrintException) {
            PrintException.printStackTrace();
        }
    }
}

From source file:BookTest.java

/**
 * Makes a book that contains a cover page and the pages for the banner.
 *///from w w w. j  a  v  a 2 s  .co m
public Book makeBook() {
    if (pageFormat == null) {
        PrinterJob job = PrinterJob.getPrinterJob();
        pageFormat = job.defaultPage();
    }
    Book book = new Book();
    String message = text.getText();
    Banner banner = new Banner(message);
    int pageCount = banner.getPageCount((Graphics2D) getGraphics(), pageFormat);
    book.append(new CoverPage(message + " (" + pageCount + " pages)"), pageFormat);
    book.append(banner, pageFormat, pageCount);
    return book;
}

From source file:com.floreantpos.jasperreport.engine.print.JRPrinterAWT.java

/**
 *
 *///from  w w w  .  j  a va2 s.c  om
private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    printerName = jasperPrint.getProperty("printerName");
    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName(jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (withPrintDialog) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}

From source file:com.openbravo.pos.util.JRPrinterAWT411.java

/**
 *
 *///from   w w w .ja v  a  2 s .  c o m
private boolean printPages(int firstPageIndex, int lastPageIndex, PrintService service) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (service == null) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.setPrintService(service);
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}

From source file:com.openbravo.pos.util.JRPrinterAWT.java

/**
 *
 *///from ww w  .j  a v  a2  s.c om
public boolean printPages(int firstPageIndex, int lastPageIndex, PrintService service) throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
            || lastPageIndex >= jasperPrint.getPages().size()) {
        throw new JRException("Invalid page index range : " + firstPageIndex + " - " + lastPageIndex + " of "
                + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientationValue()) {
    case LANDSCAPE: {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
        paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
        break;
    }
    case PORTRAIT:
    default: {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
    }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
        if (service == null) {
            if (printJob.printDialog()) {
                printJob.print();
            } else {
                isOK = false;
            }
        } else {
            printJob.setPrintService(service);
            printJob.print();
        }
    } catch (Exception ex) {
        throw new JRException("Error printing report.", ex);
    }

    return isOK;
}