javafx.print.PrinterJob Java Examples

The following examples show how to use javafx.print.PrinterJob. 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: PrintAction.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
private void print(final Node node)
{
    try
    {
        // Select printer
        final PrinterJob job = Objects.requireNonNull(PrinterJob.createPrinterJob(), "Cannot create printer job");
        final Scene scene = Objects.requireNonNull(node.getScene(), "Missing Scene");

        if (! job.showPrintDialog(scene.getWindow()))
            return;

        // Get Screenshot
        final WritableImage screenshot = node.snapshot(null, null);

        // Scale image to full page
        final Printer printer = job.getPrinter();
        final Paper paper = job.getJobSettings().getPageLayout().getPaper();
        final PageLayout pageLayout = printer.createPageLayout(paper,
                                                               PageOrientation.LANDSCAPE,
                                                               Printer.MarginType.DEFAULT);
        final double scaleX = pageLayout.getPrintableWidth() / screenshot.getWidth();
        final double scaleY = pageLayout.getPrintableHeight() / screenshot.getHeight();
        final double scale = Math.min(scaleX, scaleY);
        final ImageView print_node = new ImageView(screenshot);
        print_node.getTransforms().add(new Scale(scale, scale));

        // Print off the UI thread
        JobManager.schedule(Messages.Print, monitor ->
        {
            if (job.printPage(print_node))
                job.endJob();
        });
    }
    catch (Exception ex)
    {
        ExceptionDetailsErrorDialog.openError(node, Messages.Print, Messages.PrintErr, ex);
    }
}
 
Example #2
Source File: JFXPrinterChooser.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void choose(UIPrinterChooserHandler selectionHandler) {
	JFXPrinter jfxPrinter = null;
	PrinterJob printerJob = PrinterJob.createPrinterJob();
	if( printerJob != null && printerJob.showPrintDialog(this.window.getStage()) ) {
		jfxPrinter = new JFXPrinter(printerJob);
	}
	selectionHandler.onSelectPrinter(jfxPrinter);
}
 
Example #3
Source File: DesktopController.java    From CPUSim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * opens the page setup dialog to set the printer options for the current job
 *
 * @param event unused
 */
@FXML
protected void handlePrintSetup(ActionEvent event) {

    if (currentPrinterJob == null) {
        currentPrinterJob = PrinterJob.createPrinterJob();
    }
    currentPrinterJob.showPageSetupDialog(stage);
}
 
Example #4
Source File: JFXPrinter.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
public JFXPrinter(PrinterJob control) {
	super(control);
	
	this.resourceFactory = new JFXResourceFactory();
}
 
Example #5
Source File: DesktopController.java    From CPUSim with GNU General Public License v3.0 4 votes vote down vote up
/**
 * prints all the code from the selected tab of assembly programs
 *
 * @param event the ignored Event (selection of Print from File menu)
 */
@FXML
protected void handlePrint(ActionEvent event) {
    // the current job may have been set in the page setup dialog
    if (currentPrinterJob == null) {
        currentPrinterJob = PrinterJob.createPrinterJob();
    }
    boolean print = currentPrinterJob.showPrintDialog(stage);
    if (print) {
        Node nodeToBePrinted = textTabPane.getSelectionModel().getSelectedItem()
                .getContent();

        // break the node into pages and print them
        final List<Node> pages = getPagesForPrinting(
                (InlineStyleTextArea<StyleInfo>) nodeToBePrinted);
        PageRange[] ranges = currentPrinterJob.getJobSettings().getPageRanges();
        if (ranges != null && ranges.length > 0) {
            for (PageRange range : ranges) {
                for (int i = range.getStartPage(); i <= range.getEndPage(); i++)
                    currentPrinterJob.printPage(pages.get(i));
            }
        }
        else {
            pages.forEach(currentPrinterJob::printPage);
        }
        currentPrinterJob.endJob();
        currentPrinterJob = null;
    }

    /*
    OLD VERSION that just prints one page.
    if( currentPrinterJob == null)
        currentPrinterJob = PrinterJob.createPrinterJob();
    if (currentPrinterJob != null) {
        // show the print dialog
        boolean ok = currentPrinterJob.showPrintDialog(stage);
        // if it wasn't cancelled then print the current text area
        if (ok) {
            currentPrinterJob.getJobSettings().setPageRanges(new PageRange(1, 5));
            Node nodeToBePrinted =
                    textTabPane.getSelectionModel().getSelectedItem().getContent();

            // Now do the actual printing
            boolean success = currentPrinterJob.printPage(nodeToBePrinted);
            if (success ) {
                currentPrinterJob.endJob();
            }
        }
    }
    currentPrinterJob = null;
    */

    /*
        // Sample code that scales the node to be printed, based on the standard
        //                letter size in portrait mode.
        Printer printer = Printer.getDefaultPrinter();
        PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER,
                               PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
        double scaleX = pageLayout.getPrintableWidth() /
                        node.getBoundsInParent().getWidth();
        double scaleY = pageLayout.getPrintableHeight() /
                        node.getBoundsInParent().getHeight();
        node.getTransforms().add(new Scale(scaleX, scaleY));
   */
}