那都不算事 2017-11-22 02:51 采纳率: 0%
浏览 1235
已结题

怎么把我用controller实现的打印功能通过japplet放在浏览器中实现

现在就是需要做一个打印功能,我在java程序中写了一个controller,但是现在需要把程序通过japplet放到浏览器中运行,相当于在浏览器中调用我本地的java程序,但是看了许多文章就是搞不好,我现在把我写的打印的小程序发出来,求教大神怎么改
package print.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import Entity.Word;
import tool.SalesTicket2;
import tool.YcPrinter2;

@Controller
public class PrintController {
@RequestMapping(value = "Print2")
public String print2(HttpServletRequest req,Model model) {

String name=req.getParameter("name"); //项目名称

String demand=req.getParameter("demand"); //要求
String replenish=req.getParameter("replenish"); //补充内容
String score=req.getParameter("score"); //待遇
String check=req.getParameter("check"); //检查考核
String login=req.getParameter("login"); //责任人签字
String other=req.getParameter("other"); //其他
List word=new ArrayList();
word.add(new Word(name,demand,replenish,score,check,login,other));

SalesTicket2 stk2=new SalesTicket2(word);
YcPrinter2 p=new YcPrinter2(stk2);
p.printer();
return "yes";

}
}
下面是打印排版的设置
package tool;
import java.awt.BasicStroke;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import Entity.Word;
import Entity.GoodInfo;
public class SalesTicket2 implements Printable{
private List word; //选项列表
private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
@Override
public String toString() {
return "SalesTicket [word=" + word + "]";
}

public SalesTicket2(List word) {
super();
this.word = word;
}
private int count=1;

@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

Graphics2D g2 = (Graphics2D) graphics;

g2.setColor(Color.black);
double x= pageFormat.getImageableX();

double y= pageFormat.getImageableY();

Font font = new Font("宋体",Font.BOLD,20);
g2.setFont(font);
float heigth=30;
g2.drawString("有限公司",(float)x+63,(float)y+heigth);
g2.drawString("QYZ管理责任书",(float)x+153,(float)y+2*heigth);
BasicStroke bs1 = new BasicStroke((float) 0.5);

g2.setStroke(bs1);
if(word!=null && word.size()>0){
for(Word w:word){
g2.setFont(new Font("宋体", Font.BOLD,12));
// float heigth2 = new Font("宋体", Font.BOLD,10).getSize2D();
float heigth2 =15;

g2.drawString("一、项目:",(float)x, (float)y+4*heigth);
g2.drawString("二、要求:",(float)x, (float)y+5*heigth);
 g2.drawLine((int)x,(int)(y+8*heigth),(int)x+460,(int)(y+8*heigth));
g2.drawLine((int)x,(int)(y+11*heigth),(int)x+460,(int)(y+11*heigth));
g2.drawLine((int)x,(int)(y+8*heigth),(int)x,(int)(y+11*heigth));
g2.drawLine((int)x+460,(int)(y+8*heigth),(int)x+460,(int)(y+11*heigth));

g2.drawString("补充内容:",(float)x+12, (float)y+9*heigth-10);     
g2.drawString("三、待遇:",(float)x, (float)y+12*heigth);     
g2.drawString("四、检查考核:",(float)x, (float)y+13*heigth);     
g2.drawString("五、责任人签字:",(float)x, (float)y+16*heigth);
g2.drawString("六、其他:",(float)x, (float)y+19*heigth);
g2.drawString(sdf.format(new Date()), (float)x+350,(float)y+22*heigth);//时间

g2.setFont(new Font("宋体", Font.PLAIN,10));//设置正文字体  
g2.drawString(w.getName(),(float)x+56, (float)y+4*heigth);

int Demandlen= w.getDemand().length();
if(Demandlen<=38){
 g2.drawString(w.getDemand(),(float)x+56, (float)y+5*heigth);
}else if(38<Demandlen&Demandlen<=76){
 g2.drawString(w.getDemand().substring(0, 38),(float)x+56, (float)y+5*heigth);
 g2.drawString(w.getDemand().substring(38, Demandlen),(float)x+56, (float)y+5*heigth+heigth2);
}else if(76<Demandlen&Demandlen<=114){
 g2.drawString(w.getDemand().substring(0, 38),(float)x+56, (float)y+5*heigth);
 g2.drawString(w.getDemand().substring(38, 76),(float)x+56, (float)y+5*heigth+heigth2);
 g2.drawString(w.getDemand().substring(76, Demandlen),(float)x+56, (float)y+5*heigth+2*heigth2);
}else if(114<Demandlen&Demandlen<=152){
 g2.drawString(w.getDemand().substring(0, 38),(float)x+56, (float)y+5*heigth);
 g2.drawString(w.getDemand().substring(38, 76),(float)x+56, (float)y+5*heigth+heigth2);
 g2.drawString(w.getDemand().substring(76, 114),(float)x+56, (float)y+5*heigth+2*heigth2);
 g2.drawString(w.getDemand().substring(114, Demandlen),(float)x+56, (float)y+5*heigth+3*heigth2);
}
else{
 System.out.println("要求");
 break;
}


g2.drawString(w.getScore(),(float)x+56,(float)y+12*heigth);


int Loginlen= w.getLogin().length();
if(Loginlen<=34){
 g2.drawString(w.getLogin(),(float)x+95,(float)y+16*heigth);
}else if(34<Loginlen&Loginlen<=68){
 g2.drawString(w.getLogin().substring(0, 34),(float)x+95, (float)y+16*heigth);
 g2.drawString(w.getLogin().substring(34, Loginlen),(float)x+95, (float)y+16*heigth+heigth2);
}else if(68<Loginlen&Loginlen<=102){
 g2.drawString(w.getLogin().substring(0, 34),(float)x+95, (float)y+16*heigth);
 g2.drawString(w.getLogin().substring(34, 68),(float)x+95, (float)y+16*heigth+heigth2);
 g2.drawString(w.getLogin().substring(68, Loginlen),(float)x+95, (float)y+16*heigth+2*heigth2);
}else if(102<Loginlen&Loginlen<=136){
 g2.drawString(w.getLogin().substring(0, 34),(float)x+95, (float)y+16*heigth);
 g2.drawString(w.getLogin().substring(34, 68),(float)x+95, (float)y+16*heigth+heigth2);
 g2.drawString(w.getLogin().substring(68, 102),(float)x+95, (float)y+16*heigth+2*heigth2);
 g2.drawString(w.getLogin().substring(102, Loginlen),(float)x+95, (float)y+16*heigth+3*heigth2);
}else{
 System.out.println("签名");
 break;
}
int Otherlen= w.getOther().length();
System.out.println(Otherlen);
if(Otherlen<=38){
 g2.drawString(w.getOther(),(float)x+56,(float)y+18*heigth);
}else if(38<Otherlen&Otherlen<=76){
 g2.drawString(w.getOther().substring(0, 38),(float)x+56, (float)y+19*heigth);
 g2.drawString(w.getOther().substring(38, Otherlen),(float)x+56, (float)y+19*heigth+heigth2);
}else if(76<Otherlen&Otherlen<=114){
 g2.drawString(w.getOther().substring(0, 38),(float)x+56, (float)y+19*heigth);
 g2.drawString(w.getOther().substring(38, 76),(float)x+56, (float)y+19*heigth+heigth2);
 g2.drawString(w.getOther().substring(76, Otherlen),(float)x+56, (float)y+19*heigth+2*heigth2);
}else if(114<Otherlen&Otherlen<=152){
 g2.drawString(w.getOther().substring(0, 38),(float)x+56, (float)y+19*heigth);
 g2.drawString(w.getOther().substring(38, 76),(float)x+56, (float)y+19*heigth+heigth2);
 g2.drawString(w.getOther().substring(76, 114),(float)x+56, (float)y+19*heigth+2*heigth2);
 g2.drawString(w.getOther().substring(114, Otherlen),(float)x+56, (float)y+19*heigth+3*heigth2);
}

else{
 System.out.println("其他");
 break;
}

// g2.drawString(w.getOther(),(float)x+48,(float)y+16*heigth);

}
}

switch (pageIndex) {

case 0:

return PAGE_EXISTS; //0
default:

return NO_SUCH_PAGE; //1
}

}
}
这就是把上面的内容加载出来打印
package tool;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

/**

  • 打印对象 -> 实现Printable接口即可调用打印机打印
  • 2017年5月23日 */ public class YcPrinter2{ private SalesTicket2 salesTicket;

public YcPrinter2(SalesTicket2 salesTicket){
this.salesTicket=salesTicket;
}

public void printer() {  
   try { //Book 类提供文档的表示形式,该文档的页面可以使用不同的页面格式和页面 painter
        Book book = new Book(); //要打印的文档

        //PageFormat类描述要打印的页面大小和方向  
        PageFormat pf = new PageFormat();  //初始化一个页面打印对象
        pf.setOrientation(PageFormat.PORTRAIT); //设置页面打印方向,从上往下,从左往右

        //设置打印纸页面信息。通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。  
        Paper paper = new Paper(); 
        paper.setSize(590,840);// 纸张大小  
        paper.setImageableArea(72,72,590,840);// A4(595 X 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72  
        pf.setPaper(paper);  

        book.append(salesTicket,pf);  

        PrinterJob job = PrinterJob.getPrinterJob();   //获取打印服务对象  

        job.setPageable(book);  //设置打印类  
        boolean a=job.printDialog();
        if(a){
            job.print(); //开始打印 
        }

    } catch (PrinterException e) {  
        e.printStackTrace();  
    }  

} 

}
省略了实体属性的封装

基本就是这了,就是现在要把这个简单打印功能通过japplet放在浏览器中实现功能。不知道有大神教教没.只有200分都给大神。

现在就是不知道怎么把这个java程序放进japplet里面,包括怎么传值,最好是放japplet的浏览器中有一个打印按钮,点击就能把浏览器中的值传进来打印就好了
更多
0
分享到:

  • 写回答

2条回答

  • danielinbiti 2017-11-22 05:38
    关注
     http://zyzcoder.iteye.com/blog/1714084
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?