Example usage for java.awt.print PageFormat setOrientation

List of usage examples for java.awt.print PageFormat setOrientation

Introduction

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

Prototype

public void setOrientation(int orientation) throws IllegalArgumentException 

Source Link

Document

Sets the page orientation.

Usage

From source file:org.pentaho.reporting.engine.classic.extensions.modules.java14print.Java14PrintUtil.java

public static PageFormat extractPageFormat(final PrintRequestAttributeSet attributeSet) {
    final Media media = (Media) attributeSet.get(Media.class);
    final MediaPrintableArea printableArea = (MediaPrintableArea) attributeSet.get(MediaPrintableArea.class);
    final OrientationRequested orientationRequested = (OrientationRequested) attributeSet
            .get(OrientationRequested.class);

    final MediaSize mediaSize = lookupMediaSize(media);
    if (mediaSize == null) {
        logger.warn("Unknown media encountered, unable to compute page sizes.");
    }//w ww.j a v  a  2 s  .com

    final PageFormat pageFormat = new PageFormat();
    pageFormat.setPaper(createPaper(mediaSize, printableArea));
    if (OrientationRequested.PORTRAIT.equals(orientationRequested)) {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
    } else if (OrientationRequested.LANDSCAPE.equals(orientationRequested)) {
        pageFormat.setOrientation(PageFormat.LANDSCAPE);
    } else if (OrientationRequested.REVERSE_LANDSCAPE.equals(orientationRequested)) {
        pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
    } else if (OrientationRequested.REVERSE_PORTRAIT.equals(orientationRequested)) {
        pageFormat.setOrientation(PageFormat.PORTRAIT);
    }
    return pageFormat;
}

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

/**
 *
 *//*from ww  w.  jav a 2s  .c  om*/
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;
}