Example usage for java.awt.print PrinterJob setJobName

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

Introduction

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

Prototype

public abstract void setJobName(String jobName);

Source Link

Document

Sets the name of the document to be printed.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    try {//from  w w w  .  ja  v a 2 s .c  om
        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setJobName("Graphics Demo Printout");
        pjob.setCopies(1);
        pjob.setPrintable(new Printable() {
            public int print(Graphics pg, PageFormat pf, int pageNum) {
                if (pageNum > 0) // we only print one page
                    return Printable.NO_SUCH_PAGE; // ie., end of job

                pg.drawString("www.java2s.com", 10, 10);

                return Printable.PAGE_EXISTS;
            }
        });

        if (pjob.printDialog() == false) // choose printer
            return;
        pjob.print();
    } catch (PrinterException pe) {
        pe.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();

    job.setPrintable(new PrintDemo1());

    job.setCopies(2);/*from  w  w w.  j  a v  a 2 s .c  o  m*/
    job.setJobName("Printable");

    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }

    System.exit(0);
}

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

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

/**
 *
 *//*  www .j ava2s  . com*/
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   www  .  ja  v a2 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   w w  w  .  j  av  a 2 s.  co  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;
}

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

/**
 *
 *//*from   ww w  .  j  av a2s  . 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:org.jab.docsearch.DocSearch.java

private void doPrint() {
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName("docSearcher");
    pj.setPageable(vista);//from  ww w . jav  a2 s  .c o  m
    try {
        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);
    }/*from w  w  w .j a  va 2s.co 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 a  va2s .c  o  m

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