Search in sources :

Example 1 with StreamPrintService

use of javax.print.StreamPrintService in project jdk8u_jdk by JetBrains.

the class PrintSEUmlauts method main.

public static void main(String[] args) throws Exception {
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    String mime = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mime);
    if (factories.length == 0) {
        System.out.println("No print service found.");
        return;
    }
    FileOutputStream output = new FileOutputStream("out.ps");
    StreamPrintService service = factories[0].getPrintService(output);
    SimpleDoc doc = new SimpleDoc(new PrintSEUmlauts(), DocFlavor.SERVICE_FORMATTED.PRINTABLE, new HashDocAttributeSet());
    DocPrintJob job = service.createPrintJob();
    job.addPrintJobListener(new PrintJobAdapter() {

        @Override
        public void printJobCompleted(PrintJobEvent pje) {
            testPrintAndExit();
        }
    });
    job.print(doc, new HashPrintRequestAttributeSet());
}
Also used : HashDocAttributeSet(javax.print.attribute.HashDocAttributeSet) DocPrintJob(javax.print.DocPrintJob) StreamPrintService(javax.print.StreamPrintService) SimpleDoc(javax.print.SimpleDoc) FileOutputStream(java.io.FileOutputStream) PrintJobAdapter(javax.print.event.PrintJobAdapter) DocFlavor(javax.print.DocFlavor) StreamPrintServiceFactory(javax.print.StreamPrintServiceFactory) PrintJobEvent(javax.print.event.PrintJobEvent) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 2 with StreamPrintService

use of javax.print.StreamPrintService in project adempiere by adempiere.

the class ReportEngine method createPS.

//	createPS
/**
	 * 	Write PostScript to writer
	 * 	@param os output stream
	 * 	@return true if success
	 */
public boolean createPS(OutputStream os) {
    try {
        String outputMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
        DocFlavor docFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
        StreamPrintServiceFactory[] spsfactories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(docFlavor, outputMimeType);
        if (spsfactories.length == 0) {
            log.log(Level.SEVERE, "(fos) - No StreamPrintService");
            return false;
        }
        //	just use first one - sun.print.PSStreamPrinterFactory
        //	System.out.println("- " + spsfactories[0]);
        StreamPrintService sps = spsfactories[0].getPrintService(os);
        //	get format
        if (m_layout == null)
            layout();
        //	print it
        sps.createPrintJob().print(m_layout.getPageable(false), new HashPrintRequestAttributeSet());
        //
        os.flush();
        //following 2 line for backward compatibility
        if (os instanceof FileOutputStream)
            ((FileOutputStream) os).close();
    } catch (Exception e) {
        log.log(Level.SEVERE, "(fos)", e);
    }
    return false;
}
Also used : FileOutputStream(java.io.FileOutputStream) StreamPrintService(javax.print.StreamPrintService) DocFlavor(javax.print.DocFlavor) StreamPrintServiceFactory(javax.print.StreamPrintServiceFactory) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 3 with StreamPrintService

use of javax.print.StreamPrintService in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method printDialog.

/**
     * Presents the user a dialog for changing properties of the
     * print job interactively.
     * The services browsable here are determined by the type of
     * service currently installed.
     * If the application installed a StreamPrintService on this
     * PrinterJob, only the available StreamPrintService (factories) are
     * browsable.
     *
     * @param attributes to store changed properties.
     * @return false if the user cancels the dialog and true otherwise.
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
     * returns true.
     * @see java.awt.GraphicsEnvironment#isHeadless
     */
public boolean printDialog(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) {
        this.attributes = attributes;
        try {
            debug_println("calling setAttributes in printDialog");
            setAttributes(attributes);
        } catch (PrinterException e) {
        }
        setParentWindowID(attributes);
        boolean ret = printDialog();
        clearParentWindowID();
        this.attributes = attributes;
        return ret;
    }
    /* A security check has already been performed in the
         * java.awt.print.printerJob.getPrinterJob method.
         * So by the time we get here, it is OK for the current thread
         * to print either to a file (from a Dialog we control!) or
         * to a chosen printer.
         *
         * We raise privilege when we put up the dialog, to avoid
         * the "warning applet window" banner.
         */
    final GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    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 false;
    }
    PrintService[] services;
    StreamPrintServiceFactory[] spsFactories = null;
    if (service instanceof StreamPrintService) {
        spsFactories = lookupStreamPrintServices(null);
        services = new StreamPrintService[spsFactories.length];
        for (int i = 0; i < spsFactories.length; i++) {
            services[i] = spsFactories[i].getPrintService(null);
        }
    } else {
        services = (PrintService[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

            public Object run() {
                PrintService[] services = PrinterJob.lookupPrintServices();
                return services;
            }
        });
        if ((services == null) || (services.length == 0)) {
            /*
                 * No services but default PrintService exists?
                 * Create services using defaultService.
                 */
            services = new PrintService[1];
            services[0] = service;
        }
    }
    Rectangle bounds = gc.getBounds();
    int x = bounds.x + bounds.width / 3;
    int y = bounds.y + bounds.height / 3;
    PrintService newService;
    // temporarily add an attribute pointing back to this job.
    PrinterJobWrapper jobWrapper = new PrinterJobWrapper(this);
    attributes.add(jobWrapper);
    try {
        newService = ServiceUI.printDialog(gc, x, y, services, service, DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
    } catch (IllegalArgumentException iae) {
        newService = ServiceUI.printDialog(gc, x, y, services, services[0], DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
    }
    attributes.remove(PrinterJobWrapper.class);
    if (newService == null) {
        return false;
    }
    if (!service.equals(newService)) {
        try {
            setPrintService(newService);
        } catch (PrinterException e) {
            /*
                 * The only time it would throw an exception is when
                 * newService is no longer available but we should still
                 * select this printer.
                 */
            myService = newService;
        }
    }
    return true;
}
Also used : HeadlessException(java.awt.HeadlessException) Rectangle(java.awt.Rectangle) PrinterException(java.awt.print.PrinterException) StreamPrintService(javax.print.StreamPrintService) DialogTypeSelection(javax.print.attribute.standard.DialogTypeSelection) GraphicsConfiguration(java.awt.GraphicsConfiguration) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) StreamPrintServiceFactory(javax.print.StreamPrintServiceFactory)

Example 4 with StreamPrintService

use of javax.print.StreamPrintService in project jdk8u_jdk by JetBrains.

the class PSPrinterJob method startDoc.

/**
     * Invoked by the RasterPrinterJob super class
     * this method is called to mark the start of a
     * document.
     */
protected void startDoc() throws PrinterException {
    // A security check has been performed in the
    // java.awt.print.printerJob.getPrinterJob method.
    // We use an inner class to execute the privilged open operations.
    // Note that we only open a file if it has been nominated by
    // the end-user in a dialog that we ouselves put up.
    OutputStream output;
    if (epsPrinter == null) {
        if (getPrintService() instanceof PSStreamPrintService) {
            StreamPrintService sps = (StreamPrintService) getPrintService();
            mDestType = RasterPrinterJob.STREAM;
            if (sps.isDisposed()) {
                throw new PrinterException("service is disposed");
            }
            output = sps.getOutputStream();
            if (output == null) {
                throw new PrinterException("Null output stream");
            }
        } else {
            /* REMIND: This needs to be more maintainable */
            mNoJobSheet = super.noJobSheet;
            if (super.destinationAttr != null) {
                mDestType = RasterPrinterJob.FILE;
                mDestination = super.destinationAttr;
            }
            if (mDestType == RasterPrinterJob.FILE) {
                try {
                    spoolFile = new File(mDestination);
                    output = new FileOutputStream(spoolFile);
                } catch (IOException ex) {
                    throw new PrinterIOException(ex);
                }
            } else {
                PrinterOpener po = new PrinterOpener();
                java.security.AccessController.doPrivileged(po);
                if (po.pex != null) {
                    throw po.pex;
                }
                output = po.result;
            }
        }
        mPSStream = new PrintStream(new BufferedOutputStream(output));
        mPSStream.println(ADOBE_PS_STR);
    }
    mPSStream.println("%%BeginProlog");
    mPSStream.println(READIMAGEPROC);
    mPSStream.println("/BD {bind def} bind def");
    mPSStream.println("/D {def} BD");
    mPSStream.println("/C {curveto} BD");
    mPSStream.println("/L {lineto} BD");
    mPSStream.println("/M {moveto} BD");
    mPSStream.println("/R {grestore} BD");
    mPSStream.println("/G {gsave} BD");
    mPSStream.println("/N {newpath} BD");
    mPSStream.println("/P {closepath} BD");
    mPSStream.println("/EC {eoclip} BD");
    mPSStream.println("/WC {clip} BD");
    mPSStream.println("/EF {eofill} BD");
    mPSStream.println("/WF {fill} BD");
    mPSStream.println("/SG {setgray} BD");
    mPSStream.println("/SC {setrgbcolor} BD");
    mPSStream.println("/ISOF {");
    mPSStream.println("     dup findfont dup length 1 add dict begin {");
    mPSStream.println("             1 index /FID eq {pop pop} {D} ifelse");
    mPSStream.println("     } forall /Encoding ISOLatin1Encoding D");
    mPSStream.println("     currentdict end definefont");
    mPSStream.println("} BD");
    mPSStream.println("/NZ {dup 1 lt {pop 1} if} BD");
    /* The following procedure takes args: string, x, y, desiredWidth.
         * It calculates using stringwidth the width of the string in the
         * current font and subtracts it from the desiredWidth and divides
         * this by stringLen-1. This gives us a per-glyph adjustment in
         * the spacing needed (either +ve or -ve) to make the string
         * print at the desiredWidth. The ashow procedure call takes this
         * per-glyph adjustment as an argument. This is necessary for WYSIWYG
         */
    mPSStream.println("/" + DrawStringName + " {");
    mPSStream.println("     moveto 1 index stringwidth pop NZ sub");
    mPSStream.println("     1 index length 1 sub NZ div 0");
    mPSStream.println("     3 2 roll ashow newpath} BD");
    mPSStream.println("/FL [");
    if (mFontProps == null) {
        mPSStream.println(" /Helvetica ISOF");
        mPSStream.println(" /Helvetica-Bold ISOF");
        mPSStream.println(" /Helvetica-Oblique ISOF");
        mPSStream.println(" /Helvetica-BoldOblique ISOF");
        mPSStream.println(" /Times-Roman ISOF");
        mPSStream.println(" /Times-Bold ISOF");
        mPSStream.println(" /Times-Italic ISOF");
        mPSStream.println(" /Times-BoldItalic ISOF");
        mPSStream.println(" /Courier ISOF");
        mPSStream.println(" /Courier-Bold ISOF");
        mPSStream.println(" /Courier-Oblique ISOF");
        mPSStream.println(" /Courier-BoldOblique ISOF");
    } else {
        int cnt = Integer.parseInt(mFontProps.getProperty("font.num", "9"));
        for (int i = 0; i < cnt; i++) {
            mPSStream.println("    /" + mFontProps.getProperty("font." + String.valueOf(i), "Courier ISOF"));
        }
    }
    mPSStream.println("] D");
    mPSStream.println("/" + SetFontName + " {");
    mPSStream.println("     FL exch get exch scalefont");
    mPSStream.println("     [1 0 0 -1 0 0] makefont setfont} BD");
    mPSStream.println("%%EndProlog");
    mPSStream.println("%%BeginSetup");
    if (epsPrinter == null) {
        // Set Page Size using first page's format.
        PageFormat pageFormat = getPageable().getPageFormat(0);
        double paperHeight = pageFormat.getPaper().getHeight();
        double paperWidth = pageFormat.getPaper().getWidth();
        /* PostScript printers can always generate uncollated copies.
             */
        mPSStream.print("<< /PageSize [" + paperWidth + " " + paperHeight + "]");
        final PrintService pservice = getPrintService();
        Boolean isPS = (Boolean) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

            public Object run() {
                try {
                    Class psClass = Class.forName("sun.print.IPPPrintService");
                    if (psClass.isInstance(pservice)) {
                        Method isPSMethod = psClass.getMethod("isPostscript", (Class[]) null);
                        return (Boolean) isPSMethod.invoke(pservice, (Object[]) null);
                    }
                } catch (Throwable t) {
                }
                return Boolean.TRUE;
            }
        });
        if (isPS) {
            mPSStream.print(" /DeferredMediaSelection true");
        }
        mPSStream.print(" /ImagingBBox null /ManualFeed false");
        mPSStream.print(isCollated() ? " /Collate true" : "");
        mPSStream.print(" /NumCopies " + getCopiesInt());
        if (sidesAttr != Sides.ONE_SIDED) {
            if (sidesAttr == Sides.TWO_SIDED_LONG_EDGE) {
                mPSStream.print(" /Duplex true ");
            } else if (sidesAttr == Sides.TWO_SIDED_SHORT_EDGE) {
                mPSStream.print(" /Duplex true /Tumble true ");
            }
        }
        mPSStream.println(" >> setpagedevice ");
    }
    mPSStream.println("%%EndSetup");
}
Also used : PrintStream(java.io.PrintStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PrinterIOException(java.awt.print.PrinterIOException) PrinterException(java.awt.print.PrinterException) PrinterIOException(java.awt.print.PrinterIOException) IOException(java.io.IOException) Method(java.lang.reflect.Method) StreamPrintService(javax.print.StreamPrintService) PrintService(javax.print.PrintService) StreamPrintService(javax.print.StreamPrintService) PageFormat(java.awt.print.PageFormat) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

StreamPrintService (javax.print.StreamPrintService)4 FileOutputStream (java.io.FileOutputStream)3 StreamPrintServiceFactory (javax.print.StreamPrintServiceFactory)3 PrinterException (java.awt.print.PrinterException)2 IOException (java.io.IOException)2 DocFlavor (javax.print.DocFlavor)2 PrintService (javax.print.PrintService)2 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)1 HeadlessException (java.awt.HeadlessException)1 Rectangle (java.awt.Rectangle)1 PageFormat (java.awt.print.PageFormat)1 PrinterIOException (java.awt.print.PrinterIOException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1