Search in sources :

Example 41 with PageFormat

use of java.awt.print.PageFormat in project adempiere by adempiere.

the class PrintUtil method dump.

//	getJobPriority
/*************************************************************************/
/**
	 * 	Dump Printer Job info
	 * 	@param job printer job
	 */
public static void dump(PrinterJob job) {
    StringBuffer sb = new StringBuffer(job.getJobName());
    sb.append("/").append(job.getUserName()).append(" Service=").append(job.getPrintService().getName()).append(" Copies=").append(job.getCopies());
    PageFormat pf = job.defaultPage();
    sb.append(" DefaultPage ").append("x=").append(pf.getImageableX()).append(",y=").append(pf.getImageableY()).append(" w=").append(pf.getImageableWidth()).append(",h=").append(pf.getImageableHeight());
    System.out.println(sb.toString());
}
Also used : PageFormat(java.awt.print.PageFormat)

Example 42 with PageFormat

use of java.awt.print.PageFormat in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method setAttributes.

/* subclasses may need to pull extra information out of the attribute set
     * They can override this method & call super.setAttributes()
     */
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
    /*  reset all values to defaults */
    setCollated(false);
    sidesAttr = null;
    pageRangesAttr = null;
    copiesAttr = 0;
    jobNameAttr = null;
    userNameAttr = null;
    destinationAttr = null;
    collateAttReq = false;
    PrintService service = getPrintService();
    if (attributes == null || service == null) {
        return;
    }
    boolean fidelity = false;
    Fidelity attrFidelity = (Fidelity) attributes.get(Fidelity.class);
    if (attrFidelity != null && attrFidelity == Fidelity.FIDELITY_TRUE) {
        fidelity = true;
    }
    if (fidelity == true) {
        AttributeSet unsupported = service.getUnsupportedAttributes(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
        if (unsupported != null) {
            throw new PrinterException("Fidelity cannot be satisfied");
        }
    }
    /*
         * Since we have verified supported values if fidelity is true,
         * we can either ignore unsupported values, or substitute a
         * reasonable alternative
         */
    SheetCollate collateAttr = (SheetCollate) attributes.get(SheetCollate.class);
    if (isSupportedValue(collateAttr, attributes)) {
        setCollated(collateAttr == SheetCollate.COLLATED);
    }
    sidesAttr = (Sides) attributes.get(Sides.class);
    if (!isSupportedValue(sidesAttr, attributes)) {
        sidesAttr = Sides.ONE_SIDED;
    }
    pageRangesAttr = (PageRanges) attributes.get(PageRanges.class);
    if (!isSupportedValue(pageRangesAttr, attributes)) {
        pageRangesAttr = null;
    } else {
        if ((SunPageSelection) attributes.get(SunPageSelection.class) == SunPageSelection.RANGE) {
            // get to, from, min, max page ranges
            int[][] range = pageRangesAttr.getMembers();
            // setPageRanges uses 0-based indexing so we subtract 1
            setPageRange(range[0][0] - 1, range[0][1] - 1);
        } else {
            setPageRange(-1, -1);
        }
    }
    Copies copies = (Copies) attributes.get(Copies.class);
    if (isSupportedValue(copies, attributes) || (!fidelity && copies != null)) {
        copiesAttr = copies.getValue();
        setCopies(copiesAttr);
    } else {
        copiesAttr = getCopies();
    }
    Destination destination = (Destination) attributes.get(Destination.class);
    if (isSupportedValue(destination, attributes)) {
        try {
            // Old code (new File(destination.getURI())).getPath()
            // would generate a "URI is not hierarchical" IAE
            // for "file:out.prn" so we use getSchemeSpecificPart instead
            destinationAttr = "" + new File(destination.getURI().getSchemeSpecificPart());
        } catch (Exception e) {
            // paranoid exception
            Destination defaultDest = (Destination) service.getDefaultAttributeValue(Destination.class);
            if (defaultDest != null) {
                destinationAttr = "" + new File(defaultDest.getURI().getSchemeSpecificPart());
            }
        }
    }
    JobSheets jobSheets = (JobSheets) attributes.get(JobSheets.class);
    if (jobSheets != null) {
        noJobSheet = jobSheets == JobSheets.NONE;
    }
    JobName jobName = (JobName) attributes.get(JobName.class);
    if (isSupportedValue(jobName, attributes) || (!fidelity && jobName != null)) {
        jobNameAttr = jobName.getValue();
        setJobName(jobNameAttr);
    } else {
        jobNameAttr = getJobName();
    }
    RequestingUserName userName = (RequestingUserName) attributes.get(RequestingUserName.class);
    if (isSupportedValue(userName, attributes) || (!fidelity && userName != null)) {
        userNameAttr = userName.getValue();
    } else {
        try {
            userNameAttr = getUserName();
        } catch (SecurityException e) {
            userNameAttr = "";
        }
    }
    /* OpenBook is used internally only when app uses Printable.
         * This is the case when we use the values from the attribute set.
         */
    Media media = (Media) attributes.get(Media.class);
    OrientationRequested orientReq = (OrientationRequested) attributes.get(OrientationRequested.class);
    MediaPrintableArea mpa = (MediaPrintableArea) attributes.get(MediaPrintableArea.class);
    if ((orientReq != null || media != null || mpa != null) && getPageable() instanceof OpenBook) {
        /* We could almost(!) use PrinterJob.getPageFormat() except
             * here we need to start with the PageFormat from the OpenBook :
             */
        Pageable pageable = getPageable();
        Printable printable = pageable.getPrintable(0);
        PageFormat pf = (PageFormat) pageable.getPageFormat(0).clone();
        Paper paper = pf.getPaper();
        /* If there's a media but no media printable area, we can try
             * to retrieve the default value for mpa and use that.
             */
        if (mpa == null && media != null && service.isAttributeCategorySupported(MediaPrintableArea.class)) {
            Object mpaVals = service.getSupportedAttributeValues(MediaPrintableArea.class, null, attributes);
            if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[]) mpaVals).length > 0) {
                mpa = ((MediaPrintableArea[]) mpaVals)[0];
            }
        }
        if (isSupportedValue(orientReq, attributes) || (!fidelity && orientReq != null)) {
            int orient;
            if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
                orient = PageFormat.REVERSE_LANDSCAPE;
            } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
                orient = PageFormat.LANDSCAPE;
            } else {
                orient = PageFormat.PORTRAIT;
            }
            pf.setOrientation(orient);
        }
        if (isSupportedValue(media, attributes) || (!fidelity && media != null)) {
            if (media instanceof MediaSizeName) {
                MediaSizeName msn = (MediaSizeName) media;
                MediaSize msz = MediaSize.getMediaSizeForName(msn);
                if (msz != null) {
                    float paperWid = msz.getX(MediaSize.INCH) * 72.0f;
                    float paperHgt = msz.getY(MediaSize.INCH) * 72.0f;
                    paper.setSize(paperWid, paperHgt);
                    if (mpa == null) {
                        paper.setImageableArea(72.0, 72.0, paperWid - 144.0, paperHgt - 144.0);
                    }
                }
            }
        }
        if (isSupportedValue(mpa, attributes) || (!fidelity && mpa != null)) {
            float[] printableArea = mpa.getPrintableArea(MediaPrintableArea.INCH);
            for (int i = 0; i < printableArea.length; i++) {
                printableArea[i] = printableArea[i] * 72.0f;
            }
            paper.setImageableArea(printableArea[0], printableArea[1], printableArea[2], printableArea[3]);
        }
        pf.setPaper(paper);
        pf = validatePage(pf);
        setPrintable(printable, pf);
    } else {
        // for AWT where pageable is not an instance of OpenBook,
        // we need to save paper info
        this.attributes = attributes;
    }
}
Also used : Fidelity(javax.print.attribute.standard.Fidelity) Destination(javax.print.attribute.standard.Destination) JobName(javax.print.attribute.standard.JobName) PrinterException(java.awt.print.PrinterException) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) PageFormat(java.awt.print.PageFormat) Pageable(java.awt.print.Pageable) Copies(javax.print.attribute.standard.Copies) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Media(javax.print.attribute.standard.Media) JobSheets(javax.print.attribute.standard.JobSheets) PrinterAbortException(java.awt.print.PrinterAbortException) HeadlessException(java.awt.HeadlessException) PrintException(javax.print.PrintException) PrinterException(java.awt.print.PrinterException) IOException(java.io.IOException) AttributeSet(javax.print.attribute.AttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) SheetCollate(javax.print.attribute.standard.SheetCollate) RequestingUserName(javax.print.attribute.standard.RequestingUserName) Paper(java.awt.print.Paper) Printable(java.awt.print.Printable) File(java.io.File)

Example 43 with PageFormat

use of java.awt.print.PageFormat in project jdk8u_jdk by JetBrains.

the class PrintJob2D method printDialog.

public boolean printDialog() {
    boolean proceedWithPrint = false;
    printerJob = PrinterJob.getPrinterJob();
    if (printerJob == null) {
        return false;
    }
    DialogType d = this.jobAttributes.getDialog();
    PrintService pServ = printerJob.getPrintService();
    if ((pServ == null) && (d == DialogType.NONE)) {
        return false;
    }
    copyAttributes(pServ);
    DefaultSelectionType select = this.jobAttributes.getDefaultSelection();
    if (select == DefaultSelectionType.RANGE) {
        attributes.add(SunPageSelection.RANGE);
    } else if (select == DefaultSelectionType.SELECTION) {
        attributes.add(SunPageSelection.SELECTION);
    } else {
        attributes.add(SunPageSelection.ALL);
    }
    if (frame != null) {
        attributes.add(new DialogOwner(frame));
    }
    if (d == DialogType.NONE) {
        proceedWithPrint = true;
    } else {
        if (d == DialogType.NATIVE) {
            attributes.add(DialogTypeSelection.NATIVE);
        } else {
            //  (d == DialogType.COMMON)
            attributes.add(DialogTypeSelection.COMMON);
        }
        if (proceedWithPrint = printerJob.printDialog(attributes)) {
            if (pServ == null) {
                // Windows gives an option to install a service
                // when it detects there are no printers so
                // we make sure we get the updated print service.
                pServ = printerJob.getPrintService();
                if (pServ == null) {
                    return false;
                }
            }
            updateAttributes();
            translateOutputProps();
        }
    }
    if (proceedWithPrint) {
        JobName jname = (JobName) attributes.get(JobName.class);
        if (jname != null) {
            printerJob.setJobName(jname.toString());
        }
        pageFormat = new PageFormat();
        Media media = (Media) attributes.get(Media.class);
        MediaSize mediaSize = null;
        if (media != null && media instanceof MediaSizeName) {
            mediaSize = MediaSize.getMediaSizeForName((MediaSizeName) media);
        }
        Paper p = pageFormat.getPaper();
        if (mediaSize != null) {
            p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0);
        }
        if (pageAttributes.getOrigin() == OriginType.PRINTABLE) {
            // AWT uses 1/4" borders by default
            p.setImageableArea(18.0, 18.0, p.getWidth() - 36.0, p.getHeight() - 36.0);
        } else {
            p.setImageableArea(0.0, 0.0, p.getWidth(), p.getHeight());
        }
        pageFormat.setPaper(p);
        OrientationRequested orient = (OrientationRequested) attributes.get(OrientationRequested.class);
        if (orient != null && orient == OrientationRequested.REVERSE_LANDSCAPE) {
            pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
        } else if (orient == OrientationRequested.LANDSCAPE) {
            pageFormat.setOrientation(PageFormat.LANDSCAPE);
        } else {
            pageFormat.setOrientation(PageFormat.PORTRAIT);
        }
        printerJob.setPrintable(this, pageFormat);
    }
    return proceedWithPrint;
}
Also used : PageFormat(java.awt.print.PageFormat) MediaSize(javax.print.attribute.standard.MediaSize) MediaSizeName(javax.print.attribute.standard.MediaSizeName) JobName(javax.print.attribute.standard.JobName) Media(javax.print.attribute.standard.Media) Paper(java.awt.print.Paper) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintService(javax.print.PrintService)

Example 44 with PageFormat

use of java.awt.print.PageFormat in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method pageDialog.

/**
     * return a PageFormat corresponding to the updated attributes,
     * or null if the user cancelled the dialog.
     */
public PageFormat pageDialog(final PrintRequestAttributeSet attributes) throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    DialogTypeSelection dlg = (DialogTypeSelection) attributes.get(DialogTypeSelection.class);
    // Check for native, note that default dialog is COMMON.
    if (dlg == DialogTypeSelection.NATIVE) {
        PrintService pservice = getPrintService();
        PageFormat pageFrmAttrib = attributeToPageFormat(pservice, attributes);
        setParentWindowID(attributes);
        PageFormat page = pageDialog(pageFrmAttrib);
        clearParentWindowID();
        // page object and as per spec, we should return null in that case.
        if (page == pageFrmAttrib) {
            return null;
        }
        updateAttributesWithPageFormat(pservice, page, attributes);
        return page;
    }
    final GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    Rectangle bounds = gc.getBounds();
    int x = bounds.x + bounds.width / 3;
    int y = bounds.y + bounds.height / 3;
    PrintService service = (PrintService) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            PrintService service = getPrintService();
            if (service == null) {
                ServiceDialog.showNoPrintService(gc);
                return null;
            }
            return service;
        }
    });
    if (service == null) {
        return null;
    }
    if (onTop != null) {
        attributes.add(onTop);
    }
    ServiceDialog pageDialog = new ServiceDialog(gc, x, y, service, DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes, (Frame) null);
    pageDialog.show();
    if (pageDialog.getStatus() == ServiceDialog.APPROVE) {
        PrintRequestAttributeSet newas = pageDialog.getAttributes();
        Class amCategory = SunAlternateMedia.class;
        if (attributes.containsKey(amCategory) && !newas.containsKey(amCategory)) {
            attributes.remove(amCategory);
        }
        attributes.addAll(newas);
        return attributeToPageFormat(service, attributes);
    } else {
        return null;
    }
}
Also used : ServiceDialog(sun.print.ServiceDialog) HeadlessException(java.awt.HeadlessException) Rectangle(java.awt.Rectangle) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) GraphicsConfiguration(java.awt.GraphicsConfiguration) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) PageFormat(java.awt.print.PageFormat)

Example 45 with PageFormat

use of java.awt.print.PageFormat in project jdk8u_jdk by JetBrains.

the class WPathGraphics method redrawRegion.

/**
     * Have the printing application redraw everything that falls
     * within the page bounds defined by <code>region</code>.
     */
@Override
public void redrawRegion(Rectangle2D region, double scaleX, double scaleY, Shape savedClip, AffineTransform savedTransform) throws PrinterException {
    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();
    Printable painter = getPrintable();
    PageFormat pageFormat = getPageFormat();
    int pageIndex = getPageIndex();
    /* Create a buffered image big enough to hold the portion
         * of the source image being printed.
         */
    BufferedImage deepImage = new BufferedImage((int) region.getWidth(), (int) region.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    /* Get a graphics for the application to render into.
         * We initialize the buffer to white in order to
         * match the paper and then we shift the BufferedImage
         * so that it covers the area on the page where the
         * caller's Image will be drawn.
         */
    Graphics2D g = deepImage.createGraphics();
    ProxyGraphics2D proxy = new ProxyGraphics2D(g, wPrinterJob);
    proxy.setColor(Color.white);
    proxy.fillRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
    proxy.clipRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
    proxy.translate(-region.getX(), -region.getY());
    /* Calculate the resolution of the source image.
         */
    float sourceResX = (float) (wPrinterJob.getXRes() / scaleX);
    float sourceResY = (float) (wPrinterJob.getYRes() / scaleY);
    /* The application expects to see user space at 72 dpi.
         * so change user space from image source resolution to
         *  72 dpi.
         */
    proxy.scale(sourceResX / DEFAULT_USER_RES, sourceResY / DEFAULT_USER_RES);
    proxy.translate(-wPrinterJob.getPhysicalPrintableX(pageFormat.getPaper()) / wPrinterJob.getXRes() * DEFAULT_USER_RES, -wPrinterJob.getPhysicalPrintableY(pageFormat.getPaper()) / wPrinterJob.getYRes() * DEFAULT_USER_RES);
    /* NB User space now has to be at 72 dpi for this calc to be correct */
    proxy.transform(new AffineTransform(getPageFormat().getMatrix()));
    proxy.setPaint(Color.black);
    painter.print(proxy, pageFormat, pageIndex);
    g.dispose();
    /* We need to set the device clip using saved information.
         * savedClip intersects the user clip with a clip that restricts
         * the GDI rendered area of our BufferedImage to that which
         * may correspond to a rotate or shear.
         * The saved device transform is needed as the current transform
         * is not likely to be the same.
         */
    if (savedClip != null) {
        deviceClip(savedClip.getPathIterator(savedTransform));
    }
    /* Scale the bounding rectangle by the scale transform.
         * Because the scaling transform has only x and y
         * scaling components it is equivalent to multiplying
         * the x components of the bounding rectangle by
         * the x scaling factor and to multiplying the y components
         * by the y scaling factor.
         */
    Rectangle2D.Float scaledBounds = new Rectangle2D.Float((float) (region.getX() * scaleX), (float) (region.getY() * scaleY), (float) (region.getWidth() * scaleX), (float) (region.getHeight() * scaleY));
    /* Pull the raster data from the buffered image
         * and pass it along to GDI.
         */
    ByteComponentRaster tile = (ByteComponentRaster) deepImage.getRaster();
    wPrinterJob.drawImage3ByteBGR(tile.getDataStorage(), scaledBounds.x, scaledBounds.y, scaledBounds.width, scaledBounds.height, 0f, 0f, deepImage.getWidth(), deepImage.getHeight());
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ProxyGraphics2D(sun.print.ProxyGraphics2D) PageFormat(java.awt.print.PageFormat) ByteComponentRaster(sun.awt.image.ByteComponentRaster) AffineTransform(java.awt.geom.AffineTransform) Printable(java.awt.print.Printable) ProxyGraphics2D(sun.print.ProxyGraphics2D)

Aggregations

PageFormat (java.awt.print.PageFormat)61 Paper (java.awt.print.Paper)23 PrinterJob (java.awt.print.PrinterJob)21 PrinterException (java.awt.print.PrinterException)19 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)12 PrintService (javax.print.PrintService)11 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)10 BufferedImage (java.awt.image.BufferedImage)9 Printable (java.awt.print.Printable)9 Graphics2D (java.awt.Graphics2D)7 File (java.io.File)6 IOException (java.io.IOException)6 Media (javax.print.attribute.standard.Media)6 MediaSize (javax.print.attribute.standard.MediaSize)6 HeadlessException (java.awt.HeadlessException)5 StreamPrintService (javax.print.StreamPrintService)5 MediaSizeName (javax.print.attribute.standard.MediaSizeName)5 Dimension (java.awt.Dimension)4 Rectangle (java.awt.Rectangle)4 Rectangle2D (java.awt.geom.Rectangle2D)4