zoukankan      html  css  js  c++  java
  • java写出PDF

    1包

    <dependency>
    			<groupId>com.itextpdf</groupId>
    			<artifactId>itextpdf</artifactId>
    			<version>5.2.0</version>
    		</dependency>
    		<dependency>
    		    <groupId>com.itextpdf</groupId>
    		    <artifactId>itext-asian</artifactId>
    		    <version>5.2.0</version>
    		</dependency>


    2、画格子

    @RequestMapping("capitalBill")
    	public ModelAndView capitalBill(
    			int type,  //1日2月3季度4年
    			String dateBegin,//yyyy-MM-dd
    			String dateEnd,//yyyy-MM-dd
    			HttpServletRequest request,
    			HttpServletResponse response, HttpSession session
    			) throws Exception{
    		ModelAndView mov = new ModelAndView("/platformCapitalBill");
    		//查询数据
    		if(type==1){
    			//日报表数据, 某天0:00到23:59:59
    			//查询日报表对象
    		}else if(type==2){//月
    			
    		}else if(type==3){//季度
    			
    		}else if(type==4){//年
    			
    		}
    		//展示数据
    		response.setContentType("application/pdf");
    		response.setHeader("Content-disposition", "attachment;filename="
    				+ new String("报表".getBytes("gb2312"), "ISO8859-1")
    				+ ".pdf");
    
    		ByteArrayOutputStream stream = new ByteArrayOutputStream();
    		// 获取响应数据流
    		ServletOutputStream out = response.getOutputStream();
    		// 创建文档对象,A4纸大小,40边距
    		Document document = new Document(PageSize.A4, 80, 80, 80, 80);
    		PdfWriter.getInstance(document, stream);
    		// 解决中文不显示,设定初始字体
    		BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
    		BaseFont baseFont_V = BaseFont.createFont("STSong-Light","UniGB-UCS2-V", BaseFont.NOT_EMBEDDED);
    		Font fonttitle = new Font(baseFont, 16, Font.BOLD);
    		Font font12bold = new Font(baseFont, 12, Font.BOLD);
    		Font font8 = new Font(baseFont, 8);
    		Font font8bold = new Font(baseFont, 8, Font.BOLD);
    		// 换行
    		Paragraph enterParagraph = new Paragraph("
    ");
    		// 打开文档
    		document.open();
    		//获取路径,用于获取图片等资源
    		ServletContext servletContext = request.getSession()
    				.getServletContext();
    		String basePath = servletContext.getRealPath("/");
    		
    		// 创建标题
    		Paragraph fileTile = new Paragraph("环链平台资金日报表", fonttitle);
    		fileTile.setAlignment(Paragraph.ALIGN_CENTER);
    		fileTile.setSpacingAfter(5);// 设置与后边段落间距
    		document.add(fileTile);
    		
    		// 下载日期
    		SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
    		Paragraph ParaDate = new Paragraph("下载日期: " + sf.format(new Date()),font8);
    		ParaDate.setAlignment(Paragraph.ALIGN_LEFT);
    		ParaDate.setIndentationLeft(150);
    		document.add(ParaDate);
    
    		
    		// 创建一个表格
    		PdfPTable table = new PdfPTable(6);
    		table.setTotalWidth(new float[] { 28, 56, 163, 28, 56, 163 });// 去掉左右各40,515
    		table.setLockedWidth(true);
    		table.setSpacingBefore(5);
    		
    		// 第一行
    		PdfPCell tableCell = new PdfPCell(new Paragraph("报表日期:", new Font(baseFont, 8, Font.BOLD)));
    		tableCell.setColspan(2);
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("2039-12-12", new Font(baseFont, 8)));
    		tableCell.setColspan(4);
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    	
    		
    		
    		
    		// 第二行
    		tableCell = new PdfPCell(new Paragraph("上期余额:", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(2);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("财务环链支付在关联方之间形成新的债权债务", font8));
    		tableCell.setColspan(4);
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		
    		
    		
    		
    		
    		// 第10行
    		tableCell = new PdfPCell(new Paragraph("其中:", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(1);
    		//tableCell.setRowspan(1);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("服务费存入:", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(1);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("100.00", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(2);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		// 第11行
    
    		tableCell = new PdfPCell(new Paragraph("其他存入", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(2);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("00", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("其它转出:", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(2);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("00", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		
    		
    		// 第12行
    
    		tableCell = new PdfPCell(new Paragraph("合计:", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(2);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("0.0", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("合计:", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setColspan(2);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("0.0", font8bold));
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		
    		
    		tableCell = new PdfPCell(new Paragraph("本期全额", new Font(baseFont, 8, Font.BOLD)));
    		tableCell.setColspan(2);
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		tableCell = new PdfPCell(new Paragraph("2039-12-12", new Font(baseFont, 8)));
    		tableCell.setColspan(4);
    		tableCell.setMinimumHeight(20);
    		tableCell.setUseAscender(true);
    		tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    		tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    		table.addCell(tableCell);
    		//添加
    		document.add(table);
    		// 关闭文档
    		document.close();
    		// 文档输出
    		stream.writeTo(out);
    		out.flush();
    		out.close();
    		return mov;
    	}
    	



     
  • 相关阅读:
    《算法笔记》9. 培养贪心思维、贪心算法深度实践
    《算法笔记》8. 二叉树的递归思维实战
    《算法笔记》7. 二叉树基本算法整理
    《算法笔记》6. 链表相关面试题总结
    大家是怎么做APP接口的版本控制的?欢迎进来看看我的方案。升级版的Versioning
    secure 审计暴力登陆
    linux 查询登陆成功、失败的用户
    win10 ctrl与alt键互换
    如何保证外包团队接入企业内网安全
    学习正则匹配的一些经验
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054190.html
Copyright © 2011-2022 走看看