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:com.floreantpos.jasperreport.engine.print.JRPrinterAWT.java

/**
 *
 */// ww  w  .  ja  va  2 s.  co 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  . j a  v a 2  s .c om*/
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   www . j av  a 2 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;
}

From source file:org.gumtree.vis.core.internal.SWTChartComposite.java

/**
 * Creates a print job for the chart./*w  w w  .  ja  va2  s.c  om*/
 */
@Override
public void createChartPrintJob() {
    final PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    //      PageFormat pf2 = job.pageDialog(pf);
    PageFormat pf2 = job.defaultPage();
    pf2.setOrientation(PageFormat.LANDSCAPE);
    if (pf2 != pf) {
        Printable print = new MyPrintable();
        job.setPrintable(print, pf2);
        if (job.printDialog()) {
            getDisplay().asyncExec(new Runnable() {

                public void run() {
                    try {
                        job.print();
                    } catch (PrinterException e) {
                        MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.ICON_ERROR);
                        messageBox.setMessage(e.getMessage());
                        messageBox.open();
                    }
                }
            });
        }
    }

    //      PrintDialog dialog = new PrintDialog(getShell());
    //      // Prompts the printer dialog to let the user select a printer.
    //      PrinterData printerData = dialog.open();
    //
    //      if (printerData == null) // the user cancels the dialog
    //         return;
    //      // Loads the printer.
    //      final Printer printer = new Printer(printerData);
    //      getDisplay().asyncExec(new Runnable(){
    //
    //         public void run() {
    //            print(printer);
    //            printer.dispose();            
    //         }});
}

From source file:net.sf.jasperreports.engine.print.JRPrinterAWT.java

/**
 *
 *///from w  w w .j av a  2s .  c o 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:com.servoy.j2db.util.Utils.java

/**
 * Create a PageFormat object from the dimensions and margins.
 *
 * @param width the actual paper width - ignoring orientation. It is the width of the paper as seen by the printer.
 * @param height the actual paper height - ignoring orientation. It is the height of the paper as seen by the printer.
 * @param lm left margin of the page, not paper. So this is the left margin affected by orientation, as used in application.
 * @param rm right margin of the page, not paper. So this is the right margin affected by orientation, as used in application.
 * @param tm top margin of the page, not paper. So this is the top margin affected by orientation, as used in application.
 * @param bm bottom margin of the page, not paper. So this is the bottom margin affected by orientation, as used in application.
 * @param orientation the orientation of the page. Establishes a relation between page and paper coordinates.
 * @param units INCHES or MM.//from  w  ww .j  a  v  a  2s .  com
 * @return the required PageFormat object.
 */
public static PageFormat createPageFormat(double width, double height, double lm, double rm, double tm,
        double bm, int orientation, int units) {
    double pixWidth = convertPageFormatUnit(units, Size2DSyntax.INCH, width) * PPI;
    double pixHeight = convertPageFormatUnit(units, Size2DSyntax.INCH, height) * PPI;
    double pixLm = convertPageFormatUnit(units, Size2DSyntax.INCH, lm) * PPI;
    double pixRm = convertPageFormatUnit(units, Size2DSyntax.INCH, rm) * PPI;
    double pixTm = convertPageFormatUnit(units, Size2DSyntax.INCH, tm) * PPI;
    double pixBm = convertPageFormatUnit(units, Size2DSyntax.INCH, bm) * PPI;

    // The margins of the Paper object are relative to the physical paper, so independent
    // of the text orientation; The PageFormat object takes the orientation into account relative to the text.
    // We have to convert back to the paper-relative margins here...
    double paperLm;
    double paperRm;
    double paperTm;
    double paperBm;
    if (orientation == PageFormat.LANDSCAPE) {
        paperLm = pixTm;
        paperRm = pixBm;
        paperTm = pixRm;
        paperBm = pixLm;
    } else if (orientation == PageFormat.PORTRAIT) {
        paperLm = pixLm;
        paperRm = pixRm;
        paperTm = pixTm;
        paperBm = pixBm;
    } else
    // orientation == PageFormat.REVERSE_LANDSCAPE
    {
        paperLm = pixBm;
        paperRm = pixTm;
        paperTm = pixLm;
        paperBm = pixRm;
    }

    PageFormat pf = new PageFormat();
    pf.setOrientation(orientation);
    Paper paper = new Paper();
    paper.setSize(pixWidth, pixHeight);
    paper.setImageableArea(paperLm, paperTm, pixWidth - (paperLm + paperRm), pixHeight - (paperTm + paperBm));
    pf.setPaper(paper);

    return pf;
}

From source file:com.isti.traceview.common.TraceViewChartPanel.java

/**
 * Creates a print job for the chart.//from ww w . j ava  2 s .com
 */
public void createChartPrintJob() {

    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    pf.setOrientation(PageFormat.LANDSCAPE);
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            } catch (PrinterException e) {
                JOptionPane.showMessageDialog(this, e);
            }
        }
    }
}

From source file:PageFormatFactory.java

/**
 * Creates a new pageformat using the given paper and the given orientation.
 *
 * @param paper       the paper to use in the new pageformat
 * @param orientation one of PageFormat.PORTRAIT, PageFormat.LANDSCAPE or PageFormat.REVERSE_LANDSCAPE
 * @return the created Pageformat// www .  j  a va2 s .c o m
 * @throws NullPointerException if the paper given was null
 */
public PageFormat createPageFormat(final Paper paper, final int orientation) {
    if (paper == null) {
        throw new NullPointerException("Paper given must not be null");
    }
    final PageFormat pf = new PageFormat();
    pf.setPaper(paper);
    pf.setOrientation(orientation);
    return pf;
}

From source file:PageFormatFactory.java

/**
 * Restores a page format after it has been serialized.
 *
 * @param data the serialized page format data.
 * @return the restored page format./*w ww . j  a  v  a2 s.  c om*/
 * @deprecated This functionality is part of JCommon-Serializer
 */
public PageFormat createPageFormat(final Object[] data) {
    final Integer orientation = (Integer) data[0];
    final float[] dim = (float[]) data[1];
    final float[] rect = (float[]) data[2];
    final Paper p = new Paper();
    p.setSize(dim[0], dim[1]);
    p.setImageableArea(rect[0], rect[1], rect[2], rect[3]);
    final PageFormat format = new PageFormat();
    format.setPaper(p);
    format.setOrientation(orientation.intValue());
    return format;
}

From source file:gda.plots.SimplePlot.java

/**
 * This overrides the method in ChartPanel which seems to behave slightly differently. The replacement may be
 * unecessary -investigate./*from  w  ww.  j  ava2  s  .  c  om*/
 */
@Override
public void createChartPrintJob() {
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    PageFormat pageFormat = printerJob.defaultPage();
    pageFormat.setOrientation(PageFormat.LANDSCAPE);
    printerJob.setPrintable(this, pageFormat);
    try {
        if (printerJob.printDialog()) {
            printerJob.print();
        }
    } catch (PrinterException pe) {
        logger.error("Caught PrinterException: " + pe.getMessage());
    }
}