Search in sources :

Example 16 with PrinterJob

use of java.awt.print.PrinterJob in project jgnash by ccavanaugh.

the class JasperViewerDialogController method handleFormatAction.

@FXML
private void handleFormatAction() {
    final PageFormat oldFormat = report.get().getPageFormat();
    final PrinterJob job = PrinterJob.getPrinterJob();
    final PageFormat format = job.pageDialog(oldFormat);
    if (format != oldFormat) {
        report.get().setPageFormat(format);
        createJasperPrint(report.get());
        Platform.runLater(this::refresh);
    }
}
Also used : PrintPageFormat(net.sf.jasperreports.engine.PrintPageFormat) PageFormat(java.awt.print.PageFormat) PrinterJob(java.awt.print.PrinterJob) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Example 17 with PrinterJob

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

the class PrintHTML method printLegacy.

private void printLegacy(PrintOutput output, PrintOptions options) throws PrinterException {
    PrintOptions.Pixel pxlOpts = options.getPixelOptions();
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(output.getPrintService());
    PageFormat page = job.getPageFormat(null);
    PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, page);
    // setup swing ui
    JFrame legacyFrame = new JFrame(pxlOpts.getJobName(Constants.HTML_PRINT));
    legacyFrame.setUndecorated(true);
    legacyFrame.setLayout(new FlowLayout());
    legacyFrame.setExtendedState(Frame.ICONIFIED);
    legacyLabel = new JLabel();
    legacyLabel.setOpaque(true);
    legacyLabel.setBackground(Color.WHITE);
    legacyLabel.setBorder(null);
    legacyLabel.setDoubleBuffered(false);
    legacyFrame.add(legacyLabel);
    try {
        for (WebAppModel model : models) {
            if (model.isPlainText()) {
                legacyLabel.setText(model.getSource());
            } else {
                try (InputStream fis = new URL(model.getSource()).openStream()) {
                    String webPage = IOUtils.toString(fis, "UTF-8").replaceAll("^[\\s\\S]+<(HTML|html)\\b.*?>", "<html>");
                    legacyLabel.setText(webPage);
                }
            }
            legacyFrame.pack();
            legacyFrame.setVisible(true);
            job.setPrintable(this);
            printCopies(output, pxlOpts, job, attributes);
        }
    } catch (Exception e) {
        throw new PrinterException(e.getMessage());
    } finally {
        legacyFrame.dispose();
    }
}
Also used : InputStream(java.io.InputStream) PrinterException(java.awt.print.PrinterException) URL(java.net.URL) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) PrinterException(java.awt.print.PrinterException) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) PageFormat(java.awt.print.PageFormat) PrintOptions(qz.printer.PrintOptions)

Example 18 with PrinterJob

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

the class PrintImage method print.

@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
    if (images.isEmpty()) {
        log.warn("Nothing to print");
        return;
    }
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(output.getPrintService());
    PageFormat page = job.getPageFormat(null);
    PrintOptions.Pixel pxlOpts = options.getPixelOptions();
    PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, page);
    scaleImage = pxlOpts.isScaleContent();
    interpolation = pxlOpts.getInterpolation();
    imageRotation = pxlOpts.getRotation();
    // reverse fix for OSX
    if (SystemUtilities.isMac() && pxlOpts.getOrientation() != null && pxlOpts.getOrientation().getAsAttribute() == OrientationRequested.REVERSE_LANDSCAPE) {
        imageRotation += 180;
        manualReverse = true;
    }
    job.setJobName(pxlOpts.getJobName(Constants.IMAGE_PRINT));
    job.setPrintable(this, job.validatePage(page));
    printCopies(output, pxlOpts, job, attributes);
}
Also used : PageFormat(java.awt.print.PageFormat) PrintOptions(qz.printer.PrintOptions) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Example 19 with PrinterJob

use of java.awt.print.PrinterJob in project pdfbox by apache.

the class PrintPDF method main.

/**
 * Infamous main method.
 *
 * @param args Command line arguments, should be one and a reference to a file.
 * @throws PrinterException if the specified service cannot support the Pageable and Printable interfaces.
 * @throws IOException if there is an error parsing the file.
 */
public static void main(String[] args) throws PrinterException, IOException {
    try {
        // force KCMS (faster than LCMS) if available
        Class.forName("sun.java2d.cmm.kcms.KcmsServiceProvider");
        System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
    } catch (ClassNotFoundException e) {
        LOG.debug("KCMS service not found - using LCMS", e);
    }
    // suppress the Dock icon on OS X
    System.setProperty("apple.awt.UIElement", "true");
    String password = "";
    String pdfFile = null;
    boolean silentPrint = false;
    String printerName = null;
    Orientation orientation = Orientation.AUTO;
    boolean showPageBorder = false;
    int dpi = 0;
    Map<String, Orientation> orientationMap = new HashMap<>();
    orientationMap.put("auto", Orientation.AUTO);
    orientationMap.put("landscape", Orientation.LANDSCAPE);
    orientationMap.put("portrait", Orientation.PORTRAIT);
    for (int i = 0; i < args.length; i++) {
        switch(args[i]) {
            case PASSWORD:
                i++;
                if (i >= args.length) {
                    usage();
                }
                password = args[i];
                break;
            case PRINTER_NAME:
                i++;
                if (i >= args.length) {
                    usage();
                }
                printerName = args[i];
                break;
            case SILENT:
                silentPrint = true;
                break;
            case ORIENTATION:
                i++;
                if (i >= args.length) {
                    usage();
                }
                orientation = orientationMap.get(args[i]);
                if (orientation == null) {
                    usage();
                }
                break;
            case BORDER:
                showPageBorder = true;
                break;
            case DPI:
                i++;
                if (i >= args.length) {
                    usage();
                }
                dpi = Integer.parseInt(args[i]);
                break;
            default:
                pdfFile = args[i];
                break;
        }
    }
    if (pdfFile == null) {
        usage();
    }
    try (PDDocument document = PDDocument.load(new File(pdfFile), password)) {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setJobName(new File(pdfFile).getName());
        if (printerName != null) {
            PrintService[] printService = PrinterJob.lookupPrintServices();
            boolean printerFound = false;
            for (int i = 0; !printerFound && i < printService.length; i++) {
                if (printService[i].getName().contains(printerName)) {
                    printJob.setPrintService(printService[i]);
                    printerFound = true;
                }
            }
        }
        printJob.setPageable(new PDFPageable(document, orientation, showPageBorder, dpi));
        if (silentPrint || printJob.printDialog()) {
            printJob.print();
        }
    }
}
Also used : HashMap(java.util.HashMap) Orientation(org.apache.pdfbox.printing.Orientation) PrinterJob(java.awt.print.PrinterJob) PrintService(javax.print.PrintService) PDFPageable(org.apache.pdfbox.printing.PDFPageable) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) File(java.io.File)

Example 20 with PrinterJob

use of java.awt.print.PrinterJob in project pdfbox by apache.

the class Printing method printWithDialogAndAttributes.

/**
 * Prints with a print preview dialog and custom PrintRequestAttribute values.
 */
private static void printWithDialogAndAttributes(PDDocument document) throws IOException, PrinterException {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(new PDFPageable(document));
    PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
    // pages 1 to 1
    attr.add(new PageRanges(1, 1));
    PDViewerPreferences vp = document.getDocumentCatalog().getViewerPreferences();
    if (vp != null && vp.getDuplex() != null) {
        String dp = vp.getDuplex();
        if (PDViewerPreferences.DUPLEX.DuplexFlipLongEdge.toString().equals(dp)) {
            attr.add(Sides.TWO_SIDED_LONG_EDGE);
        } else if (PDViewerPreferences.DUPLEX.DuplexFlipShortEdge.toString().equals(dp)) {
            attr.add(Sides.TWO_SIDED_SHORT_EDGE);
        } else if (PDViewerPreferences.DUPLEX.Simplex.toString().equals(dp)) {
            attr.add(Sides.ONE_SIDED);
        }
    }
    if (job.printDialog(attr)) {
        job.print(attr);
    }
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) PageRanges(javax.print.attribute.standard.PageRanges) PDViewerPreferences(org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

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