package tloa.applet;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
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.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class PrintApplet extends Applet implements Printable {
//public static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
public void start() {
//
System.out.println("myapplet start");
//printContent();
}
private int PAGES = 0;
private String contentXml;
/*
* The method is declared in super interface must init it.
* Graphic指明打印的图形环境;PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英寸的1/72,1英寸为25.4毫米。A4纸大致为595×842点);page指明页号;
*/
public int print(Graphics gp, PageFormat pf, int page) throws PrinterException {
// this.get
Graphics2D g2 = (Graphics2D) gp;
g2.setPaint(Color.black); // 设置打印颜色为黑色
if (page >= PAGES) // 当打印页号大于需要打印的总页数时,打印工作结束
return Printable.NO_SUCH_PAGE;
g2.translate(pf.getImageableX(), pf.getImageableY());// 转换坐标,确定打印边界
Font font = new Font("宋体", Font.BOLD, 10);// 创建字体
g2.setFont(font);
//g2.drawString(optime, xstart, ystart);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbd = dbf.newDocumentBuilder();
//System.out.println
InputStream is=new ByteArrayInputStream(contentXml.getBytes());
Document doc=dbd.parse(is);
NodeList itemlist=doc.getElementsByTagName("item");
for(int i=0;i<itemlist.getLength();i++){
Element item=(Element) itemlist.item(i);
g2.drawString(item.getAttribute("text"),
Integer.parseInt(item.getAttribute("x")),
Integer.parseInt(item.getAttribute("y")));
//System.out.println("--text--"+item.getAttribute("text") );
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Printable.PAGE_EXISTS; // 存在打印页时,继续打印工作
}
// 打印内容到指定位置
public void printContent() {
//System.out.println("MyApplet printcontent ...");
PAGES = 1; // 获取打印总页数
//
Console.info("locate print svc");
// 定位默认的打印服务
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// 创建打印作业
//
Console.info("create print job");
DocPrintJob job = printService.createPrintJob();
// 设置打印属性
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 设置纸张大小,也可以新建MediaSize类来自定义大小
pras.add(MediaSizeName.ISO_A4);
DocAttributeSet das = new HashDocAttributeSet();
//
System.out.println("MyApplet printcontent 1...");
// 指定打印输出格式
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
javax.print.Doc doc = new SimpleDoc(this, flavor , das);
//
System.out.println("MyApplet 2...");
//
javax.print.Doc doc = new SimpleDoc(str, DocFlavor.STRING.TEXT_PLAIN , das);
// 不显示打印对话框,直接进行打印工作
try {
//
System.out.println("MyApplet 3...");
job.print(doc, pras); // 进行每一页的具体打印操作
} catch (PrintException pe) {
System.out.println("pe--"+pe.getMessage());
pe.printStackTrace();
}
}
/**
* 获取java版本
* @return
*/
public String getJavaVersion(){
return System.getProperty("java.version");
}
/**
* @return the contentXml
*/
public String getContentXml() {
return contentXml;
}
/**
* @param contentXml the contentXml to set
*/
public void setContentXml(String contentXml) {
this.contentXml = contentXml;
}
}
//js调用applet打印
//contentXml 打印内容及坐标
function doPrint(contentXml){
//alert("top.jsp--start");
//alert("top.jsp--"+contentXml);
document.printApplet.setContentXml(contentXml);
document.printApplet.printContent();
}
<span style="display:none;">
<applet code="tloa.applet.PrintApplet" archive="<%=ctxPath%>/resources/printApplet.jar" name="printApplet" width="100" height="100">
</applet>
</span>