Search in sources :

Example 1 with DocPrintJob

use of javax.print.DocPrintJob in project antlr4 by antlr.

the class GraphicsSupport method saveImage.

/**
	 [The "BSD license"]
	 Copyright (c) 2011 Cay Horstmann
	 All rights reserved.

	 Redistribution and use in source and binary forms, with or without
	 modification, are permitted provided that the following conditions
	 are met:

	 1. Redistributions of source code must retain the above copyright
	 notice, this list of conditions and the following disclaimer.
	 2. Redistributions in binary form must reproduce the above copyright
	 notice, this list of conditions and the following disclaimer in the
	 documentation and/or other materials provided with the distribution.
	 3. The name of the author may not be used to endorse or promote products
	 derived from this software without specific prior written permission.

	 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	 */
public static void saveImage(final JComponent comp, String fileName) throws IOException, PrintException {
    if (fileName.endsWith(".ps") || fileName.endsWith(".eps")) {
        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        String mimeType = "application/postscript";
        StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mimeType);
        System.out.println(Arrays.toString(factories));
        if (factories.length > 0) {
            FileOutputStream out = new FileOutputStream(fileName);
            PrintService service = factories[0].getPrintService(out);
            SimpleDoc doc = new SimpleDoc(new Printable() {

                @Override
                public int print(Graphics g, PageFormat pf, int page) {
                    if (page >= 1)
                        return Printable.NO_SUCH_PAGE;
                    else {
                        Graphics2D g2 = (Graphics2D) g;
                        g2.translate((pf.getWidth() - pf.getImageableWidth()) / 2, (pf.getHeight() - pf.getImageableHeight()) / 2);
                        if (comp.getWidth() > pf.getImageableWidth() || comp.getHeight() > pf.getImageableHeight()) {
                            double sf1 = pf.getImageableWidth() / (comp.getWidth() + 1);
                            double sf2 = pf.getImageableHeight() / (comp.getHeight() + 1);
                            double s = Math.min(sf1, sf2);
                            g2.scale(s, s);
                        }
                        comp.paint(g);
                        return Printable.PAGE_EXISTS;
                    }
                }
            }, flavor, null);
            DocPrintJob job = service.createPrintJob();
            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            job.print(doc, attributes);
            out.close();
        }
    } else {
        // parrt: works with [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
        Rectangle rect = comp.getBounds();
        BufferedImage image = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(Color.WHITE);
        g.fill(rect);
        //			g.setColor(Color.BLACK);
        comp.paint(g);
        String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
        boolean result = ImageIO.write(image, extension, new File(fileName));
        if (!result) {
            System.err.println("Now imager for " + extension);
        }
        g.dispose();
    }
}
Also used : DocPrintJob(javax.print.DocPrintJob) BufferedImage(java.awt.image.BufferedImage) PrintService(javax.print.PrintService) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PageFormat(java.awt.print.PageFormat) SimpleDoc(javax.print.SimpleDoc) FileOutputStream(java.io.FileOutputStream) Printable(java.awt.print.Printable) DocFlavor(javax.print.DocFlavor) StreamPrintServiceFactory(javax.print.StreamPrintServiceFactory) File(java.io.File) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 2 with DocPrintJob

use of javax.print.DocPrintJob in project camel by apache.

the class PrinterPrintTest method setupJavaPrint.

protected void setupJavaPrint() {
    // "install" another default printer
    PrintService psDefault = mock(PrintService.class);
    when(psDefault.getName()).thenReturn("DefaultPrinter");
    when(psDefault.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE);
    PrintServiceLookup psLookup = mock(PrintServiceLookup.class);
    when(psLookup.getPrintServices()).thenReturn(new PrintService[] { psDefault });
    when(psLookup.getDefaultPrintService()).thenReturn(psDefault);
    DocPrintJob docPrintJob = mock(DocPrintJob.class);
    when(psDefault.createPrintJob()).thenReturn(docPrintJob);
    MediaTray[] trays = new MediaTray[] { MediaTray.TOP, MediaTray.MIDDLE, MediaTray.BOTTOM };
    when(psDefault.getSupportedAttributeValues(Media.class, null, null)).thenReturn(trays);
    PrintServiceLookup.registerServiceProvider(psLookup);
}
Also used : PrintServiceLookup(javax.print.PrintServiceLookup) DocPrintJob(javax.print.DocPrintJob) MediaTray(javax.print.attribute.standard.MediaTray) DocFlavor(javax.print.DocFlavor) PrintService(javax.print.PrintService)

Example 3 with DocPrintJob

use of javax.print.DocPrintJob 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 4 with DocPrintJob

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

the class POSClientSide method printOtherOS.

/**
	 * Print Other S.O
	 * @param fis
	 * @return void
	 */
private void printOtherOS(FileInputStream fis) {
    DocFlavor docFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc document = new SimpleDoc(fis, docFormat, null);
    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
    PrintService[] listsPrintService = PrintServiceLookup.lookupPrintServices(docFormat, attributeSet);
    try {
        DocPrintJob printJob = null;
        for (int x = 0; x < listsPrintService.length; x++) {
            if (listsPrintService[x].getName().equals(m_Print)) {
                printJob = listsPrintService[x].createPrintJob();
            }
        }
        printJob.print(document, attributeSet);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SimpleDoc(javax.print.SimpleDoc) SimpleDoc(javax.print.SimpleDoc) Doc(javax.print.Doc) DocPrintJob(javax.print.DocPrintJob) DocFlavor(javax.print.DocFlavor) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) IOException(java.io.IOException) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintService(javax.print.PrintService)

Example 5 with DocPrintJob

use of javax.print.DocPrintJob in project camel by apache.

the class PrinterPrintTest method moreThanOneLprEndpoint.

@Test
public void moreThanOneLprEndpoint() throws Exception {
    if (isAwtHeadless()) {
        return;
    }
    int numberOfPrintservicesBefore = PrintServiceLookup.lookupPrintServices(null, null).length;
    // setup javax.print 
    PrintService ps1 = mock(PrintService.class);
    when(ps1.getName()).thenReturn("printer1");
    when(ps1.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE);
    PrintService ps2 = mock(PrintService.class);
    when(ps2.getName()).thenReturn("printer2");
    boolean res1 = PrintServiceLookup.registerService(ps1);
    assertTrue("PrintService #1 should be registered.", res1);
    boolean res2 = PrintServiceLookup.registerService(ps2);
    assertTrue("PrintService #2 should be registered.", res2);
    PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, null);
    assertEquals("lookup should report two PrintServices.", numberOfPrintservicesBefore + 2, pss.length);
    DocPrintJob job1 = mock(DocPrintJob.class);
    when(ps1.createPrintJob()).thenReturn(job1);
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:start1").to("lpr://localhost/printer1?sendToPrinter=true");
            from("direct:start2").to("lpr://localhost/printer2?sendToPrinter=false");
        }
    });
    context.start();
    // Are there two different PrintConfigurations?
    Map<String, Endpoint> epm = context().getEndpointMap();
    assertEquals("Four endpoints", 4, epm.size());
    Endpoint lp1 = null;
    Endpoint lp2 = null;
    for (Map.Entry<String, Endpoint> ep : epm.entrySet()) {
        if (ep.getKey().contains("printer1")) {
            lp1 = ep.getValue();
        }
        if (ep.getKey().contains("printer2")) {
            lp2 = ep.getValue();
        }
    }
    assertNotNull(lp1);
    assertNotNull(lp2);
    assertEquals("printer1", ((PrinterEndpoint) lp1).getConfig().getPrintername());
    assertEquals("printer2", ((PrinterEndpoint) lp2).getConfig().getPrintername());
    template.sendBody("direct:start1", "Hello Printer 1");
    context.stop();
    verify(job1, times(1)).print(any(Doc.class), any(PrintRequestAttributeSet.class));
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) DocPrintJob(javax.print.DocPrintJob) Endpoint(org.apache.camel.Endpoint) PrintService(javax.print.PrintService) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) Endpoint(org.apache.camel.Endpoint) Doc(javax.print.Doc) DocFlavor(javax.print.DocFlavor) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DocPrintJob (javax.print.DocPrintJob)8 DocFlavor (javax.print.DocFlavor)6 PrintService (javax.print.PrintService)5 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)5 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)5 Doc (javax.print.Doc)4 SimpleDoc (javax.print.SimpleDoc)3 FileOutputStream (java.io.FileOutputStream)2 StreamPrintServiceFactory (javax.print.StreamPrintServiceFactory)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 Test (org.junit.Test)2 BufferedImage (java.awt.image.BufferedImage)1 PageFormat (java.awt.print.PageFormat)1 Printable (java.awt.print.Printable)1 PrinterException (java.awt.print.PrinterException)1 File (java.io.File)1 IOException (java.io.IOException)1 Map (java.util.Map)1 PrintException (javax.print.PrintException)1 PrintServiceLookup (javax.print.PrintServiceLookup)1