Search in sources :

Example 26 with PrinterJob

use of java.awt.print.PrinterJob in project freeplane by freeplane.

the class PrintController method print.

public void print(Printable mapView, boolean showDlg) throws PrinterException {
    if (!acquirePrinterJobAndPageFormat(showDlg)) {
        return;
    }
    getPrinterJob().setPrintable(mapView, getPageFormat());
    if (!showDlg || printDialog()) {
        if (mapView instanceof MapView)
            ((MapView) mapView).preparePrinting();
        try {
            final PrinterJob printerJob = getPrinterJob();
            if (mapView instanceof Component) {
                final String name = ((Component) mapView).getName();
                if (name != null)
                    printerJob.setJobName(name);
            }
            printerJob.print();
        } catch (PrinterException ex) {
            LogUtils.warn(ex);
        } finally {
            if (mapView instanceof MapView)
                ((MapView) mapView).endPrinting();
        }
    }
}
Also used : MapView(org.freeplane.view.swing.map.MapView) PrinterException(java.awt.print.PrinterException) Component(java.awt.Component) PrinterJob(java.awt.print.PrinterJob)

Example 27 with PrinterJob

use of java.awt.print.PrinterJob in project com.revolsys.open by revolsys.

the class Print method actionPerformed.

@Override
public void actionPerformed(final ActionEvent event) {
    final Project project = Project.get();
    final Viewport2D viewport = project.getViewport();
    final PrinterJob job = PrinterJob.getPrinterJob();
    final PageFormat format = job.defaultPage();
    format.setOrientation(PageFormat.PORTRAIT);
    final Paper paper = format.getPaper();
    paper.setImageableArea(29, 29, format.getWidth() - 58, format.getHeight() - 58);
    format.setPaper(paper);
    if (this.printService != null) {
        try {
            job.setPrintService(this.printService);
        } catch (final PrinterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    final BoundingBox boundingBox = viewport.getBoundingBox();
    final MapPageable pageable = new MapPageable(project, boundingBox, format, 20000, 300, 200);
    job.setPageable(pageable);
    final boolean doPrint = job.printDialog();
    if (doPrint) {
        this.printService = job.getPrintService();
        Invoke.background("Print", () -> {
            try {
                job.print();
            } catch (final Exception e) {
                Logs.error(this, "Unable to print", e);
            }
        });
    }
}
Also used : Project(com.revolsys.swing.map.layer.Project) Viewport2D(com.revolsys.swing.map.Viewport2D) PageFormat(java.awt.print.PageFormat) BoundingBox(com.revolsys.geometry.model.BoundingBox) Paper(java.awt.print.Paper) PrinterException(java.awt.print.PrinterException) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Example 28 with PrinterJob

use of java.awt.print.PrinterJob in project com.revolsys.open by revolsys.

the class SinglePage method print.

public static void print() {
    final Project project = Project.get();
    final Viewport2D viewport = project.getViewport();
    final int viewWidth = viewport.getViewWidthPixels();
    final int viewHeight = viewport.getViewHeightPixels();
    final BoundingBox boundingBox = viewport.getBoundingBox();
    final double scaleForVisible = viewport.getScaleForVisible();
    final PrinterJob job = PrinterJob.getPrinterJob();
    job.setJobName(project.getName());
    final PageFormat format = job.defaultPage();
    final PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
    if (boundingBox.getAspectRatio() > 1) {
        format.setOrientation(PageFormat.LANDSCAPE);
    // printAttributes.add(OrientationRequested.LANDSCAPE);
    } else {
        format.setOrientation(PageFormat.PORTRAIT);
    // printAttributes.add(OrientationRequested.PORTRAIT);
    }
    final SinglePage pageable = new SinglePage(project, boundingBox, viewWidth, viewHeight, scaleForVisible);
    job.setPageable(pageable);
    final boolean doPrint = job.printDialog();
    if (doPrint) {
        Invoke.background("Print", () -> {
            try {
                job.print();
            } catch (final PrinterAbortException e) {
            } catch (final Exception e) {
                Logs.error(SinglePage.class, "Unable to print", e);
            }
        });
    }
}
Also used : PrinterAbortException(java.awt.print.PrinterAbortException) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) Project(com.revolsys.swing.map.layer.Project) Viewport2D(com.revolsys.swing.map.Viewport2D) GraphicsViewport2D(com.revolsys.swing.map.GraphicsViewport2D) PageFormat(java.awt.print.PageFormat) PrinterAbortException(java.awt.print.PrinterAbortException) BoundingBox(com.revolsys.geometry.model.BoundingBox) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 29 with PrinterJob

use of java.awt.print.PrinterJob in project OpenNotebook by jaltekruse.

the class NotebookPanel method print.

public void print() {
    DocPrinter docPrinter = new DocPrinter();
    docPrinter.setDoc(getCurrentDocViewer().getDoc());
    PrinterJob job = PrinterJob.getPrinterJob();
    Paper paper = new Paper();
    Document doc = getCurrentDocViewer().getDoc();
    paper.setImageableArea(doc.getxMargin(), doc.getyMargin(), doc.getWidth() - 2 * doc.getxMargin(), doc.getHeight() - 2 * doc.getyMargin());
    paper.setSize(doc.getWidth(), doc.getHeight());
    PageFormat pf = job.defaultPage();
    pf.setPaper(paper);
    job.setPrintable(docPrinter, pf);
    job.setJobName(getCurrentDocViewer().getDoc().getName());
    boolean ok = job.printDialog();
    if (ok) {
        try {
            job.print();
        } catch (PrinterException ex) {
        /* The job did not successfully complete */
        }
    }
}
Also used : PageFormat(java.awt.print.PageFormat) Paper(java.awt.print.Paper) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Example 30 with PrinterJob

use of java.awt.print.PrinterJob in project binnavi by google.

the class CGraphPrinter method print.

/**
 * 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);
        }
    }
}
Also used : PageFormat(java.awt.print.PageFormat) Graph2DPrinter(y.view.Graph2DPrinter) PrinterException(java.awt.print.PrinterException) OptionHandler(y.option.OptionHandler) PrinterJob(java.awt.print.PrinterJob)

Aggregations

PrinterJob (java.awt.print.PrinterJob)57 PrinterException (java.awt.print.PrinterException)28 PageFormat (java.awt.print.PageFormat)22 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)19 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)17 PDFPageable (org.apache.pdfbox.printing.PDFPageable)9 IOException (java.io.IOException)7 Paper (java.awt.print.Paper)6 Printable (java.awt.print.Printable)5 File (java.io.File)5 Copies (javax.print.attribute.standard.Copies)5 ActionEvent (java.awt.event.ActionEvent)4 PrintService (javax.print.PrintService)4 Container (java.awt.Container)3 Book (java.awt.print.Book)3 JobName (javax.print.attribute.standard.JobName)3 PageRanges (javax.print.attribute.standard.PageRanges)3 AbstractAction (javax.swing.AbstractAction)3 JButton (javax.swing.JButton)3 JFrame (javax.swing.JFrame)3