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("\n");
// 打开文档
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;
}
java写出PDF的更多相关文章
- 如何用java写出无副作用的代码
搞java的同学们可能对无副作用这个概念比较陌生,这是函数式编程中的一个概念,无副作用的意思就是: 一个函数(java里是方法)的多次调用中,只要输入参数的值相同,输出结果的值也必然相同,并且在这个函 ...
- 2017.12.1 如何用java写出一个菱形图案
上机课自己写的代码 两个图形原理都是一样的 1.一共有仨个循环 注意搞清楚每一层循环需要做的事情 2.第一层循环:是用来控制行数 3.第二层循环控制打印空格数 4.第三层循环是用来循环输出星星 imp ...
- java写出图形界面
1. 做出简单的窗体 package javaGUI; import java.awt.BorderLayout; import java.awt.Color; import javax.swing. ...
- java写出进程条代码
package com.ds; import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import ...
- 《重学 Java 设计模式》PDF 出炉了 - 小傅哥,肝了50天写出18万字271页的实战编程资料
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! @ 目录 一.前言 二.简介 1. 谁发明了设计模式? 2. 我怎么学不会设计模式? 3. 适 ...
- 【原创】怎样才能写出优雅的 Java 代码?这篇文章告诉你答案!
本文已经收录自 JavaGuide (59k+ Star):[Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. 本文比较简短,基本就是推荐一些对于写好代码非常有用的文章或者 ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 如何写出无法维护的代码(JAVA版)
程序命名(针对那些不能混淆的代码) 容易输入的名字.比如:Fred,asdf 单字母的变量名.比如:a,b,c, x,y,z,或者干脆上中文比如(阿隆索肯德基) 有创意地拼写错误.比如:SetPint ...
- 如何写出好的Java代码?
1. 优雅需要付出代价.从短期利益来看,对某个问题提出优雅的解决方法,似乎可能花你更多的时间.但当它终于能够正确执行并可轻易套用于新案例中,不需要花上数以时计,甚至以天计或以月计的辛苦代价时,你会看得 ...
随机推荐
- mysql中常见的存储引擎和索引类型
存储引擎 1. 定义 存储引擎说白了就是如何存储数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在关系数据库中数据的存储是以表的形式存储的,所以存储引擎也可以称为表类 ...
- 图像处理与计算机视觉的 topics
光学图像(optical image): the apparent reproduction of an object, formed by a lens or mirror system from ...
- Tomcat 8(十)HTTP/AJP Connector、Bio/Nio/Apr性能对照
Tomcat 8(七)解读Bootstrap介绍过,Connector初始化/启动的时候,将初始化/启动内部的ProtocolHandler.事实上ProtocolHandler仅仅是个接口 Prot ...
- React路由配置
React路由简单配置 //入口文件index.js import React from 'react'; import ReactDom from 'react-dom'; import { Rou ...
- JavaScript:undefined And null差异
班吃饭的时候,同事偶然问了一个问题:undefined和null究竟有什么差别?无法回答,回去查阅相关文档,算了有了一个了解,做相关的总结.在開始之前,请看例如以下代码,算是抛出这个问题: conso ...
- MSRA专访摘要
前段时间有幸参加微软亚洲研究院之旅,顺便投简历,没想到在两次访谈迎来,并且是连续 的两次被拒绝.严重的刺激到了我.导致我疯狂的复习刷Offer.如今最终算是告于段落.如今也最终有空沉下心来总结 总结近 ...
- mysql常见操作汇总 专题
mysql中in多个字段 1. 基本用法 SELECT * FROM USER WHERE , , ); 2. 多个字段同时使用 SELECT * FROM USER WHERE (, ),(, ), ...
- Telnet协议详解(远程登陆协议)
1. 概述 Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议.Telnet协议的目的是提供一个相对通用的,双向的,面向八位字节的通信方法,允许界面终端设备和面向终 ...
- jquery 让图片飞
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 关于在.Net开发中使用Sqlite的版本选择问题
原文:关于在.Net开发中使用Sqlite的版本选择问题 如果一个.NET应用要自适应32位/64位系统,只需要在项目的“目标平台”设置为“Any CPU”.但是如果应用中使用了SQLite,情况就不 ...