package panelPrint;
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.standard.PrinterName;
public class PanelPrint {
double px2mm = 0.2645833333333; // 1px = 0.2645833333333 mm
double dpFaktor = 72.0;
/**
* Sollte die übergebene Komponente samt Objekten drucken (<i>hier ein JPanel</i>)
* @param printObjekt Die zu druckende Komponente.
* @param printerName Der Druckername (<i>hier <b>Canon_MF720Cdw</b></i>)
* @param tray Das Papierfach (<i>"Einzug 1", "Einzug 2" oder "Mehrzweck Papierfach"</i>
* @param isoTyp Das Papierformat. (<i>A4, C5, usw</i>)
* @param myPfad Pfad zum Anwendungsorder in dem das erstellte BufferedImage als "print.png" gespeichert wird.
*/
public PanelPrint(Component printObjekt, String printerName, String tray, String isoTyp, String myPfad) {
System.out.println("Wirft die aktuelle Position und Grösse des printObjekt:");
System.out.println("\tprintObjekt X = "+printObjekt.getX()+" px \tmm = "+(printObjekt.getX()*px2mm));
System.out.println("\tprintObjekt Y = "+printObjekt.getY()+" px \tmm = "+(printObjekt.getY()*px2mm));
System.out.println("\tprintObjekt W = "+printObjekt.getWidth()+" px\tmm = "+(printObjekt.getWidth()*px2mm));
System.out.println("\tprintObjekt H = "+printObjekt.getHeight()+" px\tmm = "+(printObjekt.getHeight()*px2mm));
AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName(printerName, null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, aset);
if (services.length >= 1) {
DocPrintJob pj = services[0].createPrintJob();
MediaSizeName msnISO = null;
switch(isoTyp) {
case "A4":
msnISO = MediaSizeName.ISO_A4;
break;
case "C5":
msnISO = MediaSizeName.ISO_C5;
break;
}
MediaSize ms = MediaSize.getMediaSizeForName(msnISO);
int msWidth = Math.round(ms.getX(MediaSize.MM));
int msHeight = Math.round(ms.getY(MediaSize.MM));
PrintService drucker = null;
drucker = getPrinter(printerName);
//Hier habe ich versucht den Druckbaren bereich fest zu legen!
MediaPrintableArea mpa = new MediaPrintableArea(4, 4, msWidth, msHeight, MediaPrintableArea.MM);
DocAttributeSet das = new HashDocAttributeSet();
das.add(msnISO);
das.add(mpa);
das.add(PrintQuality.HIGH);
das.add(getMediaTray(drucker, tray));
FileInputStream fls = null;
File tmpSave = new File(myPfad+"print.png");
try {
//Erstellt ein Buffered-Image und speichert es als "../print.png"
ImageIO.write(getSnapShot(printObjekt), "png", tmpSave);
//Liest das "../print.png" in FileInputStream (fls) ein.
fls = new FileInputStream(tmpSave);
if(fls != null && tmpSave.exists()) {
Doc doc = new SimpleDoc(fls, DocFlavor.INPUT_STREAM.PNG, das);
pj.print(doc, null);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
if(tmpSave.exists()) {
// tmpSave.delete();
System.exit(0);
}
}
}
/**
* Erstellt aus einer Komponente ein <b><font color='blue'>BufferedImage</font></b>
* @param view Die Komponente (<i>JFrame, JDialog, JPanel, JLabel, etc.</i>)
* @return Das erstellte <b>BufferedImage</b>.
*/
private BufferedImage getSnapShot(Component view)
{
int w = (int) (view.getWidth());
int h = (int) (view.getHeight());
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
synchronized (view.getTreeLock())
{
view.paint(image.createGraphics());
}
return image;
}
//-----------------------------------------------------------------------------------------------
private PrintService getPrinter(String printerName) {
AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName(printerName, null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, aset);
for (PrintService service : services) {
if (service.getName().equals(printerName)) {
return service;
}
}
return null;
}
private MediaTray getMediaTray(PrintService printer, String trayName) {
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
Object o = printer.getSupportedAttributeValues(Media.class, flavor, null);
if (o != null && o.getClass().isArray()) {
for (Media media : (Media[]) o) {
if (media instanceof MediaTray) {
if(media.toString().equals(trayName)) {
return (MediaTray) media;
}
}
}
}
return null;
}
}