Example usage for java.awt.print PrinterJob setPageable

List of usage examples for java.awt.print PrinterJob setPageable

Introduction

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

Prototype

public abstract void setPageable(Pageable document) throws NullPointerException;

Source Link

Document

Queries document for the number of pages and the PageFormat and Printable for each page held in the Pageable instance, document .

Usage

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);
    job.setJobName("My book");
    if (job.printDialog()) {
        try {//from  ww w.  java 2 s .c o  m
            job.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }
}

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);
    try {/*from  w w  w. j  a v  a 2 s . c om*/
        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);
    pj.print();//from  www .j a va2  s . c  o m
}

From source file:BookTest.java

public BookTestFrame() {
    setTitle("BookTest");

    text = new JTextField();
    add(text, BorderLayout.NORTH);

    attributes = new HashPrintRequestAttributeSet();

    JPanel buttonPanel = new JPanel();

    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);//from   w w w. j  av a 2s. co  m
    printButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPageable(makeBook());
                if (job.printDialog(attributes)) {
                    job.print(attributes);
                }
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(BookTestFrame.this, e);
            }
        }
    });

    JButton pageSetupButton = new JButton("Page setup");
    buttonPanel.add(pageSetupButton);
    pageSetupButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrinterJob job = PrinterJob.getPrinterJob();
            pageFormat = job.pageDialog(attributes);
        }
    });

    JButton printPreviewButton = new JButton("Print preview");
    buttonPanel.add(printPreviewButton);
    printPreviewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            PrintPreviewDialog dialog = new PrintPreviewDialog(makeBook());
            dialog.setVisible(true);
        }
    });

    add(buttonPanel, BorderLayout.SOUTH);
    pack();
}

From source file:JavaWorldPrintExample2.java

/**
 * Constructor: Example2/* w  ww  .j  av a  2s . 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//  w w  w .  j  a va  2  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:JavaWorldPrintExample3.java

/**
 * Constructor: Example3/*  www  .  j a va  2  s .c o  m*/
 * <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:com.floreantpos.jasperreport.engine.print.JRPrinterAWT.java

/**
 *
 *//*www. ja  v  a2s  . c o  m*/
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

/**
 *
 *//*  w ww  . 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

/**
 *
 *//* w  w w . jav a2 s  .  c  o  m*/
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;
}