• 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;
    	}
    	



     
  • 相关阅读:
    [WebService]请求因 HTTP 状态 404 失败: Not Found。
    [WebService]请求因 HTTP 状态 400 失败: Bad Request。
    关于静态构造函数
    asp.net生成高质量缩略图通用函数
    ASP.NET 操作消息队列
    Asp.net 字符串操作类(补充引用)
    一行代码挂掉IE6,哈哈.......
    .Net代码生成器
    TSQL 跨库查询语句
    获取服务器地址的方法区别
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054190.html
Copyright © 2020-2023  润新知