java打印

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值