01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13:
14: package org.rapla.components.iolayer;
15:
16: import java.awt.print.*;
17: import javax.print.*;
18: import javax.print.attribute.*;
19: import javax.print.attribute.standard.*;
20: import java.io.*;
21:
22: /** This class will only work with JDK 1.4 and above, it
23: uses javax.print.PrintService for exporting to postscript format
24: */
25:
26: public class PrintExport {
27: static DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
28:
29: private static StreamPrintServiceFactory getPSExportServiceFactory() {
30: StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
31: .lookupStreamPrintServiceFactories(flavor,
32: "application/postscript");
33:
34: if (factories.length == 0) {
35: return null;
36: }
37: /*
38: for (int i=0;i<factories.length;i++) {
39: System.out.println("Stream Factory " + factories[i]);
40: System.out.println(" Output " + factories[i].getOutputFormat());
41: DocFlavor[] docFlavors = factories[i].getSupportedDocFlavors();
42: for (int j=0;j<docFlavors.length;j++) {
43: System.out.println(" Flavor " + docFlavors[j].getMimeType());
44: }
45: }*/
46: return factories[0];
47: }
48:
49: public static boolean supportsPostscriptExport() {
50: return getPSExportServiceFactory() != null;
51: }
52:
53: public void save(Printable print, PageFormat format,
54: OutputStream out) throws IOException {
55: StreamPrintService sps = null;
56: try {
57: StreamPrintServiceFactory spsf = getPSExportServiceFactory();
58: if (spsf == null) {
59: throw new UnsupportedOperationException(
60: "No suitable factories for postscript-export.");
61: }
62: sps = spsf.getPrintService(out);
63: Doc doc = new SimpleDoc(print, flavor, null);
64: PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
65: if (format.getOrientation() == PageFormat.LANDSCAPE) {
66: aset.add(OrientationRequested.LANDSCAPE);
67: }
68: if (format.getOrientation() == PageFormat.PORTRAIT) {
69: aset.add(OrientationRequested.PORTRAIT);
70: }
71: if (format.getOrientation() == PageFormat.REVERSE_LANDSCAPE) {
72: aset.add(OrientationRequested.REVERSE_LANDSCAPE);
73: }
74:
75: aset.add(MediaName.ISO_A4_WHITE);
76: Paper paper = format.getPaper();
77: if (sps.getSupportedAttributeValues(
78: MediaPrintableArea.class, null, null) != null) {
79: MediaPrintableArea printableArea = new MediaPrintableArea(
80: (float) (paper.getImageableX() / 72),
81: (float) (paper.getImageableY() / 72),
82: (float) (paper.getImageableWidth() / 72),
83: (float) (paper.getImageableHeight() / 72),
84: Size2DSyntax.INCH);
85: aset.add(printableArea);
86: // System.out.println("new Area: " + printableArea);
87: }
88: sps.createPrintJob().print(doc, aset);
89: } catch (PrintException ex) {
90: throw new IOException(ex.getMessage());
91: } finally {
92: if (sps != null)
93: sps.dispose();
94: }
95: }
96:
97: }
|