itext jsp页面打印 android

最近项目中需要一个打印功能,需求很简单,只要打印出单据就可以了,画出一个表格,一些信息需要从数据库中提取

找到了免费的itext,可以实现我的简单的功能了

代码:(打印方法)

public String createPDF(HttpServletRequest request, HttpServletResponse response, String billCode,String comm) {
 Rectangle  rectangle = new Rectangle((float) 663.14 , (float) 370.7);
    Document document = new Document(rectangle,40,50,22,10);// 建立一个Document对象
    //Document document=new Document(PageSize.A4,36,36,36,36);
    //document.setPageSize(PageSize.A4);// 设置页面大小
    //String billCode = "20121026181";
   List<ClientreCharge> reList = cRechargeManager.getreChargeByBillCode(billCode);
   SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分");
   String time = df.format(new Date());
    try {
     ByteArrayOutputStream ops = new ByteArrayOutputStream();//输出到客户段的流
     //OutputStream ops=new FileOutputStream("e:/taony125-test.pdf");//保存进磁盘的流
     //两个ops输出流用来切换,是保存进磁盘还是输出到客户端
   
     PdfWriter.getInstance(document,ops);// 建立一个PdfWriter对象
     document.open();
     BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 设置中文字体
   
     Font headFont1 = new Font(bfChinese, 14, Font.BOLD);// 设置字体大小
     Font headFontN = new Font(bfChinese, 13, Font.BOLD);// 设置字体大小
     Font headFont2 = new Font(bfChinese, 12, Font.BOLD);// 设置字体大小
     Font headFont3 = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小
   
     float[] widths = { 100, 110, 110, 110, 100, 110 };// 设置表格的列以及列宽
     PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
   
     table.setSpacingBefore(130f);// 设置表格上面空白宽度
     table.setTotalWidth(535);// 设置表格的宽度
     table.setLockedWidth(true);// 设置表格的宽度固定
     // table.getDefaultCell().setBorder(0);//设置表格默认为无边框
     PdfPCell cell = new PdfPCell(new Paragraph("石家庄瑞特维通讯网络有限公司充值业务单",headFont1));// 建立一个单元格
     // cell.setBorder(0);//设置单元格无边框
   
     cell.setBorder(0);
     cell.setFixedHeight(40);//单元格高度
     cell.setColspan(6);// 设置合并单元格的列数
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置内容水平居中显示
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     table.addCell(cell);
   
     //列标题
     cell = new PdfPCell(new Paragraph("NO:", headFontN));
     cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
     cell.setBorder(0);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(billCode, headFontN));
     cell.setBorder(0);
     cell.setColspan(3);
     table.addCell(cell);
    
     cell = new PdfPCell(new Paragraph(time, headFontN));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setColspan(2);
     cell.setBorder(0);
     table.addCell(cell);
   
     cell = new PdfPCell(new Paragraph("业务专员",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
    
     cell = new PdfPCell(new Paragraph(reList.get(0).getClientSellerName(),headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("企业号",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(reList.get(0).getClireClMark(),headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("客服专员",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(reList.get(0).getClientsevName(),headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
    
     cell = new PdfPCell(new Paragraph("公司名称",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     cell.setFixedHeight(30);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(reList.get(0).getClientName() ,headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     cell.setFixedHeight(30);
     cell.setColspan(3);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("新开",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     cell.setFixedHeight(30);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("口  是  | 口  否",headFont3));
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     //Graphics grx = null ;
     //grx.s
     //grx.drawRect(0, 0, 2, 2);
   //添加一个矩形
    //cell.addElement((Element) grx);

     table.addCell(cell);
    
     cell = new PdfPCell(new Paragraph("联系人",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(reList.get(0).getContent(),headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("联系电话",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(reList.get(0).getMoney(),headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("充值单价",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(reList.get(0).getCounts(),headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
    
     PdfPTable table2 = null;
     //cell.setRowspan(3);
     if (reList.size() < 2){
      table2 = new PdfPTable(1);
      ClientreCharge creCharge = reList.get(0);
       cell = new PdfPCell(new Paragraph("充值属性:" +creCharge.getDictName()+ "   金额: "
                  + creCharge.getClireMoney() + "         ",headFont3));
       cell.setBorder(0);
       //cell.setColspan(6);
       table2.addCell(cell);
     
     }else{
    
      table2 = new PdfPTable(2);
     for (ClientreCharge creCharge : reList){
      cell = new PdfPCell(new Paragraph("充值属性:" +creCharge.getDictName()+ "   金额: "
                 + creCharge.getClireMoney() + "         ",headFont3));
      cell.setBorder(0);
      //cell.setColspan(6);
      table2.addCell(cell);
     }}
     cell = new PdfPCell(table2);
     cell.setFixedHeight(80);
     cell.setColspan(6);
     table.addCell(cell);
    
     cell = new PdfPCell(new Paragraph("回款方式",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     cell.setRowspan(2);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("银行对公",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(" 口 中行    口    工行",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     cell.setColspan(2);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("回款时间",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(" ",headFont3));
     cell.setFixedHeight(20);
     table.addCell(cell);
    
     cell = new PdfPCell(new Paragraph("银行对私",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("口  工行    口 建行    口  交行    口  农行   口  光大银行   口  张家口银行   口  现金",headFont3));
     cell.setFixedHeight(20);
     cell.setColspan(4);
     table.addCell(cell);
    
     cell = new PdfPCell(new Paragraph("充值确认",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph(reList.get(0).getUsers(),headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("  ",headFont3));
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("业务确认",headFont3));
     cell.setHorizontalAlignment(Element.ALIGN_CENTER);
     cell.setFixedHeight(20);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("",headFont3));
     cell.setFixedHeight(20);
     cell.setColspan(2);
     table.addCell(cell);
    
    
     cell = new PdfPCell(new Paragraph("备注:" + comm, headFont3));
     // cell.setBorder(0);
     cell.setFixedHeight(50);
     cell.setColspan(6);
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
     table.addCell(cell);

     document.add(table);
     document.close();
   
     //输出到客户端 start,假如是保存进磁盘,则切换上面的流ops,且屏蔽此段代码即可
     response.setContentType("application/pdf");
     response.setContentLength(ops.size());
     ServletOutputStream out = response.getOutputStream();
     ops.writeTo(out);
     out.flush();
     //end
   
    } catch (DocumentException de) {
     System.err.println(de.getMessage());
     return null;
    } catch (IOException ioe) {
     System.err.println(ioe.getMessage());
     return null;
    }
    return null;
 }

效果:

守护
原文地址:https://www.cnblogs.com/suhe/p/2746733.html