1、获取Windows所有打印机
public List list getPrints() {
/*HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 查找所有的可用的打印服务
PrintService[] printService = PrintServiceLookup.lookupPrintServices(
flavor, pras);*/
PrintService[] pss = PrinterJob.lookupPrintServices();
PrintService ps = null;
List
/*for (int i = 0; i < printService.length; i++) {
// System.out.println(printService[i].getName());
Hashtable ht = new Hashtable();
ht.put("printName", printService[i].getName());
list.add(ht);
}*/
for (int i = 0; i < pss.length; i++) {
String sps = pss[i].toString();
sps = sps.replace("Win32 Printer : ", "");
Hashtable ht = new Hashtable();
//logger.info("打印机:"+sps);
ht.put("printName", sps);
list.add(ht);
}
return list;
}
2、将jasper内容输入到指定的打印机
public static boolean directPrintByPrintName(final JasperPrint jasperPrint,
String printName) {
if (jasperPrint != null) {
try {
PrintService[] PSs = PrinterJob.lookupPrintServices(); // java.awt.*包.查找所有打印服务.
PrintService ps = null;
if (PSs != null && PSs.length > 1 && printName != null
&& !printName.equals("")) {
for (int i = 0; i < PSs.length; i++) {
String sps = PSs[i].toString();
sps = sps.replace("Win32 Printer : ", ""); //$NON-NLS-1$ //$NON-NLS-2$
// printName 我们系统设置的默认打印机名称.
if (sps.equalsIgnoreCase(printName)) {
ps = PSs[i];// 得到打印服务对象
break;
}
}
}
// 设置打印参数,好多个参数
if (ps != null) {
long start = System.currentTimeMillis();
//PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
// printRequestAttributeSet.add(MediaSizeName.ISO_A5);//
// 处方模板是A5纸 第一个参数对象
//MediaSize ms = new MediaSize(280,380.0f,Size2DSyntax.MM,MediaSizeName.ISO_A3);
//printRequestAttributeSet.add(ms.getMediaSizeName());
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(ps.getName(),
null)); // 第二个参数对象
final JRPrintServiceExporter exporter = new JRPrintServiceExporter(); // 关键的对象,其它的对象都是为他服务的
// 以下为设置参数
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint);
/*
* exporter.setParameter(JRPrintServiceExporterParameter.
* PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
*/
exporter.setParameter(
JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
printServiceAttributeSet);
exporter.setParameter(
JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
Boolean.FALSE);
exporter.setParameter(
JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
Boolean.FALSE);
// 关键的出场,在线程里导出报表.(打印)
Thread thread = new Thread(new Runnable() {
public void run() {
try {
exporter.exportReport(); // 就这么一句.exporter对象导出报表.
} catch (Exception ex) {
System.err.println(ex.getLocalizedMessage());
logger.error(ContextUtil.getTrace(ex));
}
}
});
thread.start();
} else { // //7.采用默认打印.
Thread thread = new Thread(new Runnable() {
public void run() {
try {
// jasperPrint
// 对象就是JasperPrintManager生成的.参考上面的代码.
JasperPrintManager.printReport(jasperPrint,
false); // 这一句应该是默认打印.
} catch (Exception ex) {
logger.error(ContextUtil.getTrace(ex));
}
}
});
thread.start();
}
} catch (Exception ex) {
logger.error(ContextUtil.getTrace(ex));
return false;
}
}
return true;
}
注意:本文归作者所有,未经作者允许,不得转载