-
Java - Kodi për të printuar
keto source jane si mund te printesh from Java application
package com.pogradecari.util;
import java.io.*;
import java.awt.*;
import java.awt.print.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.*;
import com.bono.ui.slip.*;
public class Printer {
private static final String DEFAULT_PRINTER = "default";
private static final int NO_PRINTING_GROUP = 0;
private static Map printers = new HashMap();
private Printer() {
}
public static void printSlip() {
printSlip(false);
}
public static void printSlip(boolean isDriverSlip) {
String printerName = isDriverSlip ? ApplicationProperties.getProperty("PrinterDriver") : ApplicationProperties.getProperty("PrinterReceipt");
printText(SlipTable.getSlipTableModel().toHtml(isD riverSlip), printerName);
}
public static void printKitchen() {
printKitchen(false);
}
public static void printKitchen(boolean isPaid) {
int[] groups = SlipTable.getSlipTableModel().getGroups();
if(groups != null && groups.length == 1 && groups[0] == NO_PRINTING_GROUP) return;
for(int i = 0, n = groups.length; i < n; i++) {
if(groups[i] != NO_PRINTING_GROUP) printText(SlipTable.getSlipTableModel().toKitchenH tml(groups[i], isPaid), ApplicationProperties.getProperty("Printer" + groups[i]));
}
}
public static synchronized void printText(final String text, final String printerName) {
if(text == null) return;
if(ApplicationProperties.getProperty("enablePrint") == null) return;
new Thread() {
public void run() {
// Set the document type
DocFlavor myFormat = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
Printable printable = new Printable() {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if(pageIndex >= 1) return Printable.NO_SUCH_PAGE;
((Graphics2D)graphics).translate(pageFormat.getIma geableX(), pageFormat.getImageableY());
JFrame frame = new JFrame();
JEditorPane pane = new JEditorPane("text/html", text);
//RepaintManager currentManager = RepaintManager.currentManager(pane);
//currentManager.setDoubleBufferingEnabled(false);
frame.getContentPane().add(pane);
frame.pack();
//frame.setSize(300, 500);
//frame.setVisible(true);
frame.setVisible(false);
//JLabel label = new JLabel();
//label.setFont(new Font("control", Font.PLAIN, 10));
//label.setText("(char)23");
//frame.getContentPane().add(label);
//label.print(graphics);
pane.print(graphics);
return Printable.PAGE_EXISTS;
}
};
Doc myDoc = new SimpleDoc(printable, myFormat, null);
// Build a set of attributes
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
//aset.add(MediaSizeName.EXECUTIVE);
aset.add(MediaTray.MAIN);
aset.add(new MediaPrintableArea(5, 5, 80, 2000, MediaPrintableArea.MM));
//aset.add(new MediaPrintableArea(10, 10, 80, 277, MediaPrintableArea.MM));
// discover the printers that can print the format according to the
// instructions in the attribute set
//PrintService service = PrintServiceLookup.lookupDefaultPrintService();
printJob(printerName, myDoc, aset);
}
}.start();
}
private static synchronized void printJob(String printerName, Doc myDoc, PrintRequestAttributeSet aset) {
DocPrintJob job = getPrintService(printerName).createPrintJob();
try {
job.print(myDoc, aset);
}
catch (Exception ex) {
ExceptionLogger.logException(ex, "Error while printing", false);
}
}
public static PrintService getPrintService(String printerName) {
printerName = printerName.trim().toLowerCase();
if(printerName.equalsIgnoreCase(DEFAULT_PRINTER)) return PrintServiceLookup.lookupDefaultPrintService();
if(printers.isEmpty()) {
PrintService theServices[] = PrintServiceLookup.lookupPrintServices(DocFlavor.I NPUT_STREAM.AUTOSENSE, null);
//DocFlavor.SERVICE_FORMATTED.PRINTABLE
if(theServices == null || theServices.length == 0) return PrintServiceLookup.lookupDefaultPrintService();
for(int i = 0; i < theServices.length; i++) {
printers.put(theServices[i].getName().trim().toLowerCase(), theServices[i]);
}
}
PrintService theService = (PrintService)printers.get(printerName);
if(theService != null) return theService;
return PrintServiceLookup.lookupDefaultPrintService();
}
}
Ndryshuar për herë të fundit nga Pogradecari : 15-11-2003 më 14:34
-
Regullat e Postimit
- Ju nuk mund të hapni tema të reja.
- Ju nuk mund të postoni në tema.
- Ju nuk mund të bashkëngjitni skedarë.
- Ju nuk mund të ndryshoni postimet tuaja.
-
Rregullat e Forumit
Krijoni Kontakt