Search in sources :

Example 41 with PrintRequestAttributeSet

use of javax.print.attribute.PrintRequestAttributeSet in project tray by qzind.

the class PrintPixel method applyDefaultSettings.

protected PrintRequestAttributeSet applyDefaultSettings(PrintOptions.Pixel pxlOpts, PageFormat page, Media[] supported) {
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    // apply general attributes
    if (pxlOpts.getColorType() != null) {
        attributes.add(pxlOpts.getColorType().getAsChromaticity());
    }
    attributes.add(pxlOpts.getDuplex());
    if (pxlOpts.getOrientation() != null) {
        attributes.add(pxlOpts.getOrientation().getAsOrientRequested());
    }
    if (pxlOpts.getPrinterTray() != null && !pxlOpts.getPrinterTray().isEmpty()) {
        Media tray = findMediaTray(supported, pxlOpts.getPrinterTray());
        if (tray != null) {
            attributes.add(tray);
        }
    }
    // TODO - set paper thickness
    // Java prints using inches at 72dpi
    final float CONVERT = pxlOpts.getUnits().toInches() * 72f;
    log.trace("DPI: [{}x{}]\tCNV: {}", pxlOpts.getDensity(), pxlOpts.getCrossDensity(), CONVERT);
    if (pxlOpts.getDensity() > 0) {
        double cross = pxlOpts.getCrossDensity();
        if (cross == 0) {
            cross = pxlOpts.getDensity();
        }
        attributes.add(new PrinterResolution((int) cross, (int) pxlOpts.getDensity(), pxlOpts.getUnits().getDPIUnits()));
    }
    // apply sizing and margins
    Paper paper = page.getPaper();
    float pageX = 0f;
    float pageY = 0f;
    float pageW = (float) page.getWidth() / CONVERT;
    float pageH = (float) page.getHeight() / CONVERT;
    // page size
    if (pxlOpts.getSize() != null && pxlOpts.getSize().getWidth() > 0 && pxlOpts.getSize().getHeight() > 0) {
        pageW = (float) pxlOpts.getSize().getWidth();
        pageH = (float) pxlOpts.getSize().getHeight();
        paper.setSize(pageW * CONVERT, pageH * CONVERT);
    }
    // margins
    if (pxlOpts.getMargins() != null) {
        pageX += pxlOpts.getMargins().left();
        pageY += pxlOpts.getMargins().top();
        pageW -= (pxlOpts.getMargins().right() + pxlOpts.getMargins().left());
        pageH -= (pxlOpts.getMargins().bottom() + pxlOpts.getMargins().top());
    }
    log.trace("Drawable area: {},{}:{},{}", pageX, pageY, pageW, pageH);
    if (pageW > 0 && pageH > 0) {
        attributes.add(new MediaPrintableArea(pageX, pageY, pageW, pageH, pxlOpts.getUnits().getMediaSizeUnits()));
        paper.setImageableArea(pageX * CONVERT, pageY * CONVERT, pageW * CONVERT, pageH * CONVERT);
        page.setPaper(paper);
    } else {
        log.warn("Could not apply custom size, using printer default");
        attributes.add(new MediaPrintableArea(0, 0, (float) page.getWidth() / 72f, (float) page.getHeight() / 72f, PrintOptions.Unit.INCH.getMediaSizeUnits()));
    }
    log.trace("{}", Arrays.toString(attributes.toArray()));
    return attributes;
}
Also used : Paper(java.awt.print.Paper) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 42 with PrintRequestAttributeSet

use of javax.print.attribute.PrintRequestAttributeSet in project tray by qzind.

the class PrintRaw method printToPrinter.

/**
 * Constructs a {@code SimpleDoc} with the {@code commands} byte array.
 */
private void printToPrinter(PrintService service, byte[] cmds, PrintOptions.Raw rawOpts) throws PrintException {
    if (service == null) {
        throw new NullPrintServiceException("Service cannot be null");
    }
    if (cmds == null || cmds.length == 0) {
        throw new NullCommandException("No commands found to send to the printer");
    }
    SimpleDoc doc = new SimpleDoc(cmds, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    attributes.add(new JobName(rawOpts.getJobName(Constants.RAW_PRINT), Locale.getDefault()));
    DocPrintJob printJob = service.createPrintJob();
    waitForPrint(printJob, doc, attributes);
}
Also used : JobName(javax.print.attribute.standard.JobName) NullCommandException(qz.exception.NullCommandException) NullPrintServiceException(qz.exception.NullPrintServiceException) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Example 43 with PrintRequestAttributeSet

use of javax.print.attribute.PrintRequestAttributeSet in project pdfbox by apache.

the class PDFDebugger method printMenuItemActionPerformed.

private void printMenuItemActionPerformed(ActionEvent evt) {
    if (document == null) {
        return;
    }
    AccessPermission ap = document.getCurrentAccessPermission();
    if (!ap.canPrint()) {
        JOptionPane.showMessageDialog(this, "You do not have permission to print");
        return;
    }
    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(new PDFPageable(document, Orientation.AUTO, false, PrintDpiMenu.getDpiSelection()));
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        PDViewerPreferences vp = document.getDocumentCatalog().getViewerPreferences();
        if (vp != null && vp.getDuplex() != null) {
            String dp = vp.getDuplex();
            if (PDViewerPreferences.DUPLEX.DuplexFlipLongEdge.toString().equals(dp)) {
                pras.add(Sides.TWO_SIDED_LONG_EDGE);
            } else if (PDViewerPreferences.DUPLEX.DuplexFlipShortEdge.toString().equals(dp)) {
                pras.add(Sides.TWO_SIDED_SHORT_EDGE);
            } else if (PDViewerPreferences.DUPLEX.Simplex.toString().equals(dp)) {
                pras.add(Sides.ONE_SIDED);
            }
        }
        if (job.printDialog(pras)) {
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            try {
                long t0 = System.nanoTime();
                job.print(pras);
                long t1 = System.nanoTime();
                long ms = TimeUnit.MILLISECONDS.convert(t1 - t0, TimeUnit.NANOSECONDS);
                LOG.info("Printed in " + ms + " ms");
            } finally {
                setCursor(Cursor.getDefaultCursor());
            }
        }
    } catch (PrinterException e) {
        new ErrorDialog(e).setVisible(true);
    }
}
Also used : PDFPageable(org.apache.pdfbox.printing.PDFPageable) AccessPermission(org.apache.pdfbox.pdmodel.encryption.AccessPermission) ErrorDialog(org.apache.pdfbox.debugger.ui.ErrorDialog) COSString(org.apache.pdfbox.cos.COSString) PrinterException(java.awt.print.PrinterException) PDViewerPreferences(org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Aggregations

PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)43 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)34 PrinterJob (java.awt.print.PrinterJob)16 PrintService (javax.print.PrintService)14 PageFormat (java.awt.print.PageFormat)12 PrinterException (java.awt.print.PrinterException)11 IOException (java.io.IOException)10 Copies (javax.print.attribute.standard.Copies)8 DocFlavor (javax.print.DocFlavor)7 Attribute (javax.print.attribute.Attribute)7 JobName (javax.print.attribute.standard.JobName)7 Printable (java.awt.print.Printable)6 DocPrintJob (javax.print.DocPrintJob)6 File (java.io.File)5 PrintException (javax.print.PrintException)5 SimpleDoc (javax.print.SimpleDoc)5 HeadlessException (java.awt.HeadlessException)4 Rectangle (java.awt.Rectangle)4 Paper (java.awt.print.Paper)4 Locale (java.util.Locale)3