Search in sources :

Example 61 with PageFormat

use of java.awt.print.PageFormat in project antlr4 by tunnelvisionlabs.

the class GraphicsSupport method saveImage.

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 : Rectangle(java.awt.Rectangle) DocPrintJob(javax.print.DocPrintJob) BufferedImage(java.awt.image.BufferedImage) PrintService(javax.print.PrintService) Graphics2D(java.awt.Graphics2D) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) Graphics(java.awt.Graphics) 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)

Aggregations

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