Search in sources :

Example 11 with PrinterException

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

the class RasterPrinterJob method printPage.

/**
     * Print a page from the provided document.
     * @return int Printable.PAGE_EXISTS if the page existed and was drawn and
     *             Printable.NO_SUCH_PAGE if the page did not exist.
     * @see java.awt.print.Printable
     */
protected int printPage(Pageable document, int pageIndex) throws PrinterException {
    PageFormat page;
    PageFormat origPage;
    Printable painter;
    try {
        origPage = document.getPageFormat(pageIndex);
        page = (PageFormat) origPage.clone();
        painter = document.getPrintable(pageIndex);
    } catch (Exception e) {
        PrinterException pe = new PrinterException("Error getting page or printable.[ " + e + " ]");
        pe.initCause(e);
        throw pe;
    }
    /* Get the imageable area from Paper instead of PageFormat
         * because we do not want it adjusted by the page orientation.
         */
    Paper paper = page.getPaper();
    // if non-portrait and 270 degree landscape rotation
    if (page.getOrientation() != PageFormat.PORTRAIT && landscapeRotates270) {
        double left = paper.getImageableX();
        double top = paper.getImageableY();
        double width = paper.getImageableWidth();
        double height = paper.getImageableHeight();
        paper.setImageableArea(paper.getWidth() - left - width, paper.getHeight() - top - height, width, height);
        page.setPaper(paper);
        if (page.getOrientation() == PageFormat.LANDSCAPE) {
            page.setOrientation(PageFormat.REVERSE_LANDSCAPE);
        } else {
            page.setOrientation(PageFormat.LANDSCAPE);
        }
    }
    double xScale = getXRes() / 72.0;
    double yScale = getYRes() / 72.0;
    /* The deviceArea is the imageable area in the printer's
         * resolution.
         */
    Rectangle2D deviceArea = new Rectangle2D.Double(paper.getImageableX() * xScale, paper.getImageableY() * yScale, paper.getImageableWidth() * xScale, paper.getImageableHeight() * yScale);
    /* Build and hold on to a uniform transform so that
         * we can get back to device space at the beginning
         * of each band.
         */
    AffineTransform uniformTransform = new AffineTransform();
    /* The scale transform is used to switch from the
         * device space to the user's 72 dpi space.
         */
    AffineTransform scaleTransform = new AffineTransform();
    scaleTransform.scale(xScale, yScale);
    /* bandwidth is multiple of 4 as the data is used in a win32 DIB and
         * some drivers behave badly if scanlines aren't multiples of 4 bytes.
         */
    int bandWidth = (int) deviceArea.getWidth();
    if (bandWidth % 4 != 0) {
        bandWidth += (4 - (bandWidth % 4));
    }
    if (bandWidth <= 0) {
        throw new PrinterException("Paper's imageable width is too small.");
    }
    int deviceAreaHeight = (int) deviceArea.getHeight();
    if (deviceAreaHeight <= 0) {
        throw new PrinterException("Paper's imageable height is too small.");
    }
    /* Figure out the number of lines that will fit into
         * our maximum band size. The hard coded 3 reflects the
         * fact that we can only create 24 bit per pixel 3 byte BGR
         * BufferedImages. FIX.
         */
    int bandHeight = (int) (MAX_BAND_SIZE / bandWidth / 3);
    int deviceLeft = (int) Math.rint(paper.getImageableX() * xScale);
    int deviceTop = (int) Math.rint(paper.getImageableY() * yScale);
    /* The device transform is used to move the band down
         * the page using translates. Normally this is all it
         * would do, but since, when printing, the Window's
         * DIB format wants the last line to be first (lowest) in
         * memory, the deviceTransform moves the origin to the
         * bottom of the band and flips the origin. This way the
         * app prints upside down into the band which is the DIB
         * format.
         */
    AffineTransform deviceTransform = new AffineTransform();
    deviceTransform.translate(-deviceLeft, deviceTop);
    deviceTransform.translate(0, bandHeight);
    deviceTransform.scale(1, -1);
    /* Create a BufferedImage to hold the band. We set the clip
         * of the band to be tight around the bits so that the
         * application can use it to figure what part of the
         * page needs to be drawn. The clip is never altered in
         * this method, but we do translate the band's coordinate
         * system so that the app will see the clip moving down the
         * page though it s always around the same set of pixels.
         */
    BufferedImage pBand = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
    /* Have the app draw into a PeekGraphics object so we can
         * learn something about the needs of the print job.
         */
    PeekGraphics peekGraphics = createPeekGraphics(pBand.createGraphics(), this);
    Rectangle2D.Double pageFormatArea = new Rectangle2D.Double(page.getImageableX(), page.getImageableY(), page.getImageableWidth(), page.getImageableHeight());
    peekGraphics.transform(scaleTransform);
    peekGraphics.translate(-getPhysicalPrintableX(paper) / xScale, -getPhysicalPrintableY(paper) / yScale);
    peekGraphics.transform(new AffineTransform(page.getMatrix()));
    initPrinterGraphics(peekGraphics, pageFormatArea);
    AffineTransform pgAt = peekGraphics.getTransform();
    /* Update the information used to return a GraphicsConfiguration
         * for this printer device. It needs to be updated per page as
         * not all pages in a job may be the same size (different bounds)
         * The transform is the scaling transform as this corresponds to
         * the default transform for the device. The width and height are
         * those of the paper, not the page format, as we want to describe
         * the bounds of the device in its natural coordinate system of
         * device coordinate whereas a page format may be in a rotated context.
         */
    setGraphicsConfigInfo(scaleTransform, paper.getWidth(), paper.getHeight());
    int pageResult = painter.print(peekGraphics, origPage, pageIndex);
    debug_println("pageResult " + pageResult);
    if (pageResult == Printable.PAGE_EXISTS) {
        debug_println("startPage " + pageIndex);
        /* We need to check if the paper size is changed.
             * Note that it is not sufficient to ask for the pageformat
             * of "pageIndex-1", since PageRanges mean that pages can be
             * skipped. So we have to look at the actual last paper size used.
             */
        Paper thisPaper = page.getPaper();
        boolean paperChanged = previousPaper == null || thisPaper.getWidth() != previousPaper.getWidth() || thisPaper.getHeight() != previousPaper.getHeight();
        previousPaper = thisPaper;
        startPage(page, painter, pageIndex, paperChanged);
        Graphics2D pathGraphics = createPathGraphics(peekGraphics, this, painter, page, pageIndex);
        /* If we can convert the page directly to the
             * underlying graphics system then we do not
             * need to rasterize. We also may not need to
             * create the 'band' if all the pages can take
             * this path.
             */
        if (pathGraphics != null) {
            pathGraphics.transform(scaleTransform);
            // user (0,0) should be origin of page, not imageable area
            pathGraphics.translate(-getPhysicalPrintableX(paper) / xScale, -getPhysicalPrintableY(paper) / yScale);
            pathGraphics.transform(new AffineTransform(page.getMatrix()));
            initPrinterGraphics(pathGraphics, pageFormatArea);
            redrawList.clear();
            AffineTransform initialTx = pathGraphics.getTransform();
            painter.print(pathGraphics, origPage, pageIndex);
            for (int i = 0; i < redrawList.size(); i++) {
                GraphicsState gstate = (GraphicsState) redrawList.get(i);
                pathGraphics.setTransform(initialTx);
                ((PathGraphics) pathGraphics).redrawRegion(gstate.region, gstate.sx, gstate.sy, gstate.theClip, gstate.theTransform);
            }
        /* This is the banded-raster printing loop.
             * It should be moved into its own method.
             */
        } else {
            BufferedImage band = cachedBand;
            if (cachedBand == null || bandWidth != cachedBandWidth || bandHeight != cachedBandHeight) {
                band = new BufferedImage(bandWidth, bandHeight, BufferedImage.TYPE_3BYTE_BGR);
                cachedBand = band;
                cachedBandWidth = bandWidth;
                cachedBandHeight = bandHeight;
            }
            Graphics2D bandGraphics = band.createGraphics();
            Rectangle2D.Double clipArea = new Rectangle2D.Double(0, 0, bandWidth, bandHeight);
            initPrinterGraphics(bandGraphics, clipArea);
            ProxyGraphics2D painterGraphics = new ProxyGraphics2D(bandGraphics, this);
            Graphics2D clearGraphics = band.createGraphics();
            clearGraphics.setColor(Color.white);
            /* We need the actual bits of the BufferedImage to send to
                 * the native Window's code. 'data' points to the actual
                 * pixels. Right now these are in ARGB format with 8 bits
                 * per component. We need to use a monochrome BufferedImage
                 * for monochrome printers when this is supported by
                 * BufferedImage. FIX
                 */
            ByteInterleavedRaster tile = (ByteInterleavedRaster) band.getRaster();
            byte[] data = tile.getDataStorage();
            /* Loop over the page moving our band down the page,
                 * calling the app to render the band, and then send the band
                 * to the printer.
                 */
            int deviceBottom = deviceTop + deviceAreaHeight;
            /* device's printable x,y is really addressable origin
                 * we address relative to media origin so when we print a
                 * band we need to adjust for the different methods of
                 * addressing it.
                 */
            int deviceAddressableX = (int) getPhysicalPrintableX(paper);
            int deviceAddressableY = (int) getPhysicalPrintableY(paper);
            for (int bandTop = 0; bandTop <= deviceAreaHeight; bandTop += bandHeight) {
                /* Put the band back into device space and
                     * erase the contents of the band.
                     */
                clearGraphics.fillRect(0, 0, bandWidth, bandHeight);
                /* Put the band into the correct location on the
                     * page. Once the band is moved we translate the
                     * device transform so that the band will move down
                     * the page on the next iteration of the loop.
                     */
                bandGraphics.setTransform(uniformTransform);
                bandGraphics.transform(deviceTransform);
                deviceTransform.translate(0, -bandHeight);
                /* Switch the band from device space to user,
                     * 72 dpi, space.
                     */
                bandGraphics.transform(scaleTransform);
                bandGraphics.transform(new AffineTransform(page.getMatrix()));
                Rectangle clip = bandGraphics.getClipBounds();
                clip = pgAt.createTransformedShape(clip).getBounds();
                if ((clip == null) || peekGraphics.hitsDrawingArea(clip) && (bandWidth > 0 && bandHeight > 0)) {
                    /* if the client has specified an imageable X or Y
                         * which is off than the physically addressable
                         * area of the page, then we need to adjust for that
                         * here so that we pass only non -ve band coordinates
                         * We also need to translate by the adjusted amount
                         * so that printing appears in the correct place.
                         */
                    int bandX = deviceLeft - deviceAddressableX;
                    if (bandX < 0) {
                        bandGraphics.translate(bandX / xScale, 0);
                        bandX = 0;
                    }
                    int bandY = deviceTop + bandTop - deviceAddressableY;
                    if (bandY < 0) {
                        bandGraphics.translate(0, bandY / yScale);
                        bandY = 0;
                    }
                    /* Have the app's painter image into the band
                         * and then send the band to the printer.
                         */
                    painterGraphics.setDelegate((Graphics2D) bandGraphics.create());
                    painter.print(painterGraphics, origPage, pageIndex);
                    painterGraphics.dispose();
                    printBand(data, bandX, bandY, bandWidth, bandHeight);
                }
            }
            clearGraphics.dispose();
            bandGraphics.dispose();
        }
        debug_println("calling endPage " + pageIndex);
        endPage(page, painter, pageIndex);
    }
    return pageResult;
}
Also used : ByteInterleavedRaster(sun.awt.image.ByteInterleavedRaster) Rectangle2D(java.awt.geom.Rectangle2D) Rectangle(java.awt.Rectangle) PrinterException(java.awt.print.PrinterException) PrinterAbortException(java.awt.print.PrinterAbortException) HeadlessException(java.awt.HeadlessException) PrintException(javax.print.PrintException) PrinterException(java.awt.print.PrinterException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) PageFormat(java.awt.print.PageFormat) Paper(java.awt.print.Paper) AffineTransform(java.awt.geom.AffineTransform) Printable(java.awt.print.Printable)

Example 12 with PrinterException

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

the class RasterPrinterJob method print.

public void print(PrintRequestAttributeSet attributes) throws PrinterException {
    /*
         * In the future PrinterJob will probably always dispatch
         * the print job to the PrintService.
         * This is how third party 2D Print Services will be invoked
         * when applications use the PrinterJob API.
         * However the JRE's concrete PrinterJob implementations have
         * not yet been re-worked to be implemented as standalone
         * services, and are implemented only as subclasses of PrinterJob.
         * So here we dispatch only those services we do not recognize
         * as implemented through platform subclasses of PrinterJob
         * (and this class).
         */
    PrintService psvc = getPrintService();
    debug_println("psvc = " + psvc);
    if (psvc == null) {
        throw new PrinterException("No print service found.");
    }
    // Check the list of services.  This service may have been
    // deleted already
    PrinterState prnState = (PrinterState) psvc.getAttribute(PrinterState.class);
    if (prnState == PrinterState.STOPPED) {
        PrinterStateReasons prnStateReasons = (PrinterStateReasons) psvc.getAttribute(PrinterStateReasons.class);
        if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) {
            throw new PrinterException("PrintService is no longer available.");
        }
    }
    if ((PrinterIsAcceptingJobs) (psvc.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
        throw new PrinterException("Printer is not accepting job.");
    }
    if ((psvc instanceof SunPrinterJobService) && ((SunPrinterJobService) psvc).usesClass(getClass())) {
        setAttributes(attributes);
        // throw exception for invalid destination
        if (destinationAttr != null) {
            validateDestination(destinationAttr);
        }
    } else {
        spoolToService(psvc, attributes);
        return;
    }
    /* We need to make sure that the collation and copies
         * settings are initialised */
    initPrinter();
    int numCollatedCopies = getCollatedCopies();
    int numNonCollatedCopies = getNoncollatedCopies();
    debug_println("getCollatedCopies()  " + numCollatedCopies + " getNoncollatedCopies() " + numNonCollatedCopies);
    /* Get the range of pages we are to print. If the
         * last page to print is unknown, then we print to
         * the end of the document. Note that firstPage
         * and lastPage are 0 based page indices.
         */
    int numPages = mDocument.getNumberOfPages();
    if (numPages == 0) {
        return;
    }
    int firstPage = getFirstPage();
    int lastPage = getLastPage();
    if (lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) {
        int totalPages = mDocument.getNumberOfPages();
        if (totalPages != Pageable.UNKNOWN_NUMBER_OF_PAGES) {
            lastPage = mDocument.getNumberOfPages() - 1;
        }
    }
    try {
        synchronized (this) {
            performingPrinting = true;
            userCancelled = false;
        }
        startDoc();
        if (isCancelled()) {
            cancelDoc();
        }
        // PageRanges can be set even if RANGE is not selected
        // so we need to check if it is selected.
        boolean rangeIsSelected = true;
        if (attributes != null) {
            SunPageSelection pages = (SunPageSelection) attributes.get(SunPageSelection.class);
            if ((pages != null) && (pages != SunPageSelection.RANGE)) {
                rangeIsSelected = false;
            }
        }
        debug_println("after startDoc rangeSelected? " + rangeIsSelected + " numNonCollatedCopies " + numNonCollatedCopies);
        /* Three nested loops iterate over the document. The outer loop
             * counts the number of collated copies while the inner loop
             * counts the number of nonCollated copies. Normally, one of
             * these two loops will only execute once; that is we will
             * either print collated copies or noncollated copies. The
             * middle loop iterates over the pages.
             * If a PageRanges attribute is used, it constrains the pages
             * that are imaged. If a platform subclass (though a user dialog)
             * requests a page range via setPageRange(). it too can
             * constrain the page ranges that are imaged.
             * It is expected that only one of these will be used in a
             * job but both should be able to co-exist.
             */
        for (int collated = 0; collated < numCollatedCopies; collated++) {
            for (int i = firstPage, pageResult = Printable.PAGE_EXISTS; (i <= lastPage || lastPage == Pageable.UNKNOWN_NUMBER_OF_PAGES) && pageResult == Printable.PAGE_EXISTS; i++) {
                if ((pageRangesAttr != null) && rangeIsSelected) {
                    int nexti = pageRangesAttr.next(i);
                    if (nexti == -1) {
                        break;
                    } else if (nexti != i + 1) {
                        continue;
                    }
                }
                for (int nonCollated = 0; nonCollated < numNonCollatedCopies && pageResult == Printable.PAGE_EXISTS; nonCollated++) {
                    if (isCancelled()) {
                        cancelDoc();
                    }
                    debug_println("printPage " + i);
                    pageResult = printPage(mDocument, i);
                }
            }
        }
        if (isCancelled()) {
            cancelDoc();
        }
    } finally {
        // reset previousPaper in case this job is invoked again.
        previousPaper = null;
        synchronized (this) {
            if (performingPrinting) {
                endDoc();
            }
            performingPrinting = false;
            notify();
        }
    }
}
Also used : PrinterState(javax.print.attribute.standard.PrinterState) PrinterStateReasons(javax.print.attribute.standard.PrinterStateReasons) SunPrinterJobService(sun.print.SunPrinterJobService) SunPageSelection(sun.print.SunPageSelection) PrinterException(java.awt.print.PrinterException) PrinterIsAcceptingJobs(javax.print.attribute.standard.PrinterIsAcceptingJobs) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService)

Example 13 with PrinterException

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

the class RasterPrinterJob method validateDestination.

protected void validateDestination(String dest) throws PrinterException {
    if (dest == null) {
        return;
    }
    // dest is null for Destination(new URI(""))
    // because isAttributeValueSupported returns false in setAttributes
    // Destination(new URI(" ")) throws URISyntaxException
    File f = new File(dest);
    try {
        // check if this is a new file and if filename chars are valid
        if (f.createNewFile()) {
            f.delete();
        }
    } catch (IOException ioe) {
        throw new PrinterException("Cannot write to file:" + dest);
    } catch (SecurityException se) {
    //There is already file read/write access so at this point
    // only delete access is denied.  Just ignore it because in
    // most cases the file created in createNewFile gets overwritten
    // anyway.
    }
    File pFile = f.getParentFile();
    if ((f.exists() && (!f.isFile() || !f.canWrite())) || ((pFile != null) && (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
        throw new PrinterException("Cannot write to file:" + dest);
    }
}
Also used : IOException(java.io.IOException) PrinterException(java.awt.print.PrinterException) File(java.io.File)

Example 14 with PrinterException

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

the class WPrinterJob method pageDialog.

/* Instance Methods */
/**
     * Display a dialog to the user allowing the modification of a
     * PageFormat instance.
     * The <code>page</code> argument is used to initialize controls
     * in the page setup dialog.
     * If the user cancels the dialog, then the method returns the
     * original <code>page</code> object unmodified.
     * If the user okays the dialog then the method returns a new
     * PageFormat object with the indicated changes.
     * In either case the original <code>page</code> object will
     * not be modified.
     * @param     page    the default PageFormat presented to the user
     *                    for modification
     * @return    the original <code>page</code> object if the dialog
     *            is cancelled, or a new PageFormat object containing
     *            the format indicated by the user if the dialog is
     *            acknowledged
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
     * returns true.
     * @see java.awt.GraphicsEnvironment#isHeadless
     * @since     JDK1.2
     */
@Override
public PageFormat pageDialog(PageFormat page) throws HeadlessException {
    if (GraphicsEnvironment.isHeadless()) {
        throw new HeadlessException();
    }
    if (!(getPrintService() instanceof Win32PrintService)) {
        return super.pageDialog(page);
    }
    PageFormat pageClone = (PageFormat) page.clone();
    boolean result = false;
    /*
         * Fix for 4507585: show the native modal dialog the same way printDialog() does so
         * that it won't block event dispatching when called on EventDispatchThread.
         */
    WPageDialog dialog = new WPageDialog((Frame) null, this, pageClone, null);
    dialog.setRetVal(false);
    dialog.setVisible(true);
    result = dialog.getRetVal();
    dialog.dispose();
    // myService => current PrintService
    if (result && (myService != null)) {
        // It's possible that current printer is changed through
        // the "Printer..." button so we query again from native.
        String printerName = getNativePrintService();
        if (!myService.getName().equals(printerName)) {
            // we update the current PrintService
            try {
                setPrintService(Win32PrintServiceLookup.getWin32PrintLUS().getPrintServiceByName(printerName));
            } catch (PrinterException e) {
            }
        }
        // Update attributes, this will preserve the page settings.
        //  - same code as in RasterPrinterJob.java
        updatePageAttributes(myService, pageClone);
        return pageClone;
    } else {
        return page;
    }
}
Also used : Win32PrintService(sun.print.Win32PrintService) PageFormat(java.awt.print.PageFormat) HeadlessException(java.awt.HeadlessException) PrinterException(java.awt.print.PrinterException)

Example 15 with PrinterException

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

the class PrintPreviewPanel method paintComponent.

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    // x offset of page start in window
    double xOffset;
    // y offset of page start in window
    double yOffset;
    // scale factor to fit page in window
    double scale;
    double px = pageFormat.getWidth();
    double py = pageFormat.getHeight();
    double sx = getWidth() - 1;
    double sy = getHeight() - 1;
    if (px / py < sx / sy) {
        // center horizontally
        scale = sy / py;
        xOffset = 0.5 * (sx - scale * px);
        yOffset = 0;
    } else {
        // center vertically
        scale = sx / px;
        xOffset = 0;
        yOffset = 0.5 * (sy - scale * py);
    }
    g2.translate((float) xOffset, (float) yOffset);
    g2.scale((float) scale, (float) scale);
    // draw page outline (ignoring margins)
    Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);
    g2.setPaint(Color.WHITE);
    g2.fill(page);
    g2.setPaint(Color.BLACK);
    g2.draw(page);
    // draw the outline of the margin
    Rectangle2D margin = new Rectangle2D.Double(pageFormat.getImageableX(), pageFormat.getImageableY(), pageFormat.getImageableWidth(), pageFormat.getImageableHeight());
    g2.setPaint(Color.DARK_GRAY);
    g2.draw(margin);
    // reset the color
    g2.setPaint(Color.BLACK);
    try {
        printable.print(g2, pageFormat, 0);
    } catch (PrinterException e) {
        Logger.getLogger(PrintPreviewPanel.class.getName()).log(Level.INFO, e.getLocalizedMessage(), e);
    }
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) PrinterException(java.awt.print.PrinterException) Graphics2D(java.awt.Graphics2D)

Aggregations

PrinterException (java.awt.print.PrinterException)27 PrinterJob (java.awt.print.PrinterJob)15 PageFormat (java.awt.print.PageFormat)11 PrintService (javax.print.PrintService)8 Printable (java.awt.print.Printable)5 StreamPrintService (javax.print.StreamPrintService)5 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)5 HeadlessException (java.awt.HeadlessException)4 File (java.io.File)4 IOException (java.io.IOException)4 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)4 Container (java.awt.Container)3 ActionEvent (java.awt.event.ActionEvent)3 Paper (java.awt.print.Paper)3 PrintException (javax.print.PrintException)3 AbstractAction (javax.swing.AbstractAction)3 JButton (javax.swing.JButton)3 JFrame (javax.swing.JFrame)3 Graphics2D (java.awt.Graphics2D)2 Rectangle (java.awt.Rectangle)2