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:net.sf.jasperreports.engine.print.JRPrinterAWT.java

/**
 *
 *//*from w  w w.j ava 2 s.  co  m*/
public 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());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);
    if (jasperPrint.getProperty("printService") != null) {
        String printServiceName = jasperPrint.getProperty("printService");
        try {
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            for (PrintService se : services) {
                if (se.getName().contains(printServiceName)) {
                    printJob.setPrintService(se);
                    break;
                }
            }

        } catch (PrinterException ex) {
            Logger.getLogger(JRPrinterAWT.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    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 (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:org.apache.pdfbox.debugger.PDFDebugger.java

private void printMenuItemActionPerformed(ActionEvent evt) {
    if (document != null) {
        try {//from   www . ja v  a  2  s .  co m
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPageable(new PDFPageable(document));
            if (job.printDialog()) {
                job.print();
            }
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.jab.docsearch.DocSearch.java

private void doPrint() {
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName("docSearcher");
    pj.setPageable(vista);
    try {/* www.  j  av a  2  s . c o  m*/
        if (pj.printDialog()) {
            pj.print();
        }
    } catch (PrinterException pe) {
        logger.fatal("doPrint() failed with PrinterException", pe);
        showMessage(dsErrPrint, pe.toString());
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.gui.print.PrintUtil.java

public static void printDirectly(final MasterReport report, final ReportProgressListener progressListener)
        throws PrinterException, ReportProcessingException {
    final ModifiableConfiguration reportConfiguration = report.getReportConfiguration();
    final String jobName = reportConfiguration.getConfigProperty(PRINTER_JOB_NAME_KEY, report.getTitle());

    final PrinterJob printerJob = PrinterJob.getPrinterJob();
    if (jobName != null) {
        printerJob.setJobName(jobName);/*  w w w . j a v a2  s. c o  m*/
    }

    final PrintReportProcessor reportPane = new PrintReportProcessor(report);
    if (progressListener != null) {
        reportPane.addReportProgressListener(progressListener);
    }
    printerJob.setPageable(reportPane);
    try {
        printerJob.setCopies(getNumberOfCopies(reportConfiguration));
        printerJob.print();
    } finally {
        reportPane.close();
        if (progressListener != null) {
            reportPane.removeReportProgressListener(progressListener);
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.gui.print.PrintUtil.java

public static boolean print(final MasterReport report, final ReportProgressListener progressListener)
        throws PrinterException, ReportProcessingException {
    final ModifiableConfiguration reportConfiguration = report.getReportConfiguration();
    final String jobName = reportConfiguration.getConfigProperty(PRINTER_JOB_NAME_KEY, report.getTitle());

    final PrinterJob printerJob = PrinterJob.getPrinterJob();
    if (jobName != null) {
        printerJob.setJobName(jobName);/*from   w ww  .  j av  a 2s.c  om*/
    }

    final PrintReportProcessor reportPane = new PrintReportProcessor(report);
    if (progressListener != null) {
        reportPane.addReportProgressListener(progressListener);
    }

    try {
        reportPane.fireProcessingStarted();
        printerJob.setPageable(reportPane);
        printerJob.setCopies(getNumberOfCopies(reportConfiguration));
        if (printerJob.printDialog()) {
            printerJob.print();
            return true;
        }
        return false;
    } finally {
        reportPane.fireProcessingFinished();
        reportPane.close();
        if (progressListener != null) {
            reportPane.removeReportProgressListener(progressListener);
        }
    }
}

From source file:org.sanjose.util.JRPrinterAWT.java

/**
 *
 *//*w ww . jav a 2  s .co m*/
private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog,
        PrintService pService) 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);

    try {
        printJob.setPrintService(pService);
    } catch (PrinterException e) {
        e.printStackTrace();
        throw new JRException(e.getMessage());
    }

    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 (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:pcgen.gui2.dialog.PrintPreviewDialog.java

@Override
public void actionPerformed(ActionEvent e) {
    if (SHEET_COMMAND.equals(e.getActionCommand())) {
        new PreviewLoader((URI) sheetBox.getSelectedItem()).execute();
    } else if (PAGE_COMMAND.equals(e.getActionCommand())) {
        previewPanel.setPage(pageBox.getSelectedIndex());
    } else if (ZOOM_COMMAND.equals(e.getActionCommand())) {
        Double zoom = (Double) zoomBox.getSelectedItem();
        previewPanel.setScaleFactor(zoom);
    } else if (ZOOM_IN_COMMAND.equals(e.getActionCommand())) {
        Double zoom = (Double) zoomBox.getSelectedItem();
        zoomBox.setSelectedItem(zoom * ZOOM_MULTIPLIER);
    } else if (ZOOM_OUT_COMMAND.equals(e.getActionCommand())) {
        Double zoom = (Double) zoomBox.getSelectedItem();
        zoomBox.setSelectedItem(zoom / ZOOM_MULTIPLIER);
    } else if (PRINT_COMMAND.equals(e.getActionCommand())) {
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        printerJob.setPageable(pageable);
        if (printerJob.printDialog()) {
            try {
                printerJob.print();/*w w w  .java2  s. c o  m*/
                dispose();
            } catch (PrinterException ex) {
                String message = "Could not print " + character.getNameRef().get();
                Logging.errorPrint(message, ex);
                frame.showErrorMessage(Constants.APPLICATION_NAME, message);
            }
        }
    } else if (CANCEL_COMMAND.equals(e.getActionCommand())) {
        dispose();
    }
}