159彩票

欢迎访问石家庄捷弘科技有限公司!

虚拟打印

新闻分类

产品分类

联系我们

企业名称:石家庄捷弘科技有限公司

联系人:张经理

电话:0311-82971406

手机:18931131467

网址:www.dlsney.com

地址:河北省石家庄市裕华区盛和广场A1-303-2


打印拦截/Java怎么实现打印机监控

您的当前位置: 首 页 >> 159彩票 >> 行业动态

打印拦截/Java怎么实现打印机监控

发布日期:2018-10-14 作者: 点击:

实现目标:
1、可以测试打印机是否连接
2、监控打印机的状态:打印机缺纸、打印机卡纸
我从PrintService里获取PrinterStateReasons,可是总是为空。不知道怎么才能获取到打印机的PrinterStateReasons

Java代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.PrinterStateReasons;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import javax.print.event.PrintServiceAttributeEvent;
import javax.print.event.PrintServiceAttributeListener;

import com.insigma.afc.common.LogHelper;

public class TextPrinter implements Printable {
private static final LogHelper s_log = new LogHelper(TextPrinter.class);

// 半角下的中文字符
public static final short HALF_CHINESE = 0;

// 半角下的英文字符
public static final short HALF_ENGLISH = 1;

protected String text;

protected String seperator;

public TextPrinter() {

}

public void print(String text, String seperator) {

this.text = text;
this.seperator = seperator;

DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

aset.add(new MediaPrintableArea(20, 20, 700, 700, MediaPrintableArea.MM));

PrintService services = PrintServiceLookup.lookupDefaultPrintService();

if (services != null) {
DocPrintJob pj = services.createPrintJob();
services.addPrintServiceAttributeListener(new PrintServiceAttributeListener() {

public void attributeUpdate(PrintServiceAttributeEvent psae) {
PrintServiceAttributeSet attribute = psae.getAttributes();
}

});
pj.addPrintJobListener(new PrintJobListener() {

public void printDataTransferCompleted(PrintJobEvent pje) {
s_log.info("111111111111 " + pje.getPrintEventType());
}

public void printJobCanceled(PrintJobEvent pje) {
s_log.info("22222222222222222 " + pje.getPrintEventType());
}

public void printJobCompleted(PrintJobEvent pje) {
s_log.info("33333333333333 " + pje.getPrintEventType());
}

public void printJobFailed(PrintJobEvent pje) {
s_log.info("444444444444444 " + pje.getPrintEventType());

}

public void printJobNoMoreEvents(PrintJobEvent pje) {
s_log.info("5555555555555555555 " + pje.getPrintEventType());
System.out.println("打印失败:" + pje.JOB_FAILED);
System.out.println("没有事件:" + pje.NO_MORE_EVENTS);
}

public void printJobRequiresAttention(PrintJobEvent pje) {
s_log.info("66666666666666666666 " + pje.getPrintEventType());
}

});
try {
Doc doc = new SimpleDoc(this, flavor, null);
pj.print(doc, aset);
PrinterStateReasons printerStateReasons = services
.getAttribute(PrinterStateReasons.class);
System.out.println("-----------" + printerStateReasons);
} catch (PrintException e) {
e.printStackTrace();
System.err.println(e);
}
} else {
s_log.info("未找到打印机。");
}
}

public int print(Graphics g, PageFormat pf, int pageIndex) {

if (pageIndex == 0) {

Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(Color.black);

String[] lines = text.split(seperator);

for (int i = 0; i < lines.length; i++) {
g2d.drawString(lines[i], 0, (i + 1) * 20);
}

return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}

/**
* 为中文字符对齐补空格(打印对齐时候用)
*
* @param content
* 需要补空格的字符
* @param expectLength
* 占用的最大长度
* @param type
* 字符的类型:中文、英文
* @return
*/
public static String validChineseString(String content, int expectLength, short type) {
String resultString = content.trim();
String space = null;
switch (type) {
case HALF_CHINESE: {
space = " ";
break;
}
case HALF_ENGLISH: {
space = " ";
break;
}

default:
break;
}
int contentLength = content.length();
if (contentLength < expectLength) {
int different = expectLength - contentLength;
for (int i = 0; i < different; i++) {
resultString += space;
}
}
System.out.println(content + " " + resultString);
return resultString;
}
}

虚拟打印、打印拦截、打印监控


本文网址:http://www.dlsney.com/news/451.html

关键词:虚拟打印,打印拦截,打印监控

最近浏览:

  • 在线客服
  • 联系电话
    18931131467
  • 在线留言
  • 在线咨询
    内蒙古快三走势图 内蒙古快3走势图 内蒙古快3 170彩票官网 百万彩票计划软件 八马彩票官网 博牛彩票计划软件 博美彩票充值 内蒙古快3走势图 内蒙古快3