Search in sources :

Example 46 with PrinterJob

use of java.awt.print.PrinterJob in project blue by kunstmusik.

the class PrintPreview method createComponents.

protected void createComponents() {
    try {
        PrinterJob prnJob = PrinterJob.getPrinterJob();
        PageFormat pageFormat = prnJob.defaultPage();
        if (pageFormat.getHeight() == 0 || pageFormat.getWidth() == 0) {
            System.err.println("Unable to determine default page size");
            return;
        }
        m_wPage = (int) (pageFormat.getWidth());
        m_hPage = (int) (pageFormat.getHeight());
        m_scale = 10;
        int w = (m_wPage * m_scale / 100);
        int h = (m_hPage * m_scale / 100);
        int pageIndex = 0;
        while (true) {
            BufferedImage img = new BufferedImage(m_wPage, m_hPage, BufferedImage.TYPE_INT_RGB);
            Graphics g = img.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, m_wPage, m_hPage);
            if (m_target.print(g, pageFormat, pageIndex) != Printable.PAGE_EXISTS) {
                break;
            }
            PagePreview pp = new PagePreview(w, h, img);
            m_preview.add(pp);
            pageIndex++;
        }
        repaint();
    } catch (PrinterException e) {
        e.printStackTrace();
    }
}
Also used : PageFormat(java.awt.print.PageFormat) PrinterException(java.awt.print.PrinterException) BufferedImage(java.awt.image.BufferedImage) PrinterJob(java.awt.print.PrinterJob)

Example 47 with PrinterJob

use of java.awt.print.PrinterJob in project cytoscape-impl by cytoscape.

the class PrintAction method actionPerformed.

@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
    final RenderingEngine<CyNetwork> engine = serviceRegistrar.getService(CyApplicationManager.class).getCurrentRenderingEngine();
    final PrinterJob printJob = PrinterJob.getPrinterJob();
    final CyProperty<Properties> cyProps = serviceRegistrar.getService(CyProperty.class, "(cyPropertyName=cytoscape3.props)");
    final Properties props = cyProps.getProperties();
    // TODO: pick only required props
    final Set<Object> keys = props.keySet();
    for (Object key : keys) engine.getProperties().put(key, props.get(key));
    printJob.setPrintable(engine.createPrintable());
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
}
Also used : CyApplicationManager(org.cytoscape.application.CyApplicationManager) CyNetwork(org.cytoscape.model.CyNetwork) Properties(java.util.Properties) PrinterJob(java.awt.print.PrinterJob)

Example 48 with PrinterJob

use of java.awt.print.PrinterJob in project tray by qzind.

the class PrintPDF method print.

@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
    if (printables.isEmpty()) {
        log.warn("Nothing to print");
        return;
    }
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(output.getPrintService());
    PrintOptions.Pixel pxlOpts = options.getPixelOptions();
    PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, job.getPageFormat(null));
    // Disable attributes per https://github.com/qzind/tray/issues/174
    if (SystemUtilities.isMac() && Constants.JAVA_VERSION.lessThan(Version.valueOf("1.8.0-162"))) {
        log.warn("MacOS and Java < 8u162 cannot use attributes with PDF prints, disabling");
        attributes.clear();
    }
    Scaling scale = (pxlOpts.isScaleContent() ? Scaling.SCALE_TO_FIT : Scaling.ACTUAL_SIZE);
    BookBundle bundle = new BookBundle();
    for (PDDocument doc : printables) {
        PageFormat page = job.getPageFormat(null);
        applyDefaultSettings(pxlOpts, page);
        for (PDPage pd : doc.getPages()) {
            if (pxlOpts.getRotation() % 360 != 0) {
                rotatePage(doc, pd, pxlOpts.getRotation());
            }
            if (pxlOpts.getOrientation() == null) {
                PDRectangle bounds = pd.getBBox();
                if (bounds.getWidth() > bounds.getHeight() || (pd.getRotation() / 90) % 2 == 1) {
                    log.info("Adjusting orientation to print landscape PDF source");
                    page.setOrientation(PrintOptions.Orientation.LANDSCAPE.getAsFormat());
                }
            } else if (pxlOpts.getOrientation() != PrintOptions.Orientation.PORTRAIT) {
                // flip imageable area dimensions when in landscape
                Paper repap = page.getPaper();
                repap.setImageableArea(repap.getImageableX(), repap.getImageableY(), repap.getImageableHeight(), repap.getImageableWidth());
                page.setPaper(repap);
                // reverse fix for OSX
                if (SystemUtilities.isMac() && pxlOpts.getOrientation() == PrintOptions.Orientation.REVERSE_LANDSCAPE) {
                    pd.setRotation(pd.getRotation() + 180);
                }
            }
        }
        bundle.append(new PDFWrapper(doc, scale, false, (float) (pxlOpts.getDensity() * pxlOpts.getUnits().as1Inch()), false, pxlOpts.getOrientation()), page, doc.getNumberOfPages());
    }
    job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT));
    job.setPageable(bundle.wrapAndPresent());
    printCopies(output, pxlOpts, job, attributes);
}
Also used : PageFormat(java.awt.print.PageFormat) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDFWrapper(qz.printer.PDFWrapper) BookBundle(qz.printer.BookBundle) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PrintOptions(qz.printer.PrintOptions) Scaling(org.apache.pdfbox.printing.Scaling) Paper(java.awt.print.Paper) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Example 49 with PrinterJob

use of java.awt.print.PrinterJob in project Zong by Xenoage.

the class PrintProcess method requestPrint.

/**
 * Starts a print process.
 *
 * First a print dialog is shown, where the user
 * can select a printer, the range of pages and
 * the number of copies.
 * The user can cancel the process here.
 *
 * If he continues, the selected pages are printed.
 * Thus this method needs a PageLayout instance.
 */
public void requestPrint(Layout layout) {
    this.layout = layout;
    // show print dialog
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName("Score Document");
    pj.setPrintable(this);
    pj.setPageable(this);
    try {
        INSTANCE.log(Companion.remark("Opening print dialog..."));
        pj.setCopies(1);
        if (pj.printDialog()) {
            INSTANCE.log(Companion.remark("Starting print..."));
            pj.print();
        }
    } catch (PrinterException ex) {
        handle(error(Voc.ErrorWhilePrinting, ex));
    }
}
Also used : PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Example 50 with PrinterJob

use of java.awt.print.PrinterJob in project energy3d by concord-consortium.

the class PrintController method print.

private void print(final int pageNum, final Printout printout, final double x, final double y, final double w, final double h) {
    final PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(printout);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (final PrinterException exc) {
            exc.printStackTrace();
        }
    }
}
Also used : PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob)

Aggregations

PrinterJob (java.awt.print.PrinterJob)58 PrinterException (java.awt.print.PrinterException)29 PageFormat (java.awt.print.PageFormat)22 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)19 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)17 PDFPageable (org.apache.pdfbox.printing.PDFPageable)10 IOException (java.io.IOException)8 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