Java Code Examples for java.awt.print.PrinterJob#setJobName()

The following examples show how to use java.awt.print.PrinterJob#setJobName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrintDialog.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onOk() {
    super.onOk();
    final PrinterJob job = framework.getPrinterJob("idef0");
    try {
        printable.setPrintFunctions(panel.getSelectedFunctions());
        job.setPrintable(printable.createPrintable(), printable
                .getPageFormat());
        job.setJobName(printable.getJobName());
        if (job.printDialog()) {

            job.print();
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: PrintUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 );
  }

  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 );
    }
  }
}
 
Example 3
Source File: AbstractRamusPrintable.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void print(GUIFramework framework) throws PrinterException {
    final PrinterJob pj = framework.getPrinterJob(getJobKey());
    final Printable printable = createPrintable();
    pj.setPrintable(printable, getPageFormat());

    if (pj.printDialog()) {
        pj.setJobName(getJobName());
        pj.print();
        setPageFormat(getPageFormat());
    }
}
 
Example 4
Source File: DummyPrintTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void thirdPartyPrintLogic(String printerName) throws Exception {
    PrinterJob printerjob = PrinterJob.getPrinterJob();
    printerjob.setCopies(2);
    printerjob.setJobName("myJobName");
    printerjob.setPrintable(new DummyPrintable());
    for (PrintService printService : PrinterJob.lookupPrintServices()) {
        System.out.println("check printer name of service " + printService);
        if (printerName.equals(printService.getName())) {
            System.out.println("correct printer service do print...");
            printerjob.setPrintService(printService);
            printerjob.print();
            break;
        }
    }
}
 
Example 5
Source File: LandscapeStackOverflow.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static final void main( String[] parameters ) {
    PrinterJob printjob = PrinterJob.getPrinterJob();
    printjob.setJobName( "Test Print Job" );

    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    attributes.add( OrientationRequested.LANDSCAPE );

    try {
        printjob.setPrintable( new Painter() );
        printjob.print( attributes );
    } catch( PrinterException exception ) {
        exception.printStackTrace();
    }
}
 
Example 6
Source File: PrintUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 );
  }

  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 );
    }
  }
}
 
Example 7
Source File: JRPrinterAWT.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 *
 */
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(
				EXCEPTION_MESSAGE_KEY_INVALID_PAGE_RANGE,  
				new Object[]{firstPageIndex, lastPageIndex, 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 (withPrintDialog)
		{
			if (printJob.printDialog())
			{
				printJob.print();
			}
			else
			{
				isOK = false;
			}
		}
		else
		{
			printJob.print();
		}
	}
	catch (Exception ex)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_ERROR_PRINTING_REPORT,
				null, 
				ex);
	}

	return isOK;
}
 
Example 8
Source File: JRPrinterAWT.java    From nordpos with GNU General Public License v3.0 4 votes vote down vote up
/**
 *
 */
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(
				EXCEPTION_MESSAGE_KEY_INVALID_PAGE_RANGE,  
				new Object[]{firstPageIndex, lastPageIndex, 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(
				EXCEPTION_MESSAGE_KEY_ERROR_PRINTING_REPORT,
				null, 
				ex);
	}

	return isOK;
}
 
Example 9
Source File: Print.java    From wandora with GNU General Public License v3.0 4 votes vote down vote up
public void printImage(final BufferedImage image) {
    if(image != null) {

        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setJobName("Wandora");

        pj.setPrintable(
                new Printable() {
                    @Override
                    public int print(Graphics pg, PageFormat pageFormat, int pageNum) {
                        if(pageNum > 0) {
                            return Printable.NO_SUCH_PAGE;
                        }

                        Graphics2D g2 = (Graphics2D) pg;

                        double scaleX = pageFormat.getImageableWidth() / image.getWidth();
                        double scaleY = pageFormat.getImageableHeight() / image.getHeight();
                        // Maintain aspect ratio, 2 as a maximum
                        double scale = Math.min(2, Math.min(scaleX, scaleY));
                        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

                        g2.scale(scale, scale);
                        g2.drawImage(image, 0, 0, null);

                        return Printable.PAGE_EXISTS;
                    }
                }
        );
        if (pj.printDialog() == false) {
            return;
        }

        try {
            pj.print();
        } 
        catch (PrinterException ex) {
            log(ex);
        }
    }
}