现在要实现一个在JSP上点击打印,调用一个applet,上面有个打印按钮,可以调用本地打印(我用PrinterJob实现了)现在从后台传了一个字符串,用这个applet接收,并且解析打印,我不知道如何使用这个printerJob来实行打印,我传过来的是一串很长的字符串,上面记载了要打印的内容,坐标,宽度,字体,调用applet中的方法把他传过来的字符串用g.drawString画出来(我写了一个applet可以显示出来),现在不知道怎么和printerJob整合起来,直接送到打印机,就是按applet中的打印直接送打印机打印出来,那些解析的字符串不必画在页面上,我对 applet不是很熟,希望各位大虾指教下
底下是画出Graphics 的代码
public void adjust(Graphics g, String str, Font font, float left(横坐标), float up(纵坐标),
float width(打印宽度), float height(打印高度), float lineHeight(打印行数), int mode(打印居中), int num()) { // 打印时版面的调整
// float Y = up+(height-num*lineHeight)/2; // 居中
float Y = up; // 靠顶
System.out.println("left==="+left);
try {
String strLine = "";
StringReader reader = new StringReader(str);
BufferedReader data = new BufferedReader(reader);
int strBegin = 0, strEnd = 0;
while (strLine != null) {
strLine = data.readLine();
if (strLine != null) {
if (strLine.length() > 0) {
Hashtable map = new Hashtable();
map.put(TextAttribute.FONT, font);
System.out.println("font===="+font);
Font font1=new Font("宋体",0,12);
// System.out.println("1111111");
g.setFont(font1);
AttributedString as = new AttributedString(strLine, map); AttributedCharacterIterator aci = as.getIterator();
int startIndex = aci.getBeginIndex();
int endIndex = aci.getEndIndex(); LineBreakMeasurer measurer;
FontRenderContext frc = ((Graphics2D) g)
.getFontRenderContext();
measurer = new LineBreakMeasurer(aci, frc);
measurer.setPosition(startIndex); float wrappingWidth = width;
while (measurer.getPosition() < endIndex) {
TextLayout layout = measurer
.nextLayout(wrappingWidth);
strBegin = strEnd;
strEnd = strBegin + layout.getCharacterCount();
String draw = str.substring(strBegin, strEnd);
Y += layout.getAscent(); float X = left; switch (mode) {
default:
if (layout.isLeftToRight()) {
X = left;
} else {
X = left + wrappingWidth
- layout.getAdvance();
}
break;
case LEFT:
if (layout.isLeftToRight()) {
X = left;
} else {
X = left + wrappingWidth
- layout.getAdvance();
}
break; case RIGHT:
if (layout.isLeftToRight()) {
X = left + wrappingWidth
- layout.getVisibleAdvance();
} else {
X = left + wrappingWidth;
}
break; case CENTER:
if (layout.isLeftToRight()) {
X = left
+ (wrappingWidth - layout
.getVisibleAdvance()) / 2;
} else {
X = left
+ (wrappingWidth + layout
.getAdvance()) / 2
- layout.getAdvance();
}
break; case EQUALITY:
layout = layout
.getJustifiedLayout(wrappingWidth); }
g.drawString(draw, Math.round(X), Math.round(Y));
// 判断回车和换行符
if (strEnd < (str.length() - 1)
&& str.substring(strEnd, strEnd + 2)
.equals("\r\n")) {
strEnd = strEnd + 2;
} else if (strEnd < str.length()
&& str.substring(strEnd, strEnd + 1)
.equals("\n")) {
strEnd++;
}
}
} else if (strLine.length() == 0) {
Y += lineHeight;
if (strEnd < (str.length() - 1)
&& str.substring(strEnd, strEnd + 2).equals(
"\r\n")) {
strEnd = strEnd + 2;
} else if (strEnd < str.length()
&& str.substring(strEnd, strEnd + 1).equals(
"\n")) {
strEnd++;
}
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

解决方案 »

  1.   

    Button btn;
    String str;
    final static int LEFT = 0; final static int RIGHT = 1; final static int CENTER = 2; final static int EQUALITY = 3;
      public   void   init(){   
      btn = new Button("打印");
      add(btn);
      btn.addActionListener(this);
      } public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    final PrinterJob pJob=PrinterJob.getPrinterJob();
    if(pJob.printDialog()){

    }
    }
    这是调用PrinterJob