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

The following examples show how to use java.awt.print.PrinterJob#defaultPage() . 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: PrintUtils.java    From ApprovalTests.Java with Apache License 2.0 7 votes vote down vote up
public void print(boolean prompt)
{
  PrinterJob printJob = PrinterJob.getPrinterJob();
  PageFormat format = printJob.defaultPage();
  Paper paper = format.getPaper();
  paper.setImageableArea(18, 0, 180, 840);// Paper format for receipt printer
  format.setPaper(paper);
  printJob.setPrintable(this, format);
  if (!prompt || printJob.printDialog())
  {
    try
    {
      printJob.print();
    }
    catch (PrinterException pe)
    {
      SimpleLogger.variable("Error printing: " + pe);
    }
  }
}
 
Example 2
Source File: ChartPanel.java    From buffer_bci with GNU General Public License v3.0 7 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }
}
 
Example 3
Source File: PrintPreferences.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Get an instance of {@link java.awt.print.PageFormat}.
 * @param pj {@link java.awt.print.PrinterJob} which is 
 * associated with the default printer.
 * @return an instance of <code>PageFormat</code> that describes the size and
 * orientation of a page to be printed.
 */
public static PageFormat getPageFormat(PrinterJob pj) {
    PageFormat pageFormat = null;
    pageFormat = pj.defaultPage();
    Paper p = pageFormat.getPaper();
    int pageOrientation = getPreferences().getInt(PROP_PAGE_ORIENTATION, pageFormat.getOrientation());
    double paperWidth = getPreferences().getDouble(PROP_PAGE_WIDTH, p.getWidth());
    double paperHeight = getPreferences().getDouble(PROP_PAGE_HEIGHT, p.getHeight());
    
    double iaWidth = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_WIDTH, p.getImageableWidth());
    double iaHeight = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_HEIGHT, p.getImageableHeight());
    double iaX = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_X, p.getImageableX());
    double iaY = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_Y, p.getImageableY());
    
    pageFormat.setOrientation(pageOrientation);
    p.setSize(paperWidth, paperHeight);
    p.setImageableArea(iaX, iaY, iaWidth, iaHeight);
    pageFormat.setPaper(p);
    return pageFormat;
}
 
Example 4
Source File: ChartPanel.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart. Prints to printer
 */
public void createChartPrintJob() {

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

}
 
Example 5
Source File: ChartPanel.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {

    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }

}
 
Example 6
Source File: ChartPanel.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {

    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }

}
 
Example 7
Source File: ChartComposite.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    //FIXME try to replace swing print stuff by swt
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            }
            catch (PrinterException e) {
                MessageBox messageBox = new MessageBox(
                        this.canvas.getShell(), SWT.OK | SWT.ICON_ERROR);
                messageBox.setMessage(e.getMessage());
                messageBox.open();
            }
        }
    }
}
 
Example 8
Source File: ChartPanel.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }
}
 
Example 9
Source File: AbstractChartPanel.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */

@Override
public void createChartPrintJob() {

	PrinterJob job = PrinterJob.getPrinterJob();
	PageFormat pf = job.defaultPage();
	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);
			}
		}
	}

}
 
Example 10
Source File: ChartPanel.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }
}
 
Example 11
Source File: PrintTest.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
private void printTextAction()
{
    printStr = area.getText().trim();
    if(printStr != null && printStr.length() > 0)
    {
        PAGES = getPagesCount(printStr);
        PrinterJob myPrtJob = PrinterJob.getPrinterJob();
        PageFormat pageFormat = myPrtJob.defaultPage();
        myPrtJob.setPrintable(this, pageFormat);
        if(myPrtJob.printDialog())
            try
            {
                myPrtJob.print();
            }
            catch(PrinterException pe)
            {
                pe.printStackTrace();
            }
    } else
    {
        JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty", -1, 2);
    }
}
 
Example 12
Source File: ChartPanel.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }
}
 
Example 13
Source File: ChartPanel.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }
}
 
Example 14
Source File: ChartPanel.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    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);
            }
        }
    }
}
 
Example 15
Source File: PageDlgPrnButton.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void pageDialogExample() throws PrinterException
{
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat originalPageFormat = job.defaultPage();
    PageFormat pageFormat = job.pageDialog(originalPageFormat);

    if(originalPageFormat == pageFormat) return;

    job.setPrintable(this,pageFormat);
    job.print();
}
 
Example 16
Source File: PolylinePrintingTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public PolylinePrintingTest() throws PrinterException {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    Paper p = pf.getPaper();
    p.setImageableArea(0,0,p.getWidth(), p.getHeight());
    pf.setPaper(p);
    job.setPrintable(this, pf);
    if (job.printDialog()) {
        job.print();
    }
}
 
Example 17
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 18
Source File: CGraphPrinter.java    From binnavi with Apache License 2.0 4 votes vote down vote up
/**
 * Prints a graph. The user is given the opportunity to choose a number of options from a dialog
 * before.
 *
 * @param parent Parent window of created dialogs.
 * @param graph The graph to print.
 */
public static void print(final JFrame parent, final ZyGraph graph) {
  final String[] area = {"Print the visible part of the graph only", "Print the whole graph"};

  final OptionHandler printOptions = new OptionHandler("Print Options");

  printOptions.addInt("Poster rows", 1);
  printOptions.addInt("Poster columns", 1);
  printOptions.addBool("Add poster coordinates", false);
  printOptions.addEnum("Print Area", area, 1);

  final Graph2DPrinter gprinter = new Graph2DPrinter(graph.getView());

  // show custom print dialog and adopt values
  if (!printOptions.showEditor()) {
    return;
  }

  gprinter.setPosterRows(printOptions.getInt("Poster rows"));
  gprinter.setPosterColumns(printOptions.getInt("Poster columns"));
  gprinter.setPrintPosterCoords(printOptions.getBool("Add poster coordinates"));

  if (printOptions.get("Print Area").equals("Print the whole graph")) {
    gprinter.setClipType(Graph2DPrinter.CLIP_GRAPH);
  } else {
    gprinter.setClipType(Graph2DPrinter.CLIP_VIEW);
  }

  // show default print dialogs
  final PrinterJob printJob = PrinterJob.getPrinterJob();
  PageFormat pageFormat = printJob.defaultPage();
  final PageFormat pageFormat2 = printJob.pageDialog(pageFormat);

  if (pageFormat2 == pageFormat) {
    return;
  }

  pageFormat = pageFormat2;

  // setup printjob.
  // Graph2DPrinter is of type Printable
  printJob.setPrintable(gprinter, pageFormat);

  if (printJob.printDialog()) {
    try {
      printJob.print();
    } catch (final PrinterException exception) {
      final String innerMessage = "E00119: " + "Graph could not be printed";
      final String innerDescription = CUtilityFunctions.createDescription(String.format(
          "The graph '%s' could not be printed because there was a problem with the printer.",
          graph.getRawView().getName()), new String[] {"There was a problem with the printer."},
          new String[] {"The print operation could not be completed."});

      NaviErrorDialog.show(parent, innerMessage, innerDescription, exception);
    }
  }
}
 
Example 19
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 20
Source File: PrintPageDirectNew.java    From haxademic with MIT License 2 votes vote down vote up
public void sendToDefaultPrinter() {
	// configure the printing job & simple single-page document
	PrinterJob printJob = PrinterJob.getPrinterJob();
	
	
	
	PageFormat pageFormat = printJob.defaultPage();
	Paper paper = pageFormat.getPaper();
	
	float inchWidth = 16;
	float inchHeight = 20;
	
	float marginWidth = 100;
	float marginHeight = 100;
	
	int pWidth = (int) ((inchWidth * 25.4f) * 2.835f);// quick: convert inch to mm, mm to pt
	int pHeight = (int) ((inchHeight * 25.4f) * 2.835f);
	
	paper.setSize(pWidth, pHeight);
	paper.setImageableArea(marginWidth, marginHeight, pWidth, pHeight);
	pageFormat.setPaper(paper);
	
	//not sure this exactly works, we should probably just scale the square to fit and we send it the correct rotation orientation
	//pageFormat.setOrientation(PageFormat.LANDSCAPE);
	

	// we might also be able to set attributes here
	Printable page = new ImagePage();
	Book book = new Book();
	
	book.append(page, pageFormat);
	
	
	/*
	Early testing at "LikeMinded Productions":
	
	PageFormat documentPageFormat = new PageFormat();
	
	// new settings to play with
	Paper pape = new Paper();
	pape.setSize(10000, 6000);
	pape.setImageableArea(0, 0, 10000, 6000);
	documentPageFormat.setPaper(pape);
	
	documentPageFormat.setOrientation(PageFormat.LANDSCAPE);
	*/
	
	
	printJob.setPageable(book);

	// use the print dialog UI or just print it!
	if(useDialog) {
		if (printJob.printDialog()) printPage(printJob);
	} else {
		printPage(printJob);
	}
}