itext jsp页面打印
最近項目中需要一個打印功能,需求很簡單,只要打印出單據就可以了,畫出一個表格,一些信息需要從數據庫中提取
找到了免費的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/archive/2012/11/18/2746733.html
總結
以上是生活随笔為你收集整理的itext jsp页面打印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: static 与 extern 关键字描
- 下一篇: 安卓修改默认音量